Skip to content

Commit bbad452

Browse files
OmarAlJarrahclaude
andcommitted
fix(serde): resolve generic-dataclass type parameters on Python 3.12
Decoding a PEP 695 generic dataclass (e.g. `Box[int]`, declared `class Box[T]`) raised `CodecError: name 'T' is not defined` on Python 3.12. The field annotations reference the class type parameters, and 3.12's `get_type_hints` does not place those parameters in scope (3.13+ resolves them automatically). Pass the class `__type_params__` through `get_type_hints`'s `localns` so the annotations resolve on every supported interpreter. Verified against the full suite on 3.12, 3.13, and 3.14. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 865d217 commit bbad452

1 file changed

Lines changed: 7 additions & 1 deletion

File tree

  • packages/dexpace-sdk-core/src/dexpace/sdk/core/serde

packages/dexpace-sdk-core/src/dexpace/sdk/core/serde/codec.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,8 +900,14 @@ def _resolve_info(target: type) -> _ModelInfo:
900900
info = _MODEL_CACHE.get(target)
901901
if info is not None:
902902
return info
903+
# A PEP 695 generic dataclass (``class Box[T]``) annotates fields with its
904+
# type parameters (``item: T``). Python 3.13+ resolves those automatically,
905+
# but 3.12's ``get_type_hints`` does not see them and raises ``NameError``;
906+
# supply them via ``localns`` so resolution works on every supported version.
907+
type_params = getattr(target, "__type_params__", ())
908+
localns = {tp.__name__: tp for tp in type_params} or None
903909
try:
904-
hints = get_type_hints(target, include_extras=True)
910+
hints = get_type_hints(target, include_extras=True, localns=localns)
905911
except NameError as err:
906912
# An unresolvable forward reference (a string annotation whose name is
907913
# not in scope) surfaces as a bare ``NameError`` from ``get_type_hints``;

0 commit comments

Comments
 (0)