yukinarit / pyserde

Yet another serialization library on top of dataclasses, inspired by serde-rs.
https://yukinarit.github.io/pyserde/guide/en
MIT License
735 stars 41 forks source link

Support PEP 695 type statements #611

Open davetapley opened 2 weeks ago

davetapley commented 2 weeks ago

Repro (requires Python >3.12):

from dataclasses import dataclass

from serde.yaml import to_yaml

@dataclass
class Foo:
    a: int

@dataclass
class Bar:
    b: int

@dataclass
class ContainerUnion:
    data: Foo | Bar

c = ContainerUnion(Foo(1))

print(to_yaml(c))  # ok

type FooBar = Foo | Bar

@dataclass
class ContainerType:
    data: FooBar

c = ContainerType(Foo(1))

print(to_yaml(c))
# serde.compat.SerdeError: Unsupported type: Foo

https://typing.readthedocs.io/en/latest/spec/aliases.html#type-statement

davetapley commented 2 weeks ago

Not that if you remove type (i.e. FooBar = Foo | Bar), then it works.

So that's a good enough workaround for now, for me, since I don't need generics.