Skip to main content

Questions tagged [python-attrs]

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

python-attrs
1 vote
1 answer
758 views

cattrs.structure with nested dictionaries and attrs.define( converter=foo )

I am having issues structuring dicts, which contain invalid data, into @attrs.define decorated classes, but should be handled with attrs.define converters. I have managed to get it to work with Foo( **...
user19007114's user avatar
0 votes
2 answers
466 views

python attrs inherited field value gets overwritten

I have some attrs classes that inherit from a base class. The parent class has a field that is also an attrs class. If I instantiate two instances of the child classes and set the common inherited ...
waszil's user avatar
  • 460
0 votes
1 answer
392 views

How to customize attrs field hash

I'd like to use a Numpy array as a field value while keeping my attrs class hashable. For that purpose, I found joblib's hash() function to be a good means of hashing Numpy arrays. Is there any ...
M4urice's user avatar
  • 621
0 votes
1 answer
832 views

How to annotate attrs field with validator?

I am having trouble annotating attrs class attribute. I am using NewType for defining new UserId type and attrs frozen classes. This is code where mypy doesn't complain and everything is alright: from ...
devKanapka's user avatar
0 votes
1 answer
734 views

Using cattrs / attrs where attr name does not match keys to create an object

I am looking at moving to cattrs / attrs from a completely manual process of typing out all my classes but need some help understanding how to achieve the following. This is a single example but the ...
SimonT's user avatar
  • 1,019
4 votes
1 answer
2k views

Is there an easy way to construct a pandas DataFrame from an Iterable of dataclass or attrs objects?

One can do that with dataclasses like so: from dataclasses import dataclass import pandas as pd @dataclass class MyDataClass: i: int s: str df = pd.DataFrame([MyDataClass("a", 1), ...
VaNa's user avatar
  • 361
0 votes
0 answers
32 views

Different objects are preserving the state using attrs

I'm making some experiments using attrs, but I'm experiencing some unexpected behaviors in the following code: I created a class as this one: @define class User: name = field() errors = field(...
Miguel Andrade's user avatar
1 vote
1 answer
531 views

Serialising with cattrs and want to omit field x1 string field

Using cattrs to structure data and I want to omit x1 string field. I want to perform a trivial cleanup on strings that have been passed in except for the password field. I can get it to work on all ...
Pierre Brasseau's user avatar
1 vote
0 answers
275 views

Add caching to __new__

I have a frozen class (decorated with attrs.frozen). However, the program might have to create many instances of that class. Since they are immutable and I only want to read from them, I want some ...
hamnghi's user avatar
  • 99
1 vote
1 answer
159 views

What happens exactly when a python class instance use dot to access attributes?

Suppose a class A is defined as: class A: def __init__(self, val): self.val = val After A is instantiated by a = A(3), what methods will be called by executing a.val? In other words, what ...
Orange Chen's user avatar
4 votes
2 answers
491 views

attrs convert list[str] to list[float]

Given the following scenario: import attrs @attrs.define(kw_only=True) class A: values: list[float] = attrs.field(converter=float) A(values=["1.1", "2.2", "3.3"]) ...
Elia's user avatar
  • 802
0 votes
0 answers
245 views

Type annotation in python-attrs

I'm going to use attrs for a class within my ETL process and currently I struggle with its type annotation capabilities. For instance, I have this code: @define(auto_attribs=True, kw_only=True) class ...
brillenheini's user avatar
0 votes
2 answers
544 views

How to override an attrs class instance with another one when the values are not None

My goal is to be able to merge multiple instances of the same attrs class and skip 'None' values. This allows me to create default values and later override them when I need to. An example of the ...
Guy's user avatar
  • 355
-1 votes
2 answers
54 views

Define custom atrributes in attr module

I'm looking for way, that I can define class attributes as I want. For example, I tried this way: @attrs.define class MyClass: pass MyClass(a=1, b=2) But recieving the TypeError: TypeError: ...
Чётк��я ворона's user avatar
0 votes
0 answers
321 views

Exclude all attrs auto-generated methods from sphinx

I have a simple attrs data class: from attrs import frozen @frozen class Person raw: str I document it with the simplest *.rst file: .. autoclass:: api.main.person.Person :members: :...
OrenIshShalom's user avatar

15 30 50 per page
1 2
3
4 5
11