Skip to content

Fields from dataclasses that are Generic with unspecified TypeVars don't get serialized #66

@rhaps0dy

Description

@rhaps0dy

Here's a minimal reproduction:

import dataclasses
from typing import Generic, TypeVar

import databind.json

T = TypeVar("T")


@dataclasses.dataclass
class A(Generic[T]):
    a_field: int


@dataclasses.dataclass
class B(A):
    b_field: str


b = B(2, "hi")
out = databind.json.get_object_mapper().serialize(b, B)

print(out)  # prints `{'b_field': 'hi'}`, which is wrong.

I've traced this to this line, which adds to the queue base in (hint.bases or hint.type.__bases__).

When T is uninstantiated, the former is non-empty, so the base dataclasses that come the normal way don't get added.

I don't fully understand this code but I think the fix is:

for base in (*hint.bases, *hint.type.__bases__)

to make sure we iterate over everything.

Metadata

Metadata

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions