Not a particularly pressing bug, and perhaps a bug in beartype rather than pyserde.
Workarounds are documented below.
To reproduce:
from typing import Generic, TypeVar
import serde
# Assuming CustomInt must be declared after T
# we need to use a string for the union type bound
T = TypeVar("T", bound="int | CustomInt")
class CustomInt(int):
pass
# This fails with
# `beartype.roar.BeartypeDecorHintForwardRefException: Forward reference 'int | CustomInt' not valid Python attribute name.`
@serde.serde
class Foo(Generic[T]):
bar: T
Workarounds:
- Define CustomInt class before the type var, so that a string type annotation is not needed. However, this may not always be possible perhaps?
- Use the typing.Union instead of Union syntax:
T = TypeVar("T", bound=Union[int, "CustomInt"])