Skip to main content

Questions tagged [python-attrs]

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

python-attrs
0 votes
1 answer
67 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 ...
Al_'s user avatar
  • 1
0 votes
0 answers
17 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: ...
Al_'s user avatar
  • 1
0 votes
0 answers
19 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): ...
slaughter98's user avatar
  • 1,853
1 vote
1 answer
54 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 ...
slaughter98's user avatar
  • 1,853
0 votes
1 answer
289 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/...
usan's user avatar
  • 103
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() ...
Mike's user avatar
  • 155
0 votes
1 answer
56 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....
Mike's user avatar
  • 155
0 votes
1 answer
39 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. ...
ApproachingDarknessFish's user avatar
0 votes
1 answer
132 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: ...
Matt's user avatar
  • 9,938
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 ...
Kiri's user avatar
  • 9
1 vote
1 answer
84 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....
Chitaoji's user avatar
1 vote
1 answer
210 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] (...
Y123's user avatar
  • 967
2 votes
2 answers
103 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' ...
brillenheini's user avatar
0 votes
1 answer
123 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 ...
shridhar singh's user avatar
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 ...
Gabe's user avatar
  • 42

15 30 50 per page
1
2 3 4 5
11