Questions tagged [python-attrs]

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

attrs is a Python package that greatly simplifies the creation of classes by taking care of boilerplate for you, such as __init__, __repr__, comparison methods, and more.

Resources:

144 questions
4
votes
2 answers

Decorator for attrs converter

Is there a decorator for using attrs converters? There are decorators for validator and default, but I couldn't see anything mentioned for converter. Is it possible? I prefer to have the function as a "method" within the class, as opposed to global…
BrendanSimon
  • 665
  • 1
  • 9
  • 23
4
votes
2 answers

in a python attrs class, how can I override generated __init__ with my own

So I like to use attr but sometimes I need to do my own thing. can I override the __init__ method with my own? import attr @attr.s(auto_attribs=True) class MyClass: i: int def __init__(self, i, special=None): if special: …
polo
  • 1,352
  • 2
  • 16
  • 35
4
votes
1 answer

Python attrs class attribute cached lazy load

I have classes that look like this: @attr.s class ImageMagic(object): path = attr.ib() _img = attr.ib() @_img.default def _img(self): return Image.open(self.path) @attr.s class FileObject(object): # Standard path…
Bojan Jovanovic
  • 1,439
  • 2
  • 19
  • 26
4
votes
2 answers

Dynamically define attributes that depend on earlier defined attributes

I want an object that represents a path root and an arbitrary number of subdirectories that are constructed with os.path.join(root). I want to access these paths with the form self.root, self.path_a, self.path_b, etc... In addition to accessing…
truthling
  • 134
  • 10
4
votes
1 answer

python-attrs: validator in child class

Using the Python module attrs, I'm trying to have a subclass with a condition on its attributes more restrictive than its parent, like in the minimal example below. import attr @attr.s class Base: x = attr.ib() y = attr.ib() @attr.s class…
Matthieu
  • 81
  • 4
3
votes
2 answers

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"]) which results in *** TypeError: float() argument must be a string or a real…
Elia
  • 762
  • 1
  • 12
  • 28
3
votes
1 answer

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("Only odd values allowed") @define class Foo: …
Jean-Paul Calderone
  • 47,755
  • 6
  • 94
  • 122
3
votes
1 answer

How to solve "unexpected keyword argument" with python-attrs construct, so that mypy is happy?

I have a class A in a foreign library. class A: def __init__(self, a: int): self.a = a I would like to extend a class B with A like: import attr @attr.s class B(A): b: int = attr.ib() The code seems to work: import attr class A: …
Nico W.
  • 338
  • 3
  • 12
3
votes
1 answer

Mypy returns Error "Unexpected keyword argument" for subclass of a decorated class with attrs package

I have two decorated classes using attrs package as follows: @attr.s(kw_only=True) class Entity: """ base class of all entities """ entity_id = attr.ib(type=str) # ... @attr.s(kw_only=True) class Customer(Entity): …
omid
  • 702
  • 1
  • 11
  • 21
3
votes
2 answers

Type-annotate but skip class attributes in an auto_attribs class

I am currently designing a class structure using attrs. It is a tree-like structure where I need a parent backlink for certain purposes. OTOH, I want to use frozen classes (there are cached properties, which rely on read-only state). This means that…
Torben Klein
  • 2,943
  • 1
  • 19
  • 24
3
votes
1 answer

Unresolved attribute reference issue in PyCharm in doc string

When I'm using attrs library to create class, docstring in PyCharm shows a linting error unresolved attribute reference. On the other hand when I create class normally with __init__ method. It shows no such error. I can't figure out this error…
Akarsh Jain
  • 930
  • 10
  • 15
3
votes
1 answer

mypy complains about TypedDict inside attrs class having Incompatible types

I have a TypedDict DictWithOnlyX inside an attrs data class Example where mypy complains about the type returned from the getdict() method of my class, even though the return type is declared: from typing_extensions import TypedDict from attr import…
abulka
  • 1,316
  • 13
  • 18
3
votes
2 answers

any cattrs solution to serialize attribute use different name?

i was trying to find a solution similar to java Jackson ObjectMapper that can do serialization/deserialization of python object to json. and find that cattrs is closest to what i need. but it cannot do attribute mapping like use firstName in json…
Dyno Fu
  • 8,753
  • 4
  • 39
  • 64
3
votes
2 answers

frozen and hashable dataclass with Iterable field

Now that I have finally dropped support for Python 2, I am migrating from attrs to Python 3 dataclasses and there is one issue I am particularly struggling with. Let's say I have a frozen and hashable class MyClass with one field my_field of type…
Campi
  • 1,932
  • 1
  • 16
  • 21
3
votes
1 answer

How to pickle python-attrs class functions via cloudpickle?

I'm using cloudpickle to pass functions around, and I'd like to pass an attrs class function via cloudpickle. import pickle import attr @attr.s(auto_attribs=True) class myclass: an_int: int a_str: str a_float: float def…
Mr. Buttons
  • 463
  • 1
  • 3
  • 9
1
2
3
9 10