Questions tagged [python-dataclasses]

For questions concerning the Python dataclasses module (new in Python 3.7). Dataclasses are python classes but are specifically suited for storing data objects.

Dataclasses are python classes but are specifically suited for storing data objects(store data and represent a certain data type).

Official documentation: dataclasses

Original proposal: PEP 557

Tutorial / showcase: Raymond Hettinger's talk on PyCon 2018

899 questions
8
votes
1 answer

Using dataclasses with Cython

I'm using cython for code obfuscation, so performance is not an issue at the moment. The problem is with using dataclasses. There is no error during compilation time when cythonize code that contains dataclass definitions. But when running the…
Andrey Kite Gorin
  • 1,030
  • 1
  • 9
  • 23
8
votes
1 answer

New annotations break inspection of dataclasses

With PEP 563, from __future__ import annotations changes type annotations so that they are evaluated lazily, which provides a bunch of benefits like forward references. However, this seems to play badly with other features, like dataclasses. For…
drhagen
  • 8,331
  • 8
  • 53
  • 82
7
votes
2 answers

Python dataclass, one attribute referencing other

@dataclass class Stock: symbol: str price: float = get_price(symbol) Can a dataclass attribute access to the other one? In the above example, one can create a Stock by providing a symbol and the price. If price is not provided, it defaults…
Mukesh C
  • 93
  • 7
7
votes
1 answer

Exclude default fields from python `dataclass` `__repr__`

Summary I have a dataclass with 10+ fields. print()ing them buries interesting context in a wall of defaults - let's make them friendlier by not needlessly repeating those. Dataclasses in Python Python's @dataclasses.dataclass() (PEP 557) provides…
tony
  • 870
  • 7
  • 16
7
votes
2 answers

Using __new__ in inherited dataclasses

Suppose I have the following code that is used to handle links between individuals and countries: from dataclasses import dataclass @dataclass class Country: iso2 : str iso3 : str name : str countries = [ Country('AW','ABW','Aruba'), …
EdG
  • 137
  • 1
  • 10
7
votes
1 answer

Python Singleton Dataclass

I have a class like this: from __future__ import annotations import os from dataclasses import dataclass @dataclass(frozen=True) class Config: name: str age: str @staticmethod def init() -> Config: return Config( …
sixtyfootersdude
  • 25,859
  • 43
  • 145
  • 213
7
votes
2 answers

Extend dataclass' __repr__ programmatically

Suppose I have a dataclass with a set method. How do I extend the repr method so that it also updates whenever the set method is called: from dataclasses import dataclass @dataclass class State: A: int = 1 B: int = 2 def set(self, var,…
badbayesian
  • 117
  • 1
  • 8
7
votes
0 answers

Python type annotation slows down the code

I am optimizing my code for performance, and when I use cProfile to check my code, a good deal of runtime is due to type annotation! Removing type annotation indeed improves the performance. You can see the output of cProfiler for the annotated and…
7
votes
1 answer

How to auto generate docstrings in PyCharm for dataclasses?

Is there a way for auto generating docstrings for dataclasses in the same fashion of the method and function docstrings? I did not find anything useful through help / search from dataclasses import dataclass @dataclass class ExtractionConfig: …
user12690225
7
votes
4 answers

Using class or static method as default_factory in dataclasses

I want to populate an attribute of a dataclass using the default_factory method. However, since the factory method is only meaningful in the context of this specific class, I want to keep it inside the class (e.g. as a static or class method). For…
Kurt Bourbaki
  • 11,984
  • 6
  • 35
  • 53
7
votes
2 answers

How to declare python dataclass member field same as the dataclass type

How can I have a member field of a dataclass same as the class name in python3.7+ ? I am trying to define a class like so (which can be done in Java or C++) -- which might be used as a class for LinkedList node @dataclass class Node: val:str …
exifguy
  • 650
  • 7
  • 16
7
votes
2 answers

Python dataclasses: omit field from asdict

I've started making heavy use of the python dataclasses module and find it very useful. I especially like the flags that can be set on each field allowing for toggling of compare, init etc. I often find however that there is a field which I wish to…
kerzane
  • 375
  • 3
  • 8
7
votes
1 answer

Python Dataclasses: Mocking the default factory in a frozen Dataclass

I'm attempting to use freezegun in my unit tests to patch a field in a dataclass that is set to the current date when the object is initialised. I would imagine the question is relevant to any attempt to patch a function being used as a…
Steven
  • 823
  • 8
  • 25
7
votes
2 answers

Python frozen dataclass, allow changing of attribute via method

Suppose I have a dataclass: @dataclass(frozen=True) class Foo: id: str name: str I want this to be immutable (hence the frozen=True), such that foo.id = bar and foo.name = baz fail. But, I want to be able to strip the id, like so: foo =…
alex_halford
  • 369
  • 3
  • 10
7
votes
1 answer

Error: unhashable type: 'dict' with @dataclass

I have a class Table: @dataclass(frozen=True, eq=True) class Table: name: str signature: Dict[str, Type[DBType]] prinmary_key: str foreign_keys: Dict[str, Type[ForeignKey]] indexed: List[str] And need to create such…
Pavel Antspovich
  • 1,111
  • 1
  • 11
  • 27