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

Can I provide defaults for a subclass of a dataclass?

I want to create a class hierarchy, where all objects of class Base has a field field1, but different subclasses have different default values for field1. The classes are data holders, so dataclasses seems like the perfect library to use. Consider…
LudvigH
  • 3,662
  • 5
  • 31
  • 49
6
votes
1 answer

How to annotate the type of field in dataclass to be different from the type of its __init__?

I created a dataclass Foo, which accepts any type that can be converted to int: import dataclasses @dataclasses.dataclass class Foo: a: int def __post_init__(self): # Here `self.a` is converted to int, so this class accepts any…
tamuhey
  • 2,904
  • 3
  • 21
  • 50
6
votes
2 answers

Dataclass argument choices with a default option

I'm making a dataclass with a field for which I'd like there to only be a few possible values. I was thinking something like this: @dataclass class Person: name: str = field(default='Eric', choices=['Eric', 'John', 'Graham', 'Terry']) I know…
idle
  • 73
  • 1
  • 4
6
votes
1 answer

Python dataclass generate hash and exclude unsafe fields

I have this dataclass: from dataclasses import dataclass, field from typing import List @dataclass class Person: name: str dob: str friends: List['Person'] = field(default_factory=list, init=False) name and dob are immutable and…
Mike Pham
  • 437
  • 6
  • 17
6
votes
1 answer

How to use the __post_init__ method in Dataclasses in Python

I am trying to get my hands dirty with dataclasses in Python and what i want to do is have a computed field inside my class and also add the sort_index field to the call but would also want to make it frozen so that i cannot modify any attributes of…
Subhayan Bhattacharya
  • 5,407
  • 7
  • 42
  • 60
6
votes
2 answers

Dataclasses: Require at least one value to be set in grouping of model fields

How can one require at least one field in a group of fields on a dataclass to be set to a truthy value? Does this require a custom root validator method as it requires looking at many fields at once? For example, consider the following…
Joe Bane
  • 1,556
  • 1
  • 15
  • 30
6
votes
1 answer

Python dataclass: can you set a default default for fields?

I want to make a dataclass base class where all the fields in subclasses are automatically Optional and default to None (if no default is provided). The following code... almost seems to do what I want, but not quite. It errors out the same way as…
user3534080
  • 1,201
  • 1
  • 14
  • 21
6
votes
4 answers

Dataclass-style object with mutable and immutable properties?

I have been playing around with dataclasses dynamically loaded with property names from a file and I am unable to find a way to create both 'frozen' and 'non-frozen' properties. I believe dataclasses only allow you to set all properites to frozen or…
JMB
  • 63
  • 1
  • 4
6
votes
1 answer

Dataclass nested within another dataclass does not update data correctly

I generate two different instances of a python dataclass which includes a nested dataclass. When I update a value in the nested dataclass in one instance (but not in the other), the same data is placed in the nested dataclass in both instances. …
Den
  • 197
  • 1
  • 10
6
votes
1 answer

I can not name the field "from"

I write a script for myself on python, I use the dataclass library, I have a ready-made DTO written in Java, which I try to repeat on the python, there is a field called "from", what can I call this field in python, because I can't call him the same…
6
votes
1 answer

How to evolve a dataclass in python?

I'm interested to use dataclass as the syntax is shorter than attr. However I can't find a shortcut that provides the API to evolve it, using e.g. the following code: @dataclass class AB(object): a: int = 1 b: int =…
tribbloid
  • 4,026
  • 14
  • 64
  • 103
6
votes
1 answer

How to make an unfrozen dataclass instance hashable?

When defining a dataclass with frozen=False (default behaviour), and then instantiating an object of this class, is there a way to make this object hashable? Why do I need this? Before Python 3.7, I used named tuples instead of dataclasses and I…
David Dahan
  • 10,576
  • 11
  • 64
  • 137
6
votes
1 answer

Avoid Python lint warning for @dataclass variable declarations

I am having a try of the dataclasses feature in Python 3.7, but get this warning below word 'hue': 'hue' used before definition Python (use-before-def) I suppose it is a linter warrning. I tried several linters that python extension provided, but…
curtank
  • 600
  • 2
  • 7
  • 18
6
votes
1 answer

Is there a way to use a dataclass, with fields with defaults, with __slots__

I would like to put __slots__ on a dataclass with fields with defaults. When I try do that, I get this error: >>> @dataclass ... class C: ... __slots__ = ('x', 'y', ) ... x: int ... y: int = 1 ... Traceback (most recent call…
Gary van der Merwe
  • 9,134
  • 3
  • 49
  • 80
6
votes
4 answers

Forbid the usage of the default constructor from outside the class

Consider the following dataclass. I would like to prevent objects from being created using the __init__ method direclty. from __future__ import annotations from dataclasses import dataclass, field @dataclass class C: a: int @classmethod …
abc
  • 11,579
  • 2
  • 26
  • 51