Questions tagged [python-attrs]
Use for questions about the third-party Python library for data classes
python-attrs
160
questions
0
votes
1
answer
165
views
Different objects for the same class initiated with same value while working with attrs module
I'm trying to create Model class with attrs module. I'm generating iid value with bson ObjectId which returns a unique value with every call. And generating timestamp value with time module.
While I ...
0
votes
1
answer
188
views
How to invoke default methods with attrs python package
All,
Thank you for taking the time to try and address my question. My question is related to a specific Python package called attrs (https://www.attrs.org/en/stable/). I am just getting started with ...
0
votes
1
answer
66
views
Error while assigning an attrs object class into other nested objects
I am having problem assigning the class Private into nested objects; the class validates the and converts a string into a UUID object. Or by default generates a uuid1 if noting is passed.
class ...
1
vote
1
answer
1k
views
Python attr.s multiple type validation
A class that generates a UUID by default if none is provided, and validates/creates a UUID object if a str is provided.
import attr
from attrs import validators
from uuid import UUID, uuid1
def ...
2
votes
0
answers
463
views
Typing for decorator that wraps attrs.frozen and adds a new field
I am trying to set up a class decorator in Python that acts like attr.frozen but adds an additional field before creation (as well as a few other things). While the code works fine, I'm having trouble ...
3
votes
1
answer
1k
views
How best to initialize a class using an attribute from attrs?
In vanilla Python to create a class which, when initiated, would also initiate another class I would use __init__(self):
class SomeClass:
__init__(self, token: str):
self.token = token
...
2
votes
2
answers
2k
views
Python attrs/cattrs serializing a dict using frozen attrs classes as the dict keys
I would like to structure and unstructure an attrs object, which includes dict fields that use simple frozen attrs for the dict keys. This works very well for objects created at runtime, but the ...
1
vote
1
answer
1k
views
Structure JSON to an `attrs` class with extra fields using `cattrs`?
I would like to structure JSON to an attrs class that allows for extra fields using cattrs. cattrs by default will ignore extra fields and if forbid_extra_keys=True an error is raised when extra ...
2
votes
1
answer
975
views
Is there a good way to document attributes with `python-attrs`?
I'm used to documenting my __init__() function with a docstring, but I would like to take advantage of the benefits of the attrs package. The docstring is useful in IPython or a Jupyter notebook, so ...
3
votes
1
answer
2k
views
How to solve "unexpected keyword argument" with python-attrs construct, so that mypy is happy?
I have a class A in a foreign library.
class A:
def __init__(self, a: int):
self.a = a
I would like to extend a class B with A like:
import attr
@attr.s
class B(A):
b: int = attr.ib()...
1
vote
2
answers
283
views
attributes that modify other attributes
I would like to make a Block class that has the following behavior: if iip3 is set, then oip3 is set to iip3 + gain. If oip3 is set, then iip3 is set to oip3 - gain like the following:
b = Block(gain =...
5
votes
2
answers
5k
views
Mypy returns Error "Unexpected keyword argument" for subclass of a decorated class with attrs package
I have two decorated classes using attrs package as follows:
@attr.s(kw_only=True)
class Entity:
"""
base class of all entities
"""
entity_id = attr.ib(...
0
votes
1
answer
826
views
Is there "on_getattr" equivalent functionality for attrs package?
I use attrs package for my entities and it works like a charm in most cases. An issue I faced is in defining getter for attributes (generally in pure python @property is the goto)
for setter attrs has ...
2
votes
3
answers
674
views
Creating Python class with mutually exclusive arguments using attrs
I have a class that has two mutually exclusive arguments (prices and returns). That is, they must not be both provided in order to instantiate an object.
However, the class needs both for internal ...
2
votes
2
answers
1k
views
construct an object from dict using cattr/attr with defaults set to None
I would like to recreate my object from a dictionary using cattr but it looks that this does not work with nested field set to None.
Here a code example that explains the problem:
import attr
import ...