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

Top-level generic value does not deserialize correctly #516

Closed
indigoviolet opened this issue Apr 22, 2024 · 1 comment
Closed

Top-level generic value does not deserialize correctly #516

indigoviolet opened this issue Apr 22, 2024 · 1 comment

Comments

@indigoviolet
Copy link

from serde import serde
from serde.yaml import from_yaml
from typing import Generic, TypeVar

yaml = """
foo: 1
bar:
    bar: 2
"""

T = TypeVar("T")


@serde
class Bar(Generic[T]):
    bar: T


@serde
class Foo(Generic[T]):
    foo: T
    bar: Bar[T]


# prints Foo(foo=1, bar=Bar(bar='2'))
# but should Foo.foo should be a str as well
from_yaml(Foo[str], yaml)
@yukinarit
Copy link
Owner

Hi @indigoviolet

pyserde depends on beartype package for type checks, and unfortunately, runtime generic parameters are not taken into account. Can you provide type boundaries for generic parameters?

T = TypeVar("T", bound=str)

so you can get error during deserializing

serde.compat.SerdeError: Method __main__.Foo.__init__() parameter foo=1 violates type hint ~T, as int 1 not instance of str.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants