Skip to content

Commit

Permalink
Cherry pick #283 (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
wyfo committed Dec 21, 2021
1 parent 7b39a37 commit 97b67b7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
41 changes: 22 additions & 19 deletions apischema/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,27 +157,30 @@ def resolve_type_hints(obj: Any) -> Dict[str, Any]:
`obj` can also be a parametrized generic class."""
origin_or_obj = get_origin(obj) or obj
hints = get_type_hints(origin_or_obj, include_extras=True)
if isinstance(origin_or_obj, type):
hints = {}
for base in reversed(generic_mro(obj)):
base_origin = get_origin(base)
if base_origin is not None and getattr(base_origin, "__parameters__", ()): # type: ignore
substitution = dict(zip(base_origin.__parameters__, get_args(base)))
base_annotations = getattr(base_origin, "__dict__", {}).get(
"__annotations__", {}
)
for name, hint in get_type_hints(base_origin).items():
if name not in base_annotations:
continue
if isinstance(hint, TypeVar):
hints[name] = substitution.get(hint, hint)
elif getattr(hint, "__parameters__", ()):
hints[name] = hint[
tuple(substitution.get(p, p) for p in hint.__parameters__)
]
else:
hints[name] = hint
return hints
base_origin = get_origin(base) or base
base_annotations = getattr(base_origin, "__dict__", {}).get(
"__annotations__", {}
)
substitution = dict(
zip(getattr(base_origin, "__parameters__", ()), get_args(base))
)
for name, hint in get_type_hints(base_origin, include_extras=True).items():
if name not in base_annotations:
continue
if isinstance(hint, TypeVar):
hints[name] = substitution.get(hint, hint)
elif getattr(hint, "__parameters__", ()):
hints[name] = hint[
tuple(substitution.get(p, p) for p in hint.__parameters__)
]
else:
hints[name] = hint
return hints
else:
return get_type_hints(obj, include_extras=True)


_T = TypeVar("_T")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name="apischema",
version="0.15.8",
version="0.15.9",
url="https://github.com/wyfo/apischema",
author="Joseph Perez",
author_email="[email protected]",
Expand Down
9 changes: 7 additions & 2 deletions tests/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
required_keys,
resolve_type_hints,
)
from apischema.typing import Annotated

T = TypeVar("T")
U = TypeVar("U")
Expand All @@ -28,7 +29,7 @@ class C(B[str]):


class D(C):
pass
d: Annotated[int, ""]


test_cases = [
Expand All @@ -39,7 +40,11 @@ class D(C):
(B[U], [B[U], A[int, U]], {"t": int, "u": U, "v": U}),
(B[str], [B[str], A[int, str]], {"t": int, "u": str, "v": str}),
(C, [C, B[str], A[int, str]], {"t": int, "u": str, "v": str}),
(D, [D, C, B[str], A[int, str]], {"t": int, "u": str, "v": str}),
(
D,
[D, C, B[str], A[int, str]],
{"t": int, "u": str, "v": str, "d": Annotated[int, ""]},
),
]


Expand Down

0 comments on commit 97b67b7

Please sign in to comment.