Questions tagged [python-attrs]
Use for questions about the third-party Python library for data classes
python-attrs
160
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
86
views
Pylance doesn't recognize attrs.field with converters
Pylance seems to recognize an attrs field as a function (rather than a variable) when the paramter converter= is specified. Am I doing right and how to correct it? (Python v3.8.13, Pylance
v2023.12....
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. ...
0
votes
0
answers
114
views
Using attrs decorators in PyCharm
I have the following attrs class:
from attrs import define, field
@define
class Foo:
bar: int = field()
@bar.validator
def check(self, attribute, value):
...
It works ...
0
votes
1
answer
128
views
How do you type hint a protocol with attrs.field?
I want to use attrs to use its ability evolve my class, hashing and more. But I would also like to use the protocol pattern. However since attrs does not do well with multiple inheritance, I am having ...
0
votes
0
answers
58
views
python redefining `attrs.field` to accept default argument
How do you redefine the attrs.field function (and type hint it) to accept a default argument? This is my current code.
from attrs import define, field
def fixed_attr(default, **kwargs):
return ...
1
vote
2
answers
367
views
How to mypy typehint an attrs validator attribute
Given the following:
from pathlib import Path
import attr
@attr.define(frozen=False)
class ExampleAtt:
x: str = attr.field()
@x.validator
def check(self, attribute: attr.Attribute, ...
1
vote
2
answers
440
views
Instantiate attrs class from dict with superfluous key/value pairs
I decorated a class using attrs.define and added all important attributes/fields.
import attrs
@attrs.define
class MyAttrsClass:
important1 = attrs.field(type = int)
important2 = attrs.field(...
4
votes
1
answer
769
views
SQLAlchemy typing support on filter for imperatively mapped attrs classes
For my application I am trying to separate my "domain layer" classes from the database backend, in order to be able to unit test these classes independently of the database. I use mypy for ...
2
votes
2
answers
457
views
How to type dynamically created classes so mypy can lint them properly
I'm looking to refactor the function task decorator of a dataflow engine I contribute to called Pydra so that the argument types can be linted with mypy. The code captures the arguments to be passed ...
1
vote
1
answer
471
views
Python attrs nested objects convertes
I'm using attrs lib to parse my configuration file, and I'm looking for a better way to parse nested data objects.
Example :
from attrs import define, field, validators
from typing import Dict
class ...
1
vote
1
answer
464
views
Using attrs is it ok to set init=False to an attribute with no default value
I use attrs library.
I use some attributes that are set by the __attrs_post_init__ method.
For them, I want to prevent them from being part of the constructor.
Is it ok to not put a default value, or ...
1
vote
2
answers
88
views
'NoneType' object has no attribute 'attrs' error in python
I am trying to scarp the the site: https://stackoverflow.com/questions/tagged/docusignapi to get vote_count, answers and views. However I am getting None values for few questions and not sure how to ...
0
votes
1
answer
230
views
Annotating function argument accepting an instance of a class decorated with @define
I'm using pythons attrs package to @define some classes containing some members with little logic for isolation purposes.
However, when I run the following stripped down example:
from attrs import ...
2
votes
1
answer
579
views
attrs - how to validate an instance of a Literal or None
This is what I have. I believe there are two problems here - the Literal and the None.
from attrs import frozen, field
from attrs.validators import instance_of
OK_ARGS = ['a', 'b']
@field
class ...
1
vote
1
answer
52
views
Inherited has no attribute X after using `attr`
The following snippet runs fine:
class base:
@classmethod
def func(cls):
cls.params
class inherited(base):
params = 2
def my_func(self):
inherited.func()
obj = ...
0
votes
1
answer
93
views
__init__() takes from 1 to 2 positional arguments but 4 were given after refactorizing class to be under base class
I have a class that currently looks something like this:
import attr
@attr.s
class my_class(object):
var1 = attr.ib(default=5)
var2 = attr.ib(default=5)
var3 = attr.ib(default=5)
@classmethod
...