Questions tagged [python-attrs]
Use for questions about the third-party Python library for data classes
python-attrs
160
questions
0
votes
1
answer
52
views
Correct mro order attrs object can't find abstract property
The following runs fine:
from abc import ABC, abstractmethod
import attr
class A(ABC):
@abstractmethod
def prop(self):
pass
def foo(self):
print(self.prop())
class B:
...
0
votes
2
answers
403
views
How can I specify order of fields in Attrs?
Taking from attrs example here but adding group_id & name. I want to create data classes so I can then export them to JSON using json.dump().
@define
class User:
email: str
password: str
...
0
votes
1
answer
292
views
frozen dataclass in def __init__ and iteration on it
I want to use a frozen class as a structure as I don't want to use any mutable objects in my code. But also I need to iterate on my_data. How can I make this work?
SideNote: dict is not an option
from ...
1
vote
2
answers
482
views
Passing parameters to frozen super dataclass
I have a simple (frozen) dataclass inheritance as follows:
from attrs import frozen
@frozen
class Interval:
left: str
right: str
@frozen
class RealInterval(Interval):
def ...
1
vote
2
answers
356
views
Attribute Converter in attr.s-based hydra Structured Configs
Is it possible to use converters in attr.s-based hydra-structured configs.
I tried it with this minimal example:
import hydra
from hydra.core.config_store import ConfigStore
import attr
@attr.s
class ...
3
votes
1
answer
767
views
How do I annotate an `attrs` validator function without breaking it?
Using the attrs library, I can define a validator for attribute values:
from attrs import define, field
def is_odd(inst, attr, value):
if value % 2 == 1:
return None
raise ValueError(...
0
votes
1
answer
192
views
frozen data class with non trivial constructor
I'm trying to define a frozen data class with a non-trivial constructor
That is, the constructor needs to "tweak" the input before it initializes the corresponding data member:
from attrs ...
1
vote
1
answer
475
views
Correct backwards-compatible way to migrate to modern attrs/cattrs style?
Question
I have a set of projects that I wrote starting in early 2020 using attrs v19.3.0 and cattrs for serialization/deserialization. What is the correct, fully backwards-compatible way to migrate ...
4
votes
2
answers
4k
views
Python attrs: Inheriting from class which values have default values raises error
I have a situation where attrs class inherits from another class which attributes have default value. This raises ValueError.
Here's an example:
from attrs import define
@define
class A:
a: int = ...
0
votes
1
answer
1k
views
Create a customer converter and validator using ATTRS
I am trying to learn attrs and I have two questions. Please note that I am using the ATTRS library, not ATTR.
How do I create a converter to change typ to uppercase?
---> I solved this question. ...
2
votes
2
answers
2k
views
serializing to JSON an array of data classes
I have an array of data classes I need to serialize to JSON. I wrap the list in a class (Persons)
@attrs.frozen
class Person:
name: str
age: int
grades: Dict[str, int]
@attrs.define
class ...
2
votes
1
answer
655
views
Why can I not add attributes to an attrs object?
Consider this example:
import attrs
@attrs.define(frozen=True)
class A:
def __init__(self):
object.__setattr__(self, "field", 1)
A()
This fails with an exception:
...
0
votes
1
answer
116
views
With Python's attrs, is it possible to remove leading underscores from the generated __repr__
With my project that uses attrs, I'm defining some fields with a leading underscore so that I can make a property with the same name minus the leading underscore. I'm doing this because I'm adding ...
0
votes
2
answers
2k
views
How to define a class attribute by other attributes of the same class using "attrs"
I'm new with attrs module & I've encountered something that I didn't understand very well.
For simplicity I have this code below:
import attr
from typing import List
@attr.define
class A:
a: ...
1
vote
1
answer
192
views
Problem with initiating base with child class while working with python-attrs module
I'm trying to create Model class with attrs module. In my script CollectionModel class is inherited in User class. So logically all the attributes in the CollectionModel should be available in the ...