Questions tagged [python-attrs]

Use for questions about the third-party Python library for data classes

attrs is a Python package that greatly simplifies the creation of classes by taking care of boilerplate for you, such as __init__, __repr__, comparison methods, and more.

Resources:

80 questions
171
votes
11 answers

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 type of tedious repetitiveness? Should the class…
15
votes
3 answers

Setting default/empty attributes for user classes in __init__

I have a decent level of programming, and get much value from the community here. However I have never had much academic teaching in programming nor worked next to really experienced programmers. Consequently I sometimes struggle with 'best…
Andy
  • 543
  • 1
  • 4
  • 18
11
votes
2 answers

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 to fix it. The problem is forwarding very long…
Tom Swirly
  • 2,631
  • 1
  • 25
  • 41
9
votes
1 answer

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 expression with inputs; however, I do not…
jackalack
  • 445
  • 3
  • 11
7
votes
1 answer

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.validators.instance_of(str)) @attr.s class…
GlaceCelery
  • 429
  • 1
  • 6
  • 17
7
votes
1 answer

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 working y = attr.ib(type=List[int]) # not working…
Jeffrey Benjamin Brown
  • 2,618
  • 2
  • 21
  • 33
5
votes
1 answer

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.nested_attribute). My JSONs have a fixed schema, and I would…
gmolau
  • 2,188
  • 14
  • 34
5
votes
0 answers

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()) PyLint complains with the following error…
devilevil
  • 61
  • 3
5
votes
3 answers

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): ... x = attr.ib() ... y = attr.ib() ... >>>…
kuza
  • 1,853
  • 1
  • 14
  • 41
4
votes
4 answers

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 = Foo(max_count=2) f.get_max_plus_one =>3 I want…
suprita shankar
  • 1,274
  • 1
  • 12
  • 32
4
votes
1 answer

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) As you can see, they share 2 attributes (x and…
NI6
  • 1,329
  • 2
  • 12
  • 24
3
votes
0 answers

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 class that uses the enum attribute as a JSON…
3
votes
1 answer

mypy complains about TypedDict inside attrs class having Incompatible types

I have a TypedDict DictWithOnlyX inside an attrs data class Example where mypy complains about the type returned from the getdict() method of my class, even though the return type is declared: from typing_extensions import TypedDict from attr import…
abulka
  • 661
  • 8
  • 13
3
votes
2 answers

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__(self, i, special=None): if special: …
polo
  • 1,061
  • 2
  • 13
  • 27
3
votes
1 answer

Python attrs class attribute cached lazy load

I have classes that look like this: @attr.s class ImageMagic(object): path = attr.ib() _img = attr.ib() @_img.default def _img(self): return Image.open(self.path) @attr.s class FileObject(object): # Standard path…
Bojan Jovanovic
  • 1,319
  • 2
  • 17
  • 23
1
2 3 4 5 6