Skip to content

Generics and Flatten doesn't work #379

@yukinarit

Description

@yukinarit
from dataclasses import dataclass
from typing import Generic, TypeVar

from serde import from_dict, serde, to_dict, field

T = TypeVar("T")

@serde
@dataclass
class Foo(Generic[T]):
    v: T

@serde
@dataclass
class Bar(Generic[T]):
    foo: Foo[T] = field(flatten=True)

def main() -> None:
    bar = Bar[int](Foo[int](10))
    d = to_dict(bar)
    print(d)
    print(from_dict(Bar[int], d))


if __name__ == "__main__":
    main()

Got

{'foo': 10}
Traceback (most recent call last):
  File "/Users/yukinaritani/repos/p/pyserde2/examples/generics_flatten.py", line 45, in <module>
    main()
  File "/Users/yukinaritani/repos/p/pyserde2/examples/generics_flatten.py", line 41, in main
    print(from_dict(GenericBar[int], d))
  File "/Users/yukinaritani/repos/p/pyserde2/serde/de.py", line 505, in from_dict
    return from_obj(cls, o, named=True, reuse_instances=reuse_instances)
  File "/Users/yukinaritani/repos/p/pyserde2/serde/de.py", line 472, in from_obj
    raise SerdeError(e) from None
serde.compat.SerdeError: 'v'

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugBug report or fix

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions