1,040 questions
1
vote
3
answers
54
views
Assigning abstract attributes through the constructor in a `jax` python dataclass
I'm trying to subclass the AbstractWrappedSolver dataclass from the jax library diffrax. The class has this definition:
class AbstractWrappedSolver(AbstractSolver[_SolverState]):
"""...
Advice
0
votes
2
replies
97
views
Getters and Setters in Python (Dataclasses)
I'm learning for my Introductory Programming Course in uni and I don't have an overview over the different cases when it comes to Setters and Getters and the slides are not quite helpful.
So for ...
7
votes
1
answer
170
views
How to annotate a function that returns a dataclass field so that type checkers treat it correctly?
I'm currently working on a library that uses dataclasses.dataclass classes for data structures. I'm utilizing the metadata argument of the dataclasses.field() method to inject custom library ...
1
vote
1
answer
71
views
Dataclasses __post_init__ method
Why can't I simply define a 'derived' variable in the class definition and assign it a calculated value, instead of using the __post_init__ method to do so?
Example:
from dataclasses import dataclass, ...
Best practices
2
votes
3
replies
136
views
What signs indicate that Python's dataclasses are NOT a good fit for a particular use-case?
I find Python's dataclasses to be incredibly convenient.
They cut out a ton of boilerplate code (no more needing to write self.foo = foo for every __init__ parameter, and no more needing to spell out ...
-2
votes
1
answer
73
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)"
# `...
0
votes
0
answers
118
views
xsdata dataclass generation from XSD with choice fields
How is it possible using xsdata for the generation of the model (dataclass) from an XSD with choice elements, to perform validations on those choices, so that it only allows one of them to be set?
XSD ...
3
votes
1
answer
80
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 ...
2
votes
2
answers
242
views
How can I make all attributes in a Python dataclass optional without rewriting each one?
I have this data class:
@dataclass
class User:
id: str
name: str
pwd: str
picture: str
url: str
age: int
# many, many more attributes
Now, I have to make all of the ...
1
vote
1
answer
216
views
Is there a way to get a Python dataclass length?
I have a dataclass for a set of timers. Each instance of the dataclass can have a different number of timers. The number of timers in a specific instance is fixed but can vary between instances. I'd ...
2
votes
2
answers
108
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
136
views
How to type hint an attribute to be a dataclass? [duplicate]
I want a class that encapsulates data with some meta information. The data changes during runtime. I want to safe it for evaluation. The class looks like this:
from dataclasses import dataclass, ...
0
votes
1
answer
58
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 ...
1
vote
0
answers
108
views
How can I automatically create a type annotation for a dataclass method based on the dataclass' members?
Consider the following example:
from dataclasses import dataclass
import dataclasses
from typing import Self
@dataclass
class Dataclass:
a: int
b: str
c: list[int]
def update_attrs(...
4
votes
3
answers
177
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
...