-
Notifications
You must be signed in to change notification settings - Fork 1
Open
Description
Please consider the following code:
import dataglasses
from dataclasses import dataclass
@dataclass
class A:
x: int
def coerce(val: int | str) -> int:
try:
return int(val)
except ValueError as e:
raise ValueError(f'Cannot coerce {val!r} to int')
transform = {
int: (int | str, coerce)
}
a = dataglasses.from_dict(A,{'x': 1},transform=transform)
It fails with
TypeError: Expected value corresponding to one of (<class 'int'>, <class 'str'>), got 1
even though 1 is a lecit integer.
The reason is that the presence of int in the returned union creates an infinite recursion because the system tries to apply transform rules to it a receives it back again and again (in the form or a union). Maybe a possible fix is to add transformed=transformed as argument of _from_dict in the union management block?
P.S. Same beaviour (infinite recursion) is fired in schema generation
Metadata
Metadata
Assignees
Labels
No labels