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

Python nested dataclasses ...is this valid?

Background I'm using dataclasses to create a nested data structure, that I use to represent a complex test output. Previously I'd been creating a hierarchy by creating multiple top-level dataclasses and then using composition: from dataclasses…
Richard
  • 3,024
  • 2
  • 17
  • 40
5
votes
1 answer

Is it possible to prevent reading from a frozen python dataclass?

I have a situation where I would like to be able to treat a frozen dataclass instance as always having the latest data. Or in other words, I'd like to be able to detect if a dataclass instance has had replace called on it and throw an exception. It…
5
votes
1 answer

Define constraints for fields in a python dataclass

I quite a newbie to python dataclasses and was wondering if there is a smart way of defining constraints for fields in a python dataclass. Let us assume we have a data class "SomeConfiguration" with 3 fields (field1, field2, field3) which are all…
m0e33
  • 81
  • 5
5
votes
1 answer

Extending frozen dataclass and take all data from base class instance

Suppose we have a class coming from a library, @dataclass(frozen=True) class Dog: name: str blabla : int # lot of parameters # ... whatever: InitVar[Sequence[str]] I have a dog constructor coming from an external library. pluto…
farbiondriven
  • 2,450
  • 2
  • 15
  • 31
5
votes
2 answers

Python: Circular dependency of dataclasses / Forward variable declaration?

So, I have these two dataclasses in a file: @dataclass class A: children: List[B] @dataclass class B: parent: A , which are possible with the use of the __future__.annotations feature. Then I have two other files, each with a bunch of…
5
votes
3 answers

How to declare numpy array of particular type as type in dataclass

What I have: I am creating a dataclass and I am stating the types of its elements: class Task(): n_items: int max_weight: int max_size: int items: numpy.array(Item) # incorrect way of doing it What I want to do I'd like…
Ania
  • 105
  • 1
  • 4
5
votes
2 answers

How to get index of python3 dataclass instance?

I would like to limit the max number of instance of a dataclass and to know the index of the instance. This is the behaviour that I want: Veget('tomato', 2.4, 5) Veget('salad', 3.5, 2) Veget('carot', 1.2, 7) for Veget in Veget.instances: …
laticoda
  • 100
  • 14
5
votes
1 answer

Serialising a list of dataclasses: str always calls repr

I'd like to serialise dataclasses to strings. That's easy enough with dataclasses.asdict and creating a custom __str__ method. It works perfectly, even for classes that have other dataclasses or lists of them as members. However, calling str on a…
Felix
  • 2,548
  • 19
  • 48
5
votes
4 answers

How to make non-frozen dataclass frozen, and vice versa?

I want to know a simple way to make a dataclass bar frozen. @dataclass class Bar: foo: int bar = Bar(foo=1) In other words, I want a function like the following some_fn_to_freeze frozen_bar = some_fn_to_freeze(bar) frozen_bar.foo = 2 #…
tamuhey
  • 2,904
  • 3
  • 21
  • 50
5
votes
3 answers

How do I access another argument in a default argument in a python dataclass?

I am trying to derive the default value of id_ from name and vice versa. @dataclass class Item: id_ = NAME_TO_ID[name] name = ID_TO_NAME[id_] I should be able to call the class like so: Item(id_=123) Item(name='foo') If possible, I would…
skytect
  • 387
  • 3
  • 15
4
votes
1 answer

How to check if a dataclass is frozen?

Is there a way to check if a Python dataclass has been set to frozen? If not, would it be valuable to have a method like is_frozen in the dataclasses module to perform this check? e.g. from dataclasses import dataclass,…
schliwiju
  • 43
  • 6
4
votes
0 answers

Is there a Python equivalent of TypeScript's Pick type?

TypeScript has a Pick type which can take an existing type and create a new one by picking individual attributes. I would like to have a similar functionality in Python, so for example: from dataclasses import dataclass from pick import…
Cory Nezin
  • 51
  • 2
4
votes
1 answer

A pythonic way to init inherited dataclass from an object of parent type

Given a dataclass structure: @dataclass class RichParent: many_fields: int = 1 more_fields: bool = False class Child(RichParent): some_extra: bool = False def __init__(seed: RichParent): # how to init more_fields and…
y.selivonchyk
  • 8,987
  • 8
  • 54
  • 77
4
votes
2 answers

How can i type hint the init params are the same as fields in a dataclass?

Let us say I have a custom use case, and I need to dynamically create or define the __init__ method for a dataclass. For exampel, say I will need to decorate it like @dataclass(init=False) and then modify __init__() method to taking keyword…
rv.kvetch
  • 9,940
  • 3
  • 24
  • 53
4
votes
3 answers

Convert dataclass of dataclass to json string

I have a json string that I want to read, convert it to an object that I can manipulate, and then convert it back into a json string. I am utilizing the python 3.10 dataclass, and one of the attributes of the class is another class (mySubClass). …
Alex
  • 43
  • 1
  • 5