Skip to content

Commit 3b56ac3

Browse files
committed
Expand existing tests.
1 parent 9f27f3f commit 3b56ac3

File tree

1 file changed

+93
-25
lines changed

1 file changed

+93
-25
lines changed

tests/test_type_eval.py

Lines changed: 93 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
Tuple,
1616
TypeVar,
1717
Union,
18-
get_args,
1918
overload,
2019
)
2120

@@ -424,20 +423,26 @@ def f[TX](self, x: TX) -> OnlyIntToSet[TX]: ...
424423

425424
m = eval_typing(GetMember[C, Literal["f"]])
426425
assert eval_typing(m.name) == Literal["f"]
426+
assert eval_typing(IsAssignable[m.type, GenericCallable])
427427
assert eval_typing(m.quals) == Literal["ClassVar"]
428428
assert eval_typing(m.definer) == C
429429

430-
t = eval_typing(m.type)
431-
Vs = get_args(get_args(t)[0])
432-
L = get_args(t)[1]
433-
f = L(*Vs)
434-
assert (
435-
f
436-
== Callable[
437-
[Param[Literal["self"], C], Param[Literal["x"], Vs[0]]],
438-
OnlyIntToSet[Vs[0]],
439-
]
440-
)
430+
ft = m.__args__[1].__args__[1]
431+
with _ensure_context():
432+
assert (
433+
eval_typing(ft(str))
434+
== Callable[
435+
[Param[Literal["self"], C], Param[Literal["x"], str]],
436+
str,
437+
]
438+
)
439+
assert (
440+
eval_typing(ft(int))
441+
== Callable[
442+
[Param[Literal["self"], C], Param[Literal["x"], int]],
443+
set[int],
444+
]
445+
)
441446

442447

443448
def test_getmember_03():
@@ -448,20 +453,26 @@ def f[T](self, x: T) -> OnlyIntToSet[T]: ...
448453

449454
m = eval_typing(GetMember[P, Literal["f"]])
450455
assert eval_typing(m.name) == Literal["f"]
456+
assert eval_typing(IsAssignable[m.type, GenericCallable])
451457
assert eval_typing(m.quals) == Literal["ClassVar"]
452458
assert eval_typing(m.definer) != C # eval typing generates a new class
453459

454-
t = eval_typing(m.type)
455-
Vs = get_args(get_args(t)[0])
456-
L = get_args(t)[1]
457-
f = L(*Vs)
458-
assert (
459-
f
460-
== Callable[
461-
[Param[Literal["self"], Self], Param[Literal["x"], Vs[0]]],
462-
OnlyIntToSet[Vs[0]],
463-
]
464-
)
460+
ft = m.__args__[1].__args__[1]
461+
with _ensure_context():
462+
assert (
463+
eval_typing(ft(str))
464+
== Callable[
465+
[Param[Literal["self"], Self], Param[Literal["x"], str]],
466+
str,
467+
]
468+
)
469+
assert (
470+
eval_typing(ft(int))
471+
== Callable[
472+
[Param[Literal["self"], Self], Param[Literal["x"], int]],
473+
set[int],
474+
]
475+
)
465476

466477

467478
def test_getmember_04():
@@ -786,15 +797,18 @@ def test_eval_getarg_callable_02():
786797
T = TypeVar("T")
787798

788799
# Params not wrapped
789-
f = Callable[[T], T]
800+
f = lambda T: Callable[[T], T]
790801
gc = GenericCallable[tuple[T], f]
791802
t = eval_typing(GetArg[gc, GenericCallable, Literal[0]])
792803
assert t == tuple[T]
793804
gc_f = eval_typing(GetArg[gc, GenericCallable, Literal[1]])
794805
assert gc_f == Never
795806

807+
with _ensure_context():
808+
assert gc.__args__[1](str) == Callable[[str], str]
809+
796810
# Params wrapped
797-
f = Callable[
811+
f = lambda T: Callable[
798812
[
799813
Param[Literal[None], T, Literal["positional"]],
800814
Param[Literal["y"], T],
@@ -811,6 +825,19 @@ def test_eval_getarg_callable_02():
811825
gc_f = eval_typing(GetArg[gc, GenericCallable, Literal[1]])
812826
assert gc_f == Never
813827

828+
with _ensure_context():
829+
assert (
830+
gc.__args__[1](str)
831+
== Callable[
832+
[
833+
Param[Literal[None], str, Literal["positional"]],
834+
Param[Literal["y"], str],
835+
Param[Literal["z"], str, Literal["keyword"]],
836+
],
837+
str,
838+
]
839+
)
840+
814841

815842
type IndirectProtocol[T] = NewProtocol[*[m for m in Iter[Members[T]]],]
816843
type GetMethodLike[T, Name] = GetArg[
@@ -959,6 +986,20 @@ def f[T](self, x: T, /, y: T, *, z: T) -> T: ...
959986
f = eval_typing(GetArg[gc, GenericCallable, Literal[1]])
960987
assert f is Never
961988

989+
with _ensure_context():
990+
assert (
991+
gc.__args__[1](str)
992+
== Callable[
993+
[
994+
Param[Literal["self"], C, Literal["positional"]],
995+
Param[Literal["x"], str, Literal["positional"]],
996+
Param[Literal["y"], str],
997+
Param[Literal["z"], str, Literal["keyword"]],
998+
],
999+
str,
1000+
]
1001+
)
1002+
9621003

9631004
def test_eval_getarg_callable_08():
9641005
# generic classmethod
@@ -973,6 +1014,20 @@ def f[T](cls, x: T, /, y: T, *, z: T) -> T: ...
9731014
f = eval_typing(GetArg[gc, GenericCallable, Literal[1]])
9741015
assert f is Never
9751016

1017+
with _ensure_context():
1018+
assert (
1019+
gc.__args__[1](str)
1020+
== classmethod[
1021+
C,
1022+
tuple[
1023+
Param[Literal["x"], str, Literal["positional"]],
1024+
Param[Literal["y"], str],
1025+
Param[Literal["z"], str, Literal["keyword"]],
1026+
],
1027+
str,
1028+
]
1029+
)
1030+
9761031

9771032
def test_eval_getarg_callable_09():
9781033
# generic staticmethod
@@ -987,6 +1042,19 @@ def f[T](x: T, /, y: T, *, z: T) -> T: ...
9871042
f = eval_typing(GetArg[gc, GenericCallable, Literal[1]])
9881043
assert f is Never
9891044

1045+
with _ensure_context():
1046+
assert (
1047+
gc.__args__[1](str)
1048+
== staticmethod[
1049+
tuple[
1050+
Param[Literal["x"], str, Literal["positional"]],
1051+
Param[Literal["y"], str],
1052+
Param[Literal["z"], str, Literal["keyword"]],
1053+
],
1054+
str,
1055+
]
1056+
)
1057+
9901058

9911059
def test_eval_getarg_tuple():
9921060
t = tuple[int, ...]

0 commit comments

Comments
 (0)