Join us in building a kind, collaborative learning community via our updated Code of Conduct.

Questions tagged [python-attrs]

attrs is a Python package that will bring back the joy of writing classes by relieving you from the drudgery of implementing object protocols (aka dunder methods). Its main goal is to help you to write concise and correct software without slowing down your code.

0
votes
1answer
19 views

Scrape href not working with python

I have copies of this very code that I am trying to do and every time I copy it line by line it isn't working right. I am more than frustrated and can't seem to figure out where it is not working. ...
0
votes
0answers
16 views

Dynamically update abstract method

I'm trying to dynamically create a Callable class which wraps a function that preserves the kwargs using attrs. I'm not using functools.partial because the call method will be more complex. The ...
0
votes
1answer
21 views

Custom validator in python-attrs with extra parameters

I have several classes defined using attrs like this one: from attr import attrs, attrib, validators @attrs class MyClass: name = attrib(]) @name.validator def check_length(self, ...
0
votes
1answer
27 views

Using attr.asdict() filter with a callable

I'm trying to create a method on my class which will return only public properties as a dict. I am struggling with the syntax. This is what I tried unsuccessfully: @attr.s class C: x = attr.ib() ...
0
votes
1answer
66 views

How to specify an __init__ argument that is not a class attribute

With attrs, how can I specify an init argument that is not a class attribute. e.g. a CRC8 object might be passed some bytes or bytearray in the constructor, but I don't want to store that input, just ...
0
votes
0answers
25 views

mypy attrs and generics

I am running into a confusing mypy error when I attempt to place constraints on a TypeVar when it is used in the definition of an attrs class that inherits from Generic. Here is an example class ...
0
votes
1answer
42 views

Python Attrs Trigger Converter while set attribute

While using python-attrs, what is the good way to trigger the converter while set the attribute. EX: @attr.s class A(object): b = attr.ib(converter=str) >>> A(b=1) A(b='1') >>> ...
2
votes
1answer
50 views

Mocking default values of attr.ib()

I have a situation similar to the following @attrs(auto_attribs=True) class ExampleClass: _prop: OtherClass = attrib(init=False, default=OtherClass()) def some_func(self): test_var = ...
7
votes
1answer
133 views

How to specify that an attribute must be a list of (say) integers, not just a list?

Using the attrs libary and Python 3.6, I thought the following would allow me to specify that x and y can only contain integers: import attr @attr.s class C: x : List[int] = attr.ib() # not ...
0
votes
0answers
59 views

Attr: Deserialize deeply nested json?

I have a deeply nested JSON structure like this: json_data = """{ "title: "...", "links": [ { "href": "string", "method": { "method": "string" ...
0
votes
2answers
44 views

Storing passed data in object twice with `attrs` package

I am creating a data provider class that will hold data, perform transformations and make it available to other classes. If the user creates and instance of this class and passes some data at ...
1
vote
1answer
177 views

Using attrs to turn JSONs into Python classes

I was wondering if it possible to use the attrs library to convert nested JSONs to Python class instances so that I can access attributes in that JSON via dot notation (object.attribute....
1
vote
1answer
88 views

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: ...
0
votes
2answers
80 views

Dynamically “objectify” a nested data structure with python-attrs

With an arbitrary dictionary (eventually with nested collections and basic types), what is the best approach to make it a list of nested objects with python-attrs? Some constraints: the dictionary ...
2
votes
0answers
186 views

Using attr with pylint

Using the attrs package seems to cause errors with PyLint: import attr @attr.s(slots=True) class Foo: d = attr.ib(attr.Factory(dict), type=dict) f = Foo() print('bar' in f.d) print(f.d.items())...
1
vote
1answer
240 views

Python - attrs class inheriting from abstract class

I'm curious how attrs can be combined with abstract classes. I Want to define an abstract class, that defines abstract properties which every inheriting class must have. I want use attrs for this ...
0
votes
3answers
45 views

Only show non-default attributes in repr of attr.s class

I'm using attrs to define simple classes without boilerplate code. The decorator automatically generates a __repr__ that shows the values for all attributes. I'd like to only show attributes that do ...
3
votes
1answer
207 views

Python attrs - positional attribute in super class while optional in sub class

I have 2 very similiar classes: A and B: import attr @attr.s class A(object): x = attr.ib() y = attr.ib() @attr.s class B(object): x = attr.ib() z = attr.ib() y = attr.ib(default=None)...
1
vote
2answers
54 views

How to mock an attr.ib validator

I have an attrs class: @attr.s class Example: my_int = attr.ib(validator=attr.validator.instance_of(MyComplexType)) I need to mock this validator.
1
vote
1answer
119 views

Run attribute validator only after __attrs_post_init__ ends

I have: @attr.s class Example: number = attr.ib(validator=attr.validators.instance_of(int), init=False) def __attrs_post_init__(self): self.number = 'string' print('It seems, ...
3
votes
1answer
99 views

What is the difference between super() and explicit super(Cl,self) (with __slots__ and attrs)

I'm using the attrs python package, in combination with inheritance and slots. I want to call the parent class's method from within the derived method. The problem is demonstrated below: import attr ...
1
vote
2answers
179 views

How to achieve the reverse of “attr.asdict(MyObject)” using Python module 'attrs'

In documentation of Python module attrs stated that there is a method to convert attributes’ class into dictionary representation: Example: >>> @attr.s ... class Coordinates(object): ... ...
3
votes
1answer
295 views

When and why should I use attr.Factory?

When and why should I use attr.ib(default=attr.Factory(list)) over attr.ib(default=[])? From the docs I see that a Factory is used to generate a new value, which makes sense if you are using a lambda ...
7
votes
2answers
859 views

Perfect forwarding - in Python

I am a maintainer of a Python project that makes heavy use of inheritance. There's an anti-pattern that has caused us a couple of issues and makes reading difficult, and I am looking for a good way ...
1
vote
3answers
166 views

How To Deduce Or Subtype Named Tuple From Another Named Tuple?

Preface I was wondering how to conceptualize data classes in a pythonic way. Specifically I’m talking about DTO (Data Transfer Object.) I found a good answer in @jeff-oneill question “Using Python ...
0
votes
2answers
113 views

Python attrs library and referencing instance methods

How can I convert the class below to use the attrs library: class MyClass(object): def __init__(self, api, template=None, **kwargs): self.api = api self.param1 = param1 if ...
3
votes
8answers
537 views

Use Python for Creating JSON

I want to use Python for creating JSON. Since I found no library which can help me, I want to know if it's possible to inspect the order of the classes in a Python file? Example # example.py class ...
164
votes
11answers
10k views

How do I avoid the “self.x = x; self.y = y; self.z = z” pattern in __init__?

I see patterns like def __init__(self, x, y, z): ... self.x = x self.y = y self.z = z ... quite frequently, often with a lot more parameters. Is there a good way to avoid this ...