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
23
votes
1 answer

Is it possible to use *args in a dataclass?

I recently started using dataclasses and they will be a nice addition to 3.7. I'm curious if or how it is possible to recreate the same functionality of this class using dataclasses. class Nav(object): def __init__(self, name:str, menu, page,…
Marcel Wilson
  • 3,842
  • 1
  • 26
  • 55
21
votes
8 answers

python3 dataclass with **kwargs(asterisk)

Currently I used DTO(Data Transfer Object) like this. class Test1: def __init__(self, user_id: int = None, body: str = None): self.user_id = user_id self.body = body Example code is very small, But when object…
Hide
  • 3,199
  • 7
  • 41
  • 83
21
votes
5 answers

Validating input when mutating a dataclass

In Python 3.7 there are these new "dataclass" containers that are basically like mutable namedtuples. Suppose I make a dataclass that is meant to represent a person. I can add input validation via the __post_init__() function like…
dain
  • 672
  • 1
  • 7
  • 22
21
votes
2 answers

Python 3.7: dataclass does not raise `TypeError` for `eq=False`

I was trying out the new dataclasses in Python 3.7 The dataclass decorator can be passed arguments to control the dunder functions that are added to the class. For some reason, the decorator does not seem to raise TypeError for eq=False argument. As…
xssChauhan
  • 2,728
  • 2
  • 25
  • 36
20
votes
1 answer

Class Attribute and metaclass in dataclasses

I have some Python classes with class attributes and metaclass: from abc import ABCMeta class OldProduct(metaclass=ABCMeta): c_type: str c_brand: str def __init__(self, name:str): self.name = name class…
Jean-Francois T.
  • 11,549
  • 7
  • 68
  • 107
18
votes
7 answers

How to make attribute in dataclass read-only?

Let's say I have a class like this: class C: def __init__(self, stuff: int): self._stuff = stuff @property def stuff(self) -> int: return self._stuff then stuff is read-only: c = C(stuff=10) print(c.stuff) # prints…
Cleb
  • 25,102
  • 20
  • 116
  • 151
17
votes
1 answer

Trouble creating DefaultDict in Dataclass

I am having trouble setting up a simple dataclass using a new defaultdict(dict). If I tell the factory to use 'dict' as below , instantiation fails with typerror collection.defaultdict object is not callable from collections import…
Bill
  • 618
  • 6
  • 15
17
votes
2 answers

Can I have a simple list of a dataclass field

Can I have easily a list of field from a dataclass ? @dataclass class C: x: int y: int z: int t: int expected result: [x,y,z,t]
Sylvain Page
  • 581
  • 1
  • 7
  • 21
17
votes
3 answers

Python 3 - Which one is faster for accessing data: dataclasses or dictionaries?

Python 3.7 introduced dataclasses to store data. I'm considering to move to this new approach which is more organized and well structured than a dict. But I have a doubt. Python transforms keys into hashes on dicts and that makes looking for keys…
sergiomafra
  • 1,174
  • 2
  • 13
  • 21
17
votes
4 answers

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

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): # ...…
576i
  • 7,579
  • 12
  • 55
  • 92
17
votes
1 answer

Dataclass subclass does not inherit __repr__

I am using Python dataclasses module backported from Python 3.7. I stumbled upon this behavior where a dataclass subclass does not inherit __repr__: from dataclasses import dataclass @dataclass class Person: name: str def __repr__(self): …
Dmitry Figol
  • 353
  • 2
  • 7
16
votes
3 answers

How to get an abstract dataclass to pass mypy?

mypy v0.910 rejects abstract dataclasses in Python 3.9. Here's the Minimal Reproducible Example: from abc import ABC, abstractmethod from dataclasses import dataclass @dataclass class Liquid(ABC): @abstractmethod def drip(self) -> None: …
16
votes
1 answer

In Python dataclasses, why can an InitVar have default but not a default_factory?

In Python 3.7, I can create a dataclass with a defaulted InitVar just fine: from dataclasses import dataclass, InitVar, field @dataclass class Foo: seed: InitVar[str] = field(default='tomato') stored: str = field(init=False) def…
Jacobo de Vera
  • 1,863
  • 1
  • 16
  • 20
16
votes
1 answer

How to use dataclasses to generate a field value?

I have the following class: class WordItem: def __init__(self, phrase: str, word_type: WORD_TYPE): self.id = f'{phrase}_{word_type.name.lower()}' self.phrase = phrase self.word_type = word_type @classmethod def…
indigo153
  • 1,021
  • 2
  • 10
  • 23
15
votes
3 answers

What is the recommended way to include properties in dataclasses in asdict or serialization?

Note this is similar to How to get @property methods in asdict?. I have a (frozen) nested data structure like the following. A few properties that are (purely) dependent on the fields are defined. import copy import dataclasses import json from…