Questions tagged [python-attrs]
Use for questions about the third-party Python library for data classes
python-attrs
160
questions
172
votes
10
answers
16k
views
How do I avoid the "self.x = x; self.y = y; self.z = z" pattern in __init__?
I see patterns like
def __init__(self, x, y, z):
...
self.x = x
self.y = y
self.z = z
...
quite frequently, often with a lot more parameters. Is there a good way to avoid this ...
36
votes
4
answers
39k
views
Setting default/empty attributes for user classes in __init__ [closed]
When I am creating a new class, should I set all instance attributes in __init__, even if they are None and in fact later assigned values in class methods?
See example below for the attribute results ...
23
votes
1
answer
9k
views
When and why should I use attr.Factory?
When and why should I use attr.ib(default=attr.Factory(list)) over attr.ib(default=[])?
From the docs I see that a Factory is used to generate a new value, which makes sense if you are using a lambda ...
17
votes
3
answers
9k
views
How to achieve the reverse of "attr.asdict(MyObject)" using Python module 'attrs'
In documentation of Python module attrs stated that there is a method to convert attributes’ class into dictionary representation:
Example:
>>> @attr.s
... class Coordinates(object):
... ...
15
votes
2
answers
6k
views
Perfect forwarding - in Python
I am a maintainer of a Python project that makes heavy use of inheritance. There's an anti-pattern that has caused us a couple of issues and makes reading difficult, and I am looking for a good way ...
10
votes
1
answer
2k
views
How to specify that an attribute must be a list of (say) integers, not just a list?
Using the attrs libary and Python 3.6, I thought the following would allow me to specify that x and y can only contain integers:
import attr
@attr.s
class C:
x : List[int] = attr.ib() # not ...
10
votes
4
answers
2k
views
How to get @property methods in asdict?
I have something like:
from attr import attrs, attrib
@attrs
class Foo():
max_count = attrib()
@property
def get_max_plus_one(self):
return self.max_count + 1
Now when I do:
f = ...
9
votes
1
answer
9k
views
Using attrs to turn JSONs into Python classes
I was wondering if it possible to use the attrs library to convert nested JSONs to Python class instances so that I can access attributes in that JSON via dot notation (object.attribute....
7
votes
1
answer
5k
views
How to deserialise enumeration with string representation?
I would like to create a class, which has an enumeration as an attribute.
This enumeration should have a string representation that shows up as human-readable value when dumping the instance of the ...
7
votes
1
answer
490
views
Pycharm typehint on subclass using attrs
Pycharm is complaining about an unexpected argument to MyClass instance. Is there a way around this?
import attr
@attr.s
class _Super:
""" My Super class"""
x: str = attr.ib(validator=attr....
7
votes
0
answers
1k
views
Using attr with pylint
Using the attrs package seems to cause errors with PyLint:
import attr
@attr.s(slots=True)
class Foo:
d = attr.ib(attr.Factory(dict), type=dict)
f = Foo()
print('bar' in f.d)
print(f.d.items())...
6
votes
2
answers
7k
views
How to ignore field repr in pydantic?
When I want to ignore some fields using attr library, I can use repr=False option.
But I cloud't find a similar option in pydantic
Please see example code
import typing
import attr
from pydantic ...
6
votes
1
answer
6k
views
Python - attrs class inheriting from abstract class
I'm curious how attrs can be combined with abstract classes.
I Want to define an abstract class, that defines abstract properties which every inheriting class must have.
I want use attrs for this ...
5
votes
2
answers
6k
views
in a python attrs class, how can I override generated __init__ with my own
So I like to use attr but sometimes I need to do my own thing.
can I override the __init__ method with my own?
import attr
@attr.s(auto_attribs=True)
class MyClass:
i: int
def __init__(...
5
votes
1
answer
2k
views
Python attrs - positional attribute in super class while optional in sub class
I have 2 very similiar classes: A and B:
import attr
@attr.s
class A(object):
x = attr.ib()
y = attr.ib()
@attr.s
class B(object):
x = attr.ib()
z = attr.ib()
y = attr.ib(default=None)...