Skip to content

Commit e368e57

Browse files
committed
Consolidate eval_getarg_callable tests.
1 parent 8cd5bc4 commit e368e57

1 file changed

Lines changed: 148 additions & 100 deletions

File tree

tests/test_type_eval.py

Lines changed: 148 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ def test_eval_getarg_callable_old():
415415
assert args == Any
416416

417417

418-
def test_eval_getarg_callable():
418+
def test_eval_getarg_callable_01():
419419
t = Callable[[int, str], str]
420420
args = eval_typing(GetArg[t, Callable, 0])
421421
assert (
@@ -447,6 +447,153 @@ def test_eval_getarg_callable():
447447
assert args == Any
448448

449449

450+
type IndirectProtocol[T] = NewProtocol[*[m for m in Iter[Members[T]]],]
451+
type GetMethodLike[T, Name] = GetArg[
452+
tuple[
453+
*[
454+
GetType[p]
455+
for p in Iter[Members[T]]
456+
if (
457+
Sub[GetType[p], Callable]
458+
or Sub[GetType[p], staticmethod]
459+
or Sub[GetType[p], classmethod]
460+
)
461+
and Sub[Name, GetName[p]]
462+
],
463+
],
464+
tuple,
465+
0,
466+
]
467+
468+
469+
def test_eval_getarg_callable_02a():
470+
class C:
471+
def f(self, x: int, /, y: int, *, z: int) -> int: ...
472+
473+
f = eval_typing(GetMethodLike[C, Literal["f"]])
474+
t = eval_typing(GetArg[f, Callable, 0])
475+
assert (
476+
t
477+
== tuple[
478+
Param[Literal["self"], C, Literal["positional"]],
479+
Param[Literal["x"], int, Literal["positional"]],
480+
Param[Literal["y"], int],
481+
Param[Literal["z"], int, Literal["keyword"]],
482+
]
483+
)
484+
t = eval_typing(GetArg[f, Callable, 1])
485+
assert t is int
486+
487+
488+
def test_eval_getarg_callable_02b():
489+
class C:
490+
def f(self, x: int, /, y: int, *, z: int) -> int: ...
491+
492+
f = eval_typing(GetMethodLike[IndirectProtocol[C], Literal["f"]])
493+
t = eval_typing(GetArg[f, Callable, 0])
494+
assert (
495+
t
496+
== tuple[
497+
Param[Literal["self"], Self, Literal["positional"]],
498+
Param[Literal["x"], int, Literal["positional"]],
499+
Param[Literal["y"], int],
500+
Param[Literal["z"], int, Literal["keyword"]],
501+
]
502+
)
503+
t = eval_typing(GetArg[f, Callable, 1])
504+
assert t is int
505+
506+
507+
def test_eval_getarg_callable_03a():
508+
class C:
509+
@classmethod
510+
def f(cls, x: int, /, y: int, *, z: int) -> int: ...
511+
512+
f = eval_typing(GetMethodLike[C, Literal["f"]])
513+
t = eval_typing(GetArg[f, classmethod, 0])
514+
assert t == C
515+
t = eval_typing(GetArg[f, classmethod, 1])
516+
assert (
517+
t
518+
== tuple[
519+
Param[Literal["x"], int, Literal["positional"]],
520+
Param[Literal["y"], int],
521+
Param[Literal["z"], int, Literal["keyword"]],
522+
]
523+
)
524+
t = eval_typing(GetArg[f, classmethod, 2])
525+
assert t is int
526+
527+
528+
def test_eval_getarg_callable_03b():
529+
class C:
530+
@classmethod
531+
def f(cls, x: int, /, y: int, *, z: int) -> int: ...
532+
533+
f = eval_typing(GetMethodLike[IndirectProtocol[C], Literal["f"]])
534+
t = eval_typing(GetArg[f, Callable, 0])
535+
assert (
536+
t
537+
== tuple[
538+
Param[Literal["cls"], type[C], Literal["positional"]],
539+
Param[Literal["x"], int, Literal["positional"]],
540+
Param[Literal["y"], int],
541+
Param[Literal["z"], int, Literal["keyword"]],
542+
]
543+
)
544+
t = eval_typing(GetArg[f, Callable, 1])
545+
assert t is int
546+
547+
548+
def test_eval_getarg_callable_04a():
549+
class C:
550+
@staticmethod
551+
def f(x: int, /, y: int, *, z: int) -> int: ...
552+
553+
f = eval_typing(GetMethodLike[C, Literal["f"]])
554+
t = eval_typing(GetArg[f, staticmethod, 0])
555+
assert (
556+
t
557+
== tuple[
558+
Param[Literal["x"], int, Literal["positional"]],
559+
Param[Literal["y"], int],
560+
Param[Literal["z"], int, Literal["keyword"]],
561+
]
562+
)
563+
t = eval_typing(GetArg[f, staticmethod, 1])
564+
assert t is int
565+
566+
567+
def test_eval_getarg_callable_04b():
568+
class C:
569+
@staticmethod
570+
def f(x: int, /, y: int, *, z: int) -> int: ...
571+
572+
f = eval_typing(GetMethodLike[IndirectProtocol[C], Literal["f"]])
573+
t = eval_typing(GetArg[f, Callable, 0])
574+
assert (
575+
t
576+
== tuple[
577+
Param[Literal["x"], int, Literal["positional"]],
578+
Param[Literal["y"], int],
579+
Param[Literal["z"], int, Literal["keyword"]],
580+
]
581+
)
582+
t = eval_typing(GetArg[f, Callable, 1])
583+
assert t is int
584+
585+
586+
def test_eval_getarg_callable_05():
587+
class C:
588+
f: Callable[[int], int]
589+
590+
f = eval_typing(GetMethodLike[IndirectProtocol[C], Literal["f"]])
591+
t = eval_typing(GetArg[f, Callable, 0])
592+
assert t == tuple[Param[Literal[None], int, Never],]
593+
t = eval_typing(GetArg[f, Callable, 1])
594+
assert t is int
595+
596+
450597
def test_eval_getarg_tuple():
451598
t = tuple[int, ...]
452599
args = eval_typing(GetArg[t, tuple, 1])
@@ -722,105 +869,6 @@ class Container2[T]: ...
722869
assert eval_typing(GetArg[t, Container, 1]) == Never
723870

724871

725-
def test_eval_getarg_callable_01():
726-
f = Callable[[int], int]
727-
t = eval_typing(GetArg[f, Callable, 0])
728-
assert t == tuple[Param[Literal[None], int, Never]]
729-
t = eval_typing(GetArg[f, Callable, 1])
730-
assert t is int
731-
732-
733-
type IndirectProtocol[T] = NewProtocol[*[m for m in Iter[Members[T]]],]
734-
type GetMethodLike[T, Name] = GetArg[
735-
tuple[
736-
*[
737-
GetType[p]
738-
for p in Iter[Members[T]]
739-
if (
740-
Sub[GetType[p], Callable]
741-
or Sub[GetType[p], staticmethod]
742-
or Sub[GetType[p], classmethod]
743-
)
744-
and Sub[Name, GetName[p]]
745-
],
746-
],
747-
tuple,
748-
0,
749-
]
750-
751-
752-
def test_eval_getarg_callable_02():
753-
class C:
754-
def f(self, x: int, /, y: int, *, z: int) -> int: ...
755-
756-
f = eval_typing(GetMethodLike[IndirectProtocol[C], Literal["f"]])
757-
t = eval_typing(GetArg[f, Callable, 0])
758-
assert (
759-
t
760-
== tuple[
761-
Param[Literal["self"], Self, Literal["positional"]],
762-
Param[Literal["x"], int, Literal["positional"]],
763-
Param[Literal["y"], int],
764-
Param[Literal["z"], int, Literal["keyword"]],
765-
]
766-
)
767-
t = eval_typing(GetArg[f, Callable, 1])
768-
assert t is int
769-
770-
771-
type MembersAreCallable[T] = NewProtocol[
772-
*[m for m in Iter[Members[T]] if Sub[GetType[m], classmethod]]
773-
]
774-
775-
776-
def test_eval_getarg_callable_03():
777-
class C:
778-
@classmethod
779-
def f(cls, x: int, /, y: int, *, z: int) -> int: ...
780-
781-
f = eval_typing(GetMethodLike[IndirectProtocol[C], Literal["f"]])
782-
t = eval_typing(GetArg[f, Callable, 0])
783-
assert (
784-
t
785-
== tuple[
786-
Param[Literal["cls"], type[C], Literal["positional"]],
787-
Param[Literal["x"], int, Literal["positional"]],
788-
Param[Literal["y"], int],
789-
Param[Literal["z"], int, Literal["keyword"]],
790-
]
791-
)
792-
793-
794-
def test_eval_getarg_callable_04():
795-
class C:
796-
@staticmethod
797-
def f(x: int, /, y: int, *, z: int) -> int: ...
798-
799-
f = eval_typing(GetMethodLike[IndirectProtocol[C], Literal["f"]])
800-
t = eval_typing(GetArg[f, Callable, 0])
801-
assert (
802-
t
803-
== tuple[
804-
Param[Literal["x"], int, Literal["positional"]],
805-
Param[Literal["y"], int],
806-
Param[Literal["z"], int, Literal["keyword"]],
807-
]
808-
)
809-
t = eval_typing(GetArg[f, Callable, 1])
810-
assert t is int
811-
812-
813-
def test_eval_getarg_callable_05():
814-
class C:
815-
f: Callable[[int], int]
816-
817-
f = eval_typing(GetMethodLike[IndirectProtocol[C], Literal["f"]])
818-
t = eval_typing(GetArg[f, Callable, 0])
819-
assert t == tuple[Param[Literal[None], int, Never],]
820-
t = eval_typing(GetArg[f, Callable, 1])
821-
assert t is int
822-
823-
824872
type _Works[Ts, I] = Literal[True]
825873
type Works[Ts] = _Works[Ts, Length[Ts]]
826874

0 commit comments

Comments
 (0)