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:

144 questions
172
votes
10 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…
MWB
  • 11,740
  • 6
  • 46
  • 91
34
votes
4 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
  • 919
  • 2
  • 9
  • 22
20
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
  • 585
  • 4
  • 13
15
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,740
  • 1
  • 28
  • 44
14
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
  • 2,761
  • 3
  • 22
  • 56
10
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,554
  • 2
  • 16
  • 47
8
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
  • 3,427
  • 2
  • 28
  • 40
8
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,815
  • 1
  • 22
  • 45
7
votes
1 answer

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…
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
  • 921
  • 1
  • 13
  • 30
7
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
  • 81
  • 4
5
votes
1 answer

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 inheriting class, to define its properties. So I…
NI6
  • 2,477
  • 5
  • 17
  • 28
5
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
  • 2,477
  • 5
  • 17
  • 28
4
votes
2 answers

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 import BaseModel @attr.s(auto_attribs=True) class…
Atralupus
  • 131
  • 1
  • 7
4
votes
1 answer

Add type hints for init argument in attrs class

Is there a way of separating the type hints for an init arg to the type hint of an attribute? Please let me better explain myself by example. If in a regular class I'd do something like: from math import ceil from typing import Union class…
NI6
  • 2,477
  • 5
  • 17
  • 28
1
2 3
9 10