python-dataclasses

Python: Dataclass that inherits from base Dataclass, how do I upgrade a value from base to the new class?

馋奶兔 提交于 2021-02-20 06:08:05
问题 How can I upgrade values from a base dataclass to one that inherits from it? Example (Python 3.7.2) from dataclasses import dataclass @dataclass class Person: name: str smell: str = "good" @dataclass class Friend(Person): # ... more fields def say_hi(self): print(f'Hi {self.name}') friend = Friend(name='Alex') f1.say_hi() prints "Hi Alex" random_stranger = Person(name = 'Bob', smell='OK') return for random_stranger "Person(name='Bob', smell='OK')" How do I turn the random_stranger into a

How can I set an attribute in a frozen dataclass custom __init__ method?

旧街凉风 提交于 2021-01-02 06:40:57
问题 I'm trying to build a @dataclass that defines a schema but is not actually instantiated with the given members. (Basically, I'm hijacking the convenient @dataclass syntax for other purposes). This almost does what I want: @dataclass(frozen=True, init=False) class Tricky: thing1: int thing2: str def __init__(self, thing3): self.thing3 = thing3 But I get a FrozenInstanceError in the __init__ method: dataclasses.FrozenInstanceError: cannot assign to field 'thing3' I need the frozen=True (for

How can I make a python dataclass hashable?

与世无争的帅哥 提交于 2020-07-05 05:26:12
问题 Say a I have a dataclass in python3. I want to be able to hash and order these objects. I only want them ordered/hashed on id. I see in the docs that I can just implement __hash__ and all that but I'd like to get datacalsses to do the work for me because they are intended to handle this. from dataclasses import dataclass, field @dataclass(eq=True, order=True) class Category: id: str = field(compare=True) name: str = field(default="set this in post_init", compare=False) a = sorted(list(set([

How to use spec when mocking data classes in Python

独自空忆成欢 提交于 2020-06-28 04:50:11
问题 I'm trying to port our namedtuple classes into dataclass in Python 3.6 using the backport package. However, I noticed when mocking dataclass classes, you cannot use the "spec" keyword anymore. I assume it's because the dataclass code is auto generated. from dataclasses import dataclass import mock @dataclass class A: aaa: str bbb: int m = mock.Mock(spec=A) m.aaa And this is the error I get: AttributeError: Mock object has no attribute 'aaa' Any idea if there's any way to automatically set all

Python Dataclass and property decorator

浪子不回头ぞ 提交于 2020-06-07 04:59:26
问题 I've been reading up on Python 3.7's dataclass as an alternative to namedtuples (what I typically use when having to group data in a structure). I was wondering if dataclass is compatible with the property decorator to define getter and setter functions for the data elements of the dataclass. If so, is this described somewhere? Or are there examples available? 回答1: It sure does work: from dataclasses import dataclass @dataclass class Test: _name: str="schbell" @property def name(self) -> str:

Python Dataclass and property decorator

霸气de小男生 提交于 2020-06-07 04:59:06
问题 I've been reading up on Python 3.7's dataclass as an alternative to namedtuples (what I typically use when having to group data in a structure). I was wondering if dataclass is compatible with the property decorator to define getter and setter functions for the data elements of the dataclass. If so, is this described somewhere? Or are there examples available? 回答1: It sure does work: from dataclasses import dataclass @dataclass class Test: _name: str="schbell" @property def name(self) -> str: