1,041 questions
0
votes
0
answers
55
views
Generating Sphinx documentation for dataclasses with Annotated[] fields
I'd like to use the autodoc_typehints functionality of Sphinx (or the package sphinx_autodoc_typehints) to document fields of Python dataclasses, and I'm currently having trouble getting nice output.
...
Best practices
0
votes
4
replies
96
views
Dataclass from dynamically created class
I am trying to create a dataclass from a dynamically created class. I am doing something like
[1] A = dataclass(type("ClsA", (), {"a":1}))
However, the attribute a is not ...
1
vote
3
answers
70
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
198
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
191
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
74
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
253
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 ...
-3
votes
1
answer
77
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
150
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
90
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
268
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
251
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
122
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
166
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
59
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 ...