Skip to main content

Questions tagged [python-attrs]

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

python-attrs
3 votes
2 answers
286 views

How to mock an attr.ib validator

I have an attrs class: @attr.s class Example: my_int = attr.ib(validator=attr.validator.instance_of(MyComplexType)) I need to mock this validator.
zhukovgreen's user avatar
  • 1,608
3 votes
1 answer
2k views

Run attribute validator only after __attrs_post_init__ ends

I have: @attr.s class Example: number = attr.ib(validator=attr.validators.instance_of(int), init=False) def __attrs_post_init__(self): self.number = 'string' print('It seems, ...
zhukovgreen's user avatar
  • 1,608
4 votes
1 answer
475 views

What is the difference between super() and explicit super(Cl,self) (with __slots__ and attrs)

I'm using the attrs python package, in combination with inheritance and slots. I want to call the parent class's method from within the derived method. The problem is demonstrated below: import attr ...
Niobos's user avatar
  • 910
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): ... ...
kuza's user avatar
  • 2,971
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 ...
jackalack's user avatar
  • 615
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 ...
Tom Swirly's user avatar
  • 2,761
3 votes
5 answers
1k views

How To Deduce Or Subtype Named Tuple From Another Named Tuple?

Preface I was wondering how to conceptualize data classes in a pythonic way. Specifically I’m talking about DTO (Data Transfer Object.) I found a good answer in @jeff-oneill question “Using Python ...
kuza's user avatar
  • 2,971
0 votes
2 answers
612 views

Python attrs library and referencing instance methods

How can I convert the class below to use the attrs library: class MyClass(object): def __init__(self, api, template=None, **kwargs): self.api = api self.param1 = param1 if ...
Sage Wiseman's user avatar
3 votes
8 answers
929 views

Use Python for Creating JSON

I want to use Python for creating JSON. Since I found no library which can help me, I want to know if it's possible to inspect the order of the classes in a Python file? Example # example.py class ...
guettli's user avatar
  • 27.8k
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 ...
MWB's user avatar
  • 12.1k

15 30 50 per page
1
7 8 9 10
11