1,037 questions
0
votes
0
answers
61
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
63
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
201
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 ...
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 ...
1
vote
1
answer
131
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
84
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
96
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
45
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
92
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
152
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
...
0
votes
1
answer
135
views
How can I define a protocol for a class that contains a protocol field and use a dataclass implementer?
I want to do this:
import typing
import dataclasses
class ConfigProtocol(typing.Protocol):
a: str
@dataclasses.dataclass
class Config:
a: str
class HasConfigProtocol(typing.Protocol):
...
1
vote
2
answers
113
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
184
views
How can I provide type hints while destructuring my class
I would like to create a class that looks something like ConfigAndPath:
import pathlib
from typing import TypeVar, Generic
from dataclasses import dataclass, astuple
class ConfigBase:
pass
T = ...
1
vote
1
answer
98
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]) -> '...
2
votes
2
answers
182
views
how to gracefully ignore non-matching keyword matching arguments in python dataclass
With normal classes you have **kwargs in the __init__ so non-matching keyword arguments can be ignored:
class MyClass:
def __init__(self, a, **kwargs):
self.a=a
my_class = MyClass(20, **{&...