python-attrs

attrs – how to validate an instance of a Literal or None

attrs – how to validate an instance of a Literal or None Question: 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 MyClass: my_field: Literal[OK_ARGS] | None = field(validator=instance_of((Literal[OK_ARGS], None))) Error: …

Total answers: 1

frozen dataclass in def __init__ and iteration on it

frozen dataclass in def __init__ and iteration on it Question: I want to use a frozen class as a structure as I don’t want to use any mutable objects in my code. But also I need to iterate on my_data. How can I make this work? SideNote: dict is not an option from dataclasses import …

Total answers: 1

Setting default/empty attributes for user classes in __init__

Setting default/empty attributes for user classes in __init__ Question: I have a decent level of programming, and get much value from the community here. However I have never had much academic teaching in programming nor worked next to really experienced programmers. Consequently I sometimes struggle with ‘best practice’. Question: When I am creating a new …

Total answers: 4

Using attrs to turn JSONs into Python classes

Using attrs to turn JSONs into Python classes Question: 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.nested_attribute). My JSONs have a fixed schema, and I would be fine with having to …

Total answers: 1