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

Reserved word as an attribute name in a dataclass when parsing a JSON object

I stumbled upon a problem, when I was working on my ETL pipeline. I am using dataclasses dataclass to parse JSON objects. One of the keywords of the JSON object is a reserved keyword. Is there a way around this: from dataclasses import…
Naz
  • 2,012
  • 1
  • 21
  • 45
7
votes
2 answers

How to initialize Python dataclass 2D list?

Is is possible to create a default initializer to python dataclasses initializing a 2D array, i.e. resulting in the same as from dataclasses import dataclass, field from typing import List MAX = 5 @dataclass class AlgoData: list2D:…
mrtnlrsn
  • 1,105
  • 11
  • 19
7
votes
1 answer

Does cython support dataclasses or something similar

I am interested in passing data between Python and Cython code so that the data are accessible from C and without GIL. I am thinking about achieving this using dataclasses (since py3.7), named tuples (with nice defining syntax since py3.6) or…
Lefty
  • 407
  • 5
  • 12
7
votes
0 answers

Python @dataclass with multiple inheritance (mixin) and mixed default options

I'm trying to create a Python 3.7 dataclass composed of inherited dataclasses: from dataclasses import dataclass @dataclass class A: title: str synopsis: str = "A book" @dataclass class B: id: str description: str = "A short book…
Alastair McCormack
  • 26,573
  • 8
  • 77
  • 100
7
votes
1 answer

Python 3.7: Initialize objects with dataclasses module?

Here is the my code in python 3.6 class A(object) def __init__(self, a: str): self._int_a: int = int(a) # desired composition def get_int_a(self) -> int: return self._int_a I want to rewrite this code in python 3.7,…
Vladimir Yahello
  • 243
  • 1
  • 2
  • 10
6
votes
2 answers

Python get the name of all fields in a dataclass

I am trying to write a function to log dataclasses I would like to get the name of all fields in the dataclass and print the value to each (similar to how you might write a function to print a dictionary) i.e. @dataclasses.dataclass class Test: …
Pioneer_11
  • 670
  • 4
  • 19
6
votes
2 answers

Initializing python dataclass object without passing instance variables or default values

I want to initialize python dataclass object even if no instance variables are passed into it and we have not added default values to the param @dataclass class TestClass: paramA: str paramB: float paramC: str obj1 =…
CoderGuyy
  • 137
  • 1
  • 7
6
votes
3 answers

Can I create an Enum of a dataclass instances?

I have a fixed set of three sensors that I want to model as an enum. Each of these sensors is parametrised by a few different attributes. I therefore want to model the sensors themselves as a dataclass. My naive attempt looks something like…
Johz
  • 161
  • 1
  • 4
6
votes
1 answer

How do I get Python dataclass InitVar fields to work with typing.get_type_hints while also using annotations?

When messing with Python dataclasses, I ran into this odd error that's pretty easy to reproduce. from __future__ import annotations import dataclasses as dc import typing @dc.dataclass class Test: foo:…
6
votes
2 answers

Required positional arguments with dataclass properties

It seems there's been a fair bit of discussion about this already. I found this post particularly helpful, and it seems to provide one of the best solutions. But there is a problem with the recommended solution. Well, it seems to work great at…
corvus
  • 556
  • 7
  • 18
6
votes
1 answer

Python dataclass AttributeError

I have a dataclass set up like this: from dataclasses import dataclass, field from typing import List @dataclass class stats: target_list: List[None] = field(default_factory=list) When I try to compare the contents of the list like so: if…
Mato098
  • 65
  • 1
  • 4
6
votes
3 answers

How to define a dataclass so each of its attributes is the list of its subclass attributes?

I have this code: from dataclasses import dataclass from typing import List @dataclass class Position: name: str lon: float lat: float @dataclass class Section: positions: List[Position] pos1 = Position('a', 52, 10) pos2 =…
lhoupert
  • 584
  • 8
  • 25
6
votes
2 answers

Calling generated `__init__` in custom `__init__` override on dataclass

Currently I have something like this: @dataclass(frozen=True) class MyClass: a: str b: str c: str d: Dict[str, str] ...which is all well and good except dicts are mutable, so I can't use my class to key another dictionary. Instead, I'd like…
UtterlyConfused
  • 983
  • 1
  • 10
  • 18
6
votes
2 answers

Type hints for dataclass defined inside a class with generic types

I know that the title is very confusing, so let me take the Binary Search Tree as an example: Using ordinary class definition # This code passed mypy test from typing import Generic, TypeVar T = TypeVar('T') class BST(Generic[T]): class Node: …
Leon Cruz
  • 345
  • 3
  • 11
6
votes
1 answer

How to freeze individual field of non-frozen dataclass?

Is it possible to specify that a single field is frozen in a non-frozen dataclass? Something like this: @dataclass class Data: fixed: int = field(frozen=True) mutable: int = field(frozen=False) d = Data(2, 3) d.mutable = 5 # fine d.fixed…
Dave Doty
  • 285
  • 1
  • 9