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
32
votes
2 answers

Dynamically add fields to dataclass objects

I'm writing a library to access REST API. It returns json with user object. I convert it to dict, and then convert it to dataclass object. The problem is that not all fields are fixed. I want to add additional fields (which are not specified in my…
rominf
  • 2,719
  • 3
  • 21
  • 39
32
votes
5 answers

Replace attributes in Data Class objects

I'd like to replace the attributes of a dataclass instance, analogous to namedtuple._replace(), i.e. making an altered copy of the original object: from dataclasses import dataclass from collections import namedtuple U = namedtuple("U",…
BayerSe
  • 1,101
  • 2
  • 12
  • 23
31
votes
2 answers

Check if a class is a dataclass in Python

How can I check if a class is a dataclass in Python? I found out that I can check the existance of __dataclass_fields__ and __dataclass_params__ attributes, but I'd love to find a more elegant way to do this. I'd expect using something like…
Or B
  • 1,675
  • 5
  • 20
  • 41
31
votes
1 answer

How do I define a datetime field in a python dataclass?

trying to get the syntax of the Python 3.7 new dataclass right. if I want to include a datetime value in my dataclass, import datetime from dataclasses import dataclass @dataclass class MyExampleWithDateTime: mystring: str myint: int …
576i
  • 7,579
  • 12
  • 55
  • 92
29
votes
7 answers

How to use enum value in asdict function from dataclasses module

I have a dataclass with a field template of type Enum. When using the asdict function it converts my dataclass to a dictionary. Is it possible to use the value attribute of FoobarEnum to return the string value instead of the Enum object? My initial…
Nepo Znat
  • 3,000
  • 5
  • 28
  • 49
29
votes
4 answers

How to add a dataclass field without annotating the type?

When there is a field in a dataclass for which the type can be anything, how can you omit the annotation? @dataclass class Favs: fav_number: int = 80085 fav_duck = object() fav_word: str = 'potato' It seems the code above doesn't…
wim
  • 338,267
  • 99
  • 616
  • 750
28
votes
1 answer

Python Class "Constants" in Dataclasses

Understanding that the below are not true constants, attempting to follow PEP 8 I'd like to make a "constant" in my @dataclass in Python 3.7. @dataclass class MyClass: data: DataFrame SEED = 8675309 # Jenny's Constant My code used to…
rhaskett
  • 1,864
  • 3
  • 29
  • 48
27
votes
3 answers

Python dataclass, what's a pythonic way to validate initialization arguments?

What's a pythonic way to validate the init arguments before instantiation w/o overriding dataclasses built-in init? I thought perhaps leveraging the __new__ dunder-method would be appropriate? from dataclasses import dataclass @dataclass class…
tgk
  • 3,857
  • 2
  • 27
  • 42
26
votes
5 answers

Initializing a pydantic dataclass from json

I'm in the process of converting existing dataclasses in my project to pydantic-dataclasses, I'm using these dataclasses to represent models I need to both encode-to and parse-from json. Here's an example of my current approach that is not good…
hagaiw
  • 301
  • 1
  • 3
  • 6
26
votes
2 answers

Python 3.7: Utility of Dataclasses and SimpleNameSpace

Python 3.7 provides new dataclasses which have predefined special functions. From an overview point, dataclasses and SimpleNamespace both provide nice data encapsulating facility. @dataclass class MyData: name:str age: int data_1 =…
xssChauhan
  • 2,728
  • 2
  • 25
  • 36
24
votes
4 answers

"pydantic\validators.py" : no validator found for

Below DataFrame of pandas is not validated by pydantic. How to handle this? from pydantic.dataclasses import dataclass @dataclass class DataFrames: dataframe1: pd.DataFrame = None dataframe2: pd.DataFrame = None This throws the following…
23
votes
1 answer

Create recursive dataclass with self-referential type hints

I want to write a dataclass definition in Python, but can't refer to that same class inside the declaration. Mainly what I want to achieve is the typing of this nested structure, as illustrated below: @dataclass class Category: title: str …
23
votes
5 answers

Pretty-print dataclasses prettier with line breaks and indentation

Python Data Classes instances also include a string representation method, but its result isn't really sufficient for pretty printing purposes when classes have more than a few fields and/or longer field values. Basically I'm looking for a way to…
maxschlepzig
  • 35,645
  • 14
  • 145
  • 182
23
votes
4 answers

control initialize order when Python dataclass inheriting a class

What I kown The Python dataclass allows inheritance, either with dataclass or class. In best practice (and also in other languages), when we do inheritance, the initialization should be called first. In Python it is: def __init__(self): …
WeiChing 林煒清
  • 4,452
  • 3
  • 30
  • 65
23
votes
3 answers

How to define circularly dependent data classes in Python 3.7+?

Suppose that class A has a member whose type is class B, and class B has a member whose type is class A. In Scala or Kotlin, you can define the classes in any order without worries in this case because the firstly-defined class can use the…
Naetmul
  • 14,544
  • 8
  • 57
  • 81
1 2
3
59 60