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

Cherry pick #283 #285

Merged
merged 2 commits into from
Dec 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 22 additions & 19 deletions apischema/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,27 +158,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] = (Union if is_union(hint) else 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
4 changes: 3 additions & 1 deletion apischema/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ def substitute_type_vars(tp: AnyType, substitution: Mapping[TV, AnyType]) -> Any
except KeyError:
return Union[tp.__constraints__] if tp.__constraints__ else Any
elif getattr(tp, "__parameters__", ()):
return tp[tuple(substitution.get(p, p) for p in tp.__parameters__)]
return (Union if is_union(tp) else tp)[
tuple(substitution.get(p, p) for p in tp.__parameters__)
]
else:
return tp

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.16.5",
version="0.16.6",
url="https://github.com/wyfo/apischema",
author="Joseph Perez",
author_email="joperez@hotmail.fr",
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_unsupported_union_member.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from dataclasses import dataclass
from typing import Annotated, Union
from typing import Annotated, Union # type: ignore

from pytest import raises

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