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

Error when serializing/deserializing object from Generic subclass #38

Open
NiklasRosenstein opened this issue Mar 23, 2023 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@NiklasRosenstein
Copy link
Owner

Expected behaviour: When (de)serializing a dataclass that subclasses from Generic[T] and it has a member that uses T, we should be able to handle the member annotated with T gracefully when a concrete type is given.

Actual behaviour: Databind does not understand the concrete type for a type variable and fails at trying to (de)serialize the field when a concrete type A is given but the type at that position is TypeHint(~T).

Steps to reproduce:

from dataclasses import dataclass
from typing import Generic, TypeVar
from databind.json import dump, load

T = TypeVar("T")

@dataclass
class Box(Generic[T]):
  value: T

print(dump(Box(42), Box[int]))         # no serializer for `TypeHint(~T)` ...
print(load({"value": 42}, Box[int]))   # no deserializer for `TypeHint(~T)` ...

Results in

databind.core.converter.NoMatchingConverter: no serializer for `TypeHint(~T)` and payload of type `int`

Trace:
    $: TypeHint(__main__.Box[int])
    .value: TypeHint(~T)
@NiklasRosenstein NiklasRosenstein added the bug Something isn't working label Mar 23, 2023
@NiklasRosenstein NiklasRosenstein self-assigned this Mar 23, 2023
@rhaps0dy
Copy link
Contributor

This bug seems fixed in the latest develop branch. Should we close?

@NiklasRosenstein
Copy link
Owner Author

I can't remember fixing it tbh., what Python version have you tested it on? Regardless, I would want to add a test case before closing this.

@rhaps0dy
Copy link
Contributor

Python 3.10. Yeah, I agree there should be a test case.

Zooming out, have you considered using Hypothesis (https://hypothesis.readthedocs.io/en/latest/) to test python-databind? The usecase here is perfect: write a test-case-generator for types and instances, and then apply it to a round-trip test. Then apply more computation and find bugs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants