1,037 questions
-2
votes
1
answer
62
views
dataclass __repr__ shows init=False members
import dataclasses
@dataclasses.dataclass
class MyClass:
a: int
b: int = dataclasses.field(default=0, init=False)
m = MyClass(a=0)
print(repr(m))
# prints: "MyClass(a=0, b=0)"
# `...
3
votes
1
answer
70
views
Trying to reduce the verboseness of __post_init__ in a python dataclass
I am writing a Python config script that creates an array of input files in a domain-specific language (DSL), so my use case is a bit unusual. In this scenario, we want medium-level users to be able ...
0
votes
0
answers
31
views
How to use the data class itself as the type of its attribute [duplicate]
I have a data class:
from dataclasses import dataclass
from typing import Any
@dataclass
class Url:
http: bool
params: dict[str: Any]
body: str
# More stuff here
Here I have a Url ...
2
votes
2
answers
91
views
Data class with argument optional only in init
I have the following simple class in Python:
class Point:
def __init__(x: int, y: int | None = None):
self.x = x
self.y = y if y is not None else x
How can the same thing be ...
0
votes
1
answer
46
views
Find field throwing error in Python Dataclass conversion
I'm trying to convert a json array to a Python list of typed objects. It's data from Teltonika FOTA
The call result_list = fromlist(FotaDevice, intermediate_list) is failing with the error message ...
4
votes
3
answers
158
views
Create a typing from a custom dataclass
I'm currently working with a dataclass (defined in one of the dependencies I'm currently working with inside of my project), which is defined similar to this snippet:
from dataclasses import dataclass
...
1
vote
2
answers
122
views
Dataclass/attrs based orm models and their relationships
I am building a small python application to learn Domain-Driven-Design (DDD) approaches. Therefore I am using a dataclass/attrs class as my domain model and also use this class to imperatively model ...
1
vote
1
answer
103
views
Python dataclasses, inheritance and alternate class constructors
In Python, I am using dataclass with the following class hierarchy:
from dataclasses import dataclass
@dataclass
class Foo:
id: str
@classmethod
def fromRaw(cls, raw: dict[str, str]) -> '...
0
votes
1
answer
205
views
How to create "dynamic" Literal types from dataclass members
How to do add support for custom smart type completion in Python?
Let's say I have a dataclass:
@dataclass
class MyData:
mine: str
yours: str
def col(self, value: str):
return "The ...
0
votes
0
answers
79
views
python/Jupyter-lab: How to use built-in JSON visualization functionality for a dataclass
IPython/Jupyter has built-in functionality to visualise JSON structures as an interactive tree where you can expand and collapse nodes. I like using it for lists and dictionaries, but now I want to ...
2
votes
1
answer
132
views
How to make an easily instantiable derivative attribute-only protocol class?
I have a Protocol subclass that defines objects with attributes from an external library:
class P(Protocol):
val: int
For testing purposes, I want to turn this protocol class into something I can ...
0
votes
1
answer
61
views
Python dictionary vs dataclass (and metaclasses) for dynamic attributes
Less object oriented snippet (1):
class Trickjump(TypedDict):
id: int
name: str
difficulty: str
...
Dataclass snippet (2):
@dataclass
class Trickjump():
id: IdAttr
name: ...
2
votes
2
answers
346
views
How to make my dataclass compatible with ctypes and not lose the “dunder“ methods?
Consider a simple data class:
from ctypes import c_int32, c_int16
from dataclasses import dataclass
@dataclass
class MyClass:
field1: c_int32
field2: c_int16
According to the docs, if we ...
2
votes
1
answer
82
views
Generate dataclass for typing programmatically
How can I express
@dataclass
class Measurements:
width: int
height: int
head_left: int
head_right: int
head_width: int
head_top: int
head_bottom: int
head_height: int
...
0
votes
1
answer
49
views
Access container class from contained class with python dataclasses
I have a parent class, that contains a child class. Both are implemented with python dataclasses. The classes look like this:
from __future__ import annotations
from dataclasses import dataclass
@...
1
vote
0
answers
75
views
Refactoring marshmellow schema to pydantic 2
I have been struggling to refactor this marshmellow schema to pydantic 2. Problem is with AttributeValue field. When I refactor this to pydantic, I always get strange errors regarding this field. This ...
0
votes
0
answers
71
views
How to enforce a data schema on a numpy array
I am using numpy arrays extensively in my codebase. The different columns of the array relate to different data. I want to create a class that enforces some schema and allows custom methods to be ...
2
votes
1
answer
267
views
Reading C struct dumped into a file into Python Dataclass
Is there a consistent way of reading a c struct from a file back into a Python Dataclass?
E.g.
I have this c struct
struct boh {
uint32_t a;
int8_t b;
boolean c;
};
And I want to read it's data ...
0
votes
1
answer
45
views
Is it safe to use dataclasses with hidden/non-field inherited properties?
I have a dataclass use strategy that is quite flexible, but I am unsure if it is relying on features that aren't part of the type contract. I am often telling people to not rely on the insertion order ...
-1
votes
3
answers
101
views
Re-decorate a python (class) decorator [duplicate]
I'd like to create a decorator that basically wraps an already existing decorator that has parameters, such that the new decorator acts like the old one with some of the arguments supplied.
...
0
votes
2
answers
81
views
Descriptor in DataClass
Here is descriptor class:
#descriptor
class Validator:
def __set_name__(self,owner,name):
self.private_name = '_' + name
def __set__(self,obj,value):
self.validate(value)
...
0
votes
1
answer
346
views
Failing to serialize a dataclass generated with make_dataclass with pickle
In my code, I am generating classes run-time using the make_dataclass function.
The problem is that such dataclasses cannot be serialized with pickle as shown in the code snippet here below.
It is ...
0
votes
1
answer
67
views
How to declare multiple similar member variables of a dataclass in a single line?
Can this dataclass declaration:
@dataclass
class Point:
x: float
y: float
z: float
be rewritten in order to reduce boilerplate and resemble something like this:
@dataclass
class Point:
x, y, ...
1
vote
1
answer
201
views
How to get nice types from python `dataclass` without instantiating the class
Let's say I have the following class:
from dataclasses import dataclass
from typing import Annotated
@dataclass
class Pizza:
price: Annotated[float, "money"]
size: Annotated[float, &...
3
votes
2
answers
324
views
Why are type annotations needed in Python dataclasses?
In opposition to standard classes in Python dataclasses fields must have a type annotation. But I don't understand what the purpose of these type annotations really is. One can create a dataclass like ...