Skip to content

Commit

Permalink
Updates for py 3.9-10
Browse files Browse the repository at this point in the history
  • Loading branch information
seandstewart committed Feb 24, 2024
1 parent 3a7a0c2 commit 1c94696
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
4 changes: 4 additions & 0 deletions src/typical/compat.py
Expand Up @@ -99,6 +99,8 @@ def evaluate_forwardref(
localns: Any,
recursive_guard: set = None,
) -> Any:
if type(type_) is not ForwardRef:
return type_
recursive_guard = recursive_guard or set()
return type_._evaluate(globalns, localns, recursive_guard)

Expand All @@ -111,6 +113,8 @@ def evaluate_forwardref(
localns: Any,
recursive_guard: set = None,
) -> Any:
if type(type_) is not ForwardRef:
return type_
arg = transform_annotation(type_.__forward_arg__)
globalns = {**globals(), **globalns}
type_ = ForwardRef(arg, is_argument=type_.__forward_is_argument__)
Expand Down
5 changes: 4 additions & 1 deletion src/typical/inspection.py
Expand Up @@ -145,6 +145,9 @@ def get_args(annotation: Any) -> Tuple[Any, ...]:
args = typing.get_args(annotation)
if not args:
args = getattr(annotation, "__args__", args)
if not isinstance(args, Iterable):
return ()

return (*_normalize_typevars(*args),)


Expand Down Expand Up @@ -301,7 +304,7 @@ def _hints_from_signature(obj: Union[Type, Callable]) -> Dict[str, Type[Any]]:
if annotation.__class__ is str:
ref = compat.ForwardRef(annotation)
try:
annotation = compat.eval_type(ref, globalns or None, None)
annotation = compat.evaluate_forwardref(ref, globalns or None, None)
except NameError:
annotation = ref
hints[name] = annotation
Expand Down
18 changes: 13 additions & 5 deletions tests/legacy/test_typed.py
Expand Up @@ -263,15 +263,23 @@ def test_get_origin(annotation, origin):
@pytest.mark.parametrize(
argnames=("annotation", "args"),
argvalues=[
(typing.List, (typing.Any,)),
(typing.List[T], (typing.Any,)),
(typing.List[str], (str,)),
(typing.Optional[str], (str, type(None))),
(typing.List, ((typing.Any,), ())),
(typing.List[T], ((typing.Any,), ())),
(typing.List[str], ((str,),)),
(
typing.Optional[str],
(
(
str,
type(None),
),
),
),
],
ids=objects.get_id,
)
def test_get_args(annotation, args):
assert get_args(annotation) == args
assert get_args(annotation) in args


@pytest.mark.parametrize(
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/test_inspection.py
Expand Up @@ -2,6 +2,8 @@

import dataclasses
import inspect
import sys
import typing
import typing as t

import pytest
Expand Down Expand Up @@ -114,6 +116,7 @@ class FieldTuple(t.NamedTuple):
VarTuple = t.Tuple[str, ...]


@pytest.mark.skipif(sys.version_info < (3, 9), reason="3.8 doesn't handle ForwardRef.")
@pytest.mark.suite(
user_class=dict(
annotation=FieldClass,
Expand Down Expand Up @@ -162,7 +165,7 @@ class FieldTuple(t.NamedTuple):
name="field",
kind=inspect.Parameter.POSITIONAL_OR_KEYWORD,
default=None,
annotation="str",
annotation=typing.ForwardRef("str"),
),
)
),
Expand Down

0 comments on commit 1c94696

Please sign in to comment.