Skip to main content
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 ...
Luis Daniel's user avatar
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 ...
globglogabgalab's user avatar
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 ...
Aadvik's user avatar
  • 1,504
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 ...
Aadvik's user avatar
  • 1,504
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 ...
JKomp's user avatar
  • 99
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 ...
Dominik Kaszewski's user avatar
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, ...
Durtal's user avatar
  • 1,108
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 ...
Hecatonchires's user avatar
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(...
bzm3r's user avatar
  • 4,654
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 ...
Jaime 's user avatar
  • 143
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): ...
spacether's user avatar
  • 2,821
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 ...
5th's user avatar
  • 2,515
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 = ...
Siddharth Suri's user avatar
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]) -> '...
hvtilborg's user avatar
  • 1,487
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, **{&...
THATS MY QUANT MY QUANTITATIVE's user avatar

15 30 50 per page
1
2 3 4 5
70