Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generics and Flatten doesn't work #379

Open
yukinarit opened this issue Jun 11, 2023 · 0 comments
Open

Generics and Flatten doesn't work #379

yukinarit opened this issue Jun 11, 2023 · 0 comments
Labels
bug Bug report or fix

Comments

@yukinarit
Copy link
Owner

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'
@yukinarit yukinarit added the bug Bug report or fix label Jun 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug report or fix
Projects
Status: No status
Development

No branches or pull requests

1 participant