Skip to content

Commit a5d2429

Browse files
committed
Expand existing tests.
1 parent 86b122e commit a5d2429

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

@@ -428,20 +427,26 @@ def f[TX](self, x: TX) -> OnlyIntToSet[TX]: ...
428427

429428
m = eval_typing(GetMember[C, Literal["f"]])
430429
assert eval_typing(GetName[m]) == Literal["f"]
430+
assert eval_typing(IsAssignable[GetType[m], GenericCallable])
431431
assert eval_typing(GetQuals[m]) == Literal["ClassVar"]
432432
assert eval_typing(GetDefiner[m]) == C
433433

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

446451

447452
def test_getmember_03():
@@ -452,20 +457,26 @@ def f[T](self, x: T) -> OnlyIntToSet[T]: ...
452457

453458
m = eval_typing(GetMember[P, Literal["f"]])
454459
assert eval_typing(GetName[m]) == Literal["f"]
460+
assert eval_typing(IsAssignable[GetType[m], GenericCallable])
455461
assert eval_typing(GetQuals[m]) == Literal["ClassVar"]
456462
assert eval_typing(GetDefiner[m]) != C # eval typing generates a new class
457463

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

470481

471482
def test_getmember_04():
@@ -790,15 +801,18 @@ def test_eval_getarg_callable_02():
790801
T = TypeVar("T")
791802

792803
# Params not wrapped
793-
f = Callable[[T], T]
804+
f = lambda T: Callable[[T], T]
794805
gc = GenericCallable[tuple[T], f]
795806
t = eval_typing(GetArg[gc, GenericCallable, Literal[0]])
796807
assert t == tuple[T]
797808
gc_f = eval_typing(GetArg[gc, GenericCallable, Literal[1]])
798809
assert gc_f == Never
799810

811+
with _ensure_context():
812+
assert gc.__args__[1](str) == Callable[[str], str]
813+
800814
# Params wrapped
801-
f = Callable[
815+
f = lambda T: Callable[
802816
[
803817
Param[Literal[None], T, Literal["positional"]],
804818
Param[Literal["y"], T],
@@ -815,6 +829,19 @@ def test_eval_getarg_callable_02():
815829
gc_f = eval_typing(GetArg[gc, GenericCallable, Literal[1]])
816830
assert gc_f == Never
817831

832+
with _ensure_context():
833+
assert (
834+
gc.__args__[1](str)
835+
== Callable[
836+
[
837+
Param[Literal[None], str, Literal["positional"]],
838+
Param[Literal["y"], str],
839+
Param[Literal["z"], str, Literal["keyword"]],
840+
],
841+
str,
842+
]
843+
)
844+
818845

819846
type IndirectProtocol[T] = NewProtocol[*[m for m in Iter[Members[T]]],]
820847
type GetMethodLike[T, Name] = GetArg[
@@ -963,6 +990,20 @@ def f[T](self, x: T, /, y: T, *, z: T) -> T: ...
963990
f = eval_typing(GetArg[gc, GenericCallable, Literal[1]])
964991
assert f is Never
965992

993+
with _ensure_context():
994+
assert (
995+
gc.__args__[1](str)
996+
== Callable[
997+
[
998+
Param[Literal["self"], C, Literal["positional"]],
999+
Param[Literal["x"], str, Literal["positional"]],
1000+
Param[Literal["y"], str],
1001+
Param[Literal["z"], str, Literal["keyword"]],
1002+
],
1003+
str,
1004+
]
1005+
)
1006+
9661007

9671008
def test_eval_getarg_callable_08():
9681009
# generic classmethod
@@ -977,6 +1018,20 @@ def f[T](cls, x: T, /, y: T, *, z: T) -> T: ...
9771018
f = eval_typing(GetArg[gc, GenericCallable, Literal[1]])
9781019
assert f is Never
9791020

1021+
with _ensure_context():
1022+
assert (
1023+
gc.__args__[1](str)
1024+
== classmethod[
1025+
C,
1026+
tuple[
1027+
Param[Literal["x"], str, Literal["positional"]],
1028+
Param[Literal["y"], str],
1029+
Param[Literal["z"], str, Literal["keyword"]],
1030+
],
1031+
str,
1032+
]
1033+
)
1034+
9801035

9811036
def test_eval_getarg_callable_09():
9821037
# generic staticmethod
@@ -991,6 +1046,19 @@ def f[T](x: T, /, y: T, *, z: T) -> T: ...
9911046
f = eval_typing(GetArg[gc, GenericCallable, Literal[1]])
9921047
assert f is Never
9931048

1049+
with _ensure_context():
1050+
assert (
1051+
gc.__args__[1](str)
1052+
== staticmethod[
1053+
tuple[
1054+
Param[Literal["x"], str, Literal["positional"]],
1055+
Param[Literal["y"], str],
1056+
Param[Literal["z"], str, Literal["keyword"]],
1057+
],
1058+
str,
1059+
]
1060+
)
1061+
9941062

9951063
def test_eval_getarg_tuple():
9961064
t = tuple[int, ...]

0 commit comments

Comments
 (0)