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: When I am creating a new class, should I set all instance attributes in __init__, even if they are None and in fact later assigned values in class methods? See example below for the attribute results of MyClass: class MyClass: def __init__(self,df): self.df = df self.results …

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