All Questions
Tagged with python-attrs python
139
questions
0
votes
1
answer
72
views
How to implement class composition
Hi I have a model that contain several items, now all of the items have a set of shared properties: id, name, parent. In addition some of this items are meant only to contain other items. Some are ...
0
votes
0
answers
18
views
Pylint warnings with attrs
I keep getting warnings from pylint while trying to iterate over an attribute in a class that uses the attrs package.
from typing import List
from attrs import define, Factory
@define
class MyClass:
...
0
votes
0
answers
21
views
Is it possible to utilize a lambda function in an python-attrs converter? [duplicate]
I am attempting to automatically create converter functions that require metadata to operate. I am seeing odd behavior when a lambda function is used for the converter.
import attrs
def add(x, y=10):
...
1
vote
1
answer
60
views
In python Attrs package, how to add a field in the field_transformer function?
The documentation for the python Attrs, the Automatic Field Transformation and Modification section states
...You can add converters, change types, and even remove attributes
completely or create new ...
0
votes
1
answer
329
views
Pyright cause reportAttributeAccessIssue on attrs validator
Environment
Python v3.9.13
Pyright 1.1.357
attrs 23.1.0
The code at official document of attrs library make the pyright type checker casue reportAttributeAccessIssue
https://www.attrs.org/en/stable/...
0
votes
0
answers
22
views
Interlinked validators using python attrs
I want to use attrs to run validators on values inside my class. Here is an example:
from attrs import define, field
@define(kw_only=True)
class Foo:
x: float = field()
min_x: float = field()
...
0
votes
1
answer
61
views
Set defaults for attrs subclass that uses validators from parent class?
Consider this minimal example:
from attrs import define, field, validators
@define(kw_only=True)
class Vehicle:
num_wheels: int = field(validator=validators.instance_of(int))
@num_wheels....
0
votes
1
answer
42
views
Unexpected `FrozenInstanceError` in unit test when using a frozen exception
I have a unit test that I want to show failing as part of a bug report. It's important not only that the test fail with an error, but that the error message very clearly evidences the underlying bug. ...
0
votes
1
answer
144
views
How do you get cattrs to unstructure nested structures?
In Python, with the libraries attrs and cattrs, I have a nested structure defined as follows:
@attrs.define
class Score:
added: datetime
value: float
@attrs.define
class Entry:
score: ...
1
vote
0
answers
56
views
How do I set parameters of objects saved in a list class without looping?
I have 1 main class and one class, that functions as a storage list for every object created, so I can keep track of every newly created user.
But how can I access the parameters of a user in that ...
1
vote
1
answer
216
views
mypy doesn't recognize the latest version of attrs?
Is some special setup needed to help mypy recognize standard attrs usage?
somefile.py:7: error: Cannot find implementation or library stub for module named "attr" [import-not-found]
(...
2
votes
2
answers
105
views
How to nest a list of attrs classes into an attrs class
I have a list of dicts and I'd like to use python-attrs to convert them into classes.
Here's the sample data:
[[characters]]
first_name = 'Duffy'
last_name = 'Duck'
[[characters]]
first_name = 'Bugs'
...
0
votes
1
answer
129
views
Using class methods to return instances and simply defining __init__(self, **kwargs):
One pattern I recently started using is writing class methods to return instances. In particular, I've been using it for dataclasses (with or without the @dataclass decorator). But it has also led me ...
0
votes
1
answer
52
views
Is there a way to access a class field as a property in python?
I've been trying to craft a small "mongodb orm" on top of pymongo with attrs. For this I've found that there's not a way for me to keep a consistent structure through the whole app since ...
2
votes
2
answers
256
views
How should I pass a class as an attribute to another class with attrs?
So, I just stumbled upon a hurdle concerning the use of attrs, which is quite new to me (I guess this also applies to dataclasses?). I have two classes, one I want to use as an attribute for another. ...