Skip to content

Commit b0a9102

Browse files
committed
Move Callable functionality to existing eval_call_with_types.
1 parent 52704ac commit b0a9102

4 files changed

Lines changed: 99 additions & 82 deletions

File tree

tests/test_qblike.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
from typing import Literal, Unpack
44

5-
from typemap.type_eval import eval_call, eval_typing
5+
from typemap.type_eval import (
6+
eval_call,
7+
eval_call_with_types,
8+
eval_typing,
9+
)
610
from typemap.typing import (
711
BaseTypedDict,
812
NewProtocol,
@@ -122,3 +126,21 @@ class select[...]:
122126
class PropsOnly[tests.test_qblike.Tgt]:
123127
name: tests.test_qblike.Property[str]
124128
""")
129+
130+
131+
def test_qblike_4():
132+
t = eval_call_with_types(
133+
select,
134+
A,
135+
x=bool,
136+
w=bool,
137+
z=bool,
138+
)
139+
fmt = format_helper.format_class(t)
140+
141+
assert fmt == textwrap.dedent("""\
142+
class select[...]:
143+
x: tests.test_qblike.Property[int]
144+
w: tests.test_qblike.Property[list[str]]
145+
z: tests.test_qblike.Link[tests.test_qblike.PropsOnly[tests.test_qblike.Tgt]]
146+
""")

tests/test_type_eval.py

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import pytest
1919

20-
from typemap.type_eval import eval_typing, eval_type_call
20+
from typemap.type_eval import eval_call_with_types, eval_typing
2121
from typemap.typing import (
2222
Attrs,
2323
FromUnion,
@@ -850,17 +850,17 @@ def test_type_eval_annotated_04():
850850

851851

852852
def test_type_call_callable_01():
853-
res = eval_type_call(Callable[[], int])
853+
res = eval_call_with_types(Callable[[], int])
854854
assert res is int
855855

856856

857857
def test_type_call_callable_02():
858-
res = eval_type_call(Callable[[Param[Literal["x"], int]], int], int)
858+
res = eval_call_with_types(Callable[[Param[Literal["x"], int]], int], int)
859859
assert res is int
860860

861861

862862
def test_type_call_callable_03():
863-
res = eval_type_call(
863+
res = eval_call_with_types(
864864
Callable[[Param[Literal["x"], int, Literal["keyword"]]], int], x=int
865865
)
866866
assert res is int
@@ -869,21 +869,21 @@ def test_type_call_callable_03():
869869
def test_type_call_callable_04():
870870
class C: ...
871871

872-
res = eval_type_call(Callable[[Param[Literal["self"], Self]], int], C)
872+
res = eval_call_with_types(Callable[[Param[Literal["self"], Self]], int], C)
873873
assert res is int
874874

875875

876876
def test_type_call_callable_05():
877877
class C: ...
878878

879-
res = eval_type_call(Callable[[Param[Literal["self"], Self]], C], C)
879+
res = eval_call_with_types(Callable[[Param[Literal["self"], Self]], C], C)
880880
assert res is C
881881

882882

883883
def test_type_call_callable_06():
884884
class C: ...
885885

886-
res = eval_type_call(
886+
res = eval_call_with_types(
887887
Callable[[Param[Literal["self"], Self], Param[Literal["x"], int]], int],
888888
C,
889889
int,
@@ -894,7 +894,7 @@ class C: ...
894894
def test_type_call_callable_07():
895895
class C: ...
896896

897-
res = eval_type_call(
897+
res = eval_call_with_types(
898898
Callable[
899899
[
900900
Param[Literal["self"], Self],
@@ -910,13 +910,13 @@ class C: ...
910910

911911
def test_type_call_callable_08():
912912
T = TypeVar("T")
913-
res = eval_type_call(Callable[[Param[Literal["x"], T]], str], int)
913+
res = eval_call_with_types(Callable[[Param[Literal["x"], T]], str], int)
914914
assert res is str
915915

916916

917917
def test_type_call_callable_09():
918918
T = TypeVar("T")
919-
res = eval_type_call(Callable[[Param[Literal["x"], T]], T], int)
919+
res = eval_call_with_types(Callable[[Param[Literal["x"], T]], T], int)
920920
assert res is int
921921

922922

@@ -925,7 +925,7 @@ def test_type_call_callable_10():
925925

926926
class C(Generic[T]): ...
927927

928-
res = eval_type_call(Callable[[Param[Literal["x"], C[T]]], T], C[int])
928+
res = eval_call_with_types(Callable[[Param[Literal["x"], C[T]]], T], C[int])
929929
assert res is int
930930

931931

@@ -938,30 +938,30 @@ class D(C[int]): ...
938938

939939
class E(D): ...
940940

941-
res = eval_type_call(Callable[[Param[Literal["x"], C[T]]], T], D)
941+
res = eval_call_with_types(Callable[[Param[Literal["x"], C[T]]], T], D)
942942
assert res is int
943-
res = eval_type_call(Callable[[Param[Literal["x"], C[T]]], T], E)
943+
res = eval_call_with_types(Callable[[Param[Literal["x"], C[T]]], T], E)
944944
assert res is int
945945

946946

947947
def test_type_call_local_function_01():
948948
def func(x: int) -> int: ...
949949

950-
res = eval_type_call(func, int)
950+
res = eval_call_with_types(func, int)
951951
assert res is int
952952

953953

954954
def test_type_call_local_function_02():
955955
def func(*, x: int) -> int: ...
956956

957-
res = eval_type_call(func, x=int)
957+
res = eval_call_with_types(func, x=int)
958958
assert res is int
959959

960960

961961
def test_type_call_local_function_03():
962962
def func[T](x: T) -> T: ...
963963

964-
res = eval_type_call(func, int)
964+
res = eval_call_with_types(func, int)
965965
assert res is int
966966

967967

@@ -970,7 +970,7 @@ class C: ...
970970

971971
def func(x: C) -> C: ...
972972

973-
res = eval_type_call(func, C)
973+
res = eval_call_with_types(func, C)
974974
assert res is C
975975

976976

@@ -979,7 +979,7 @@ class C: ...
979979

980980
def func[T](x: T) -> T: ...
981981

982-
res = eval_type_call(func, C)
982+
res = eval_call_with_types(func, C)
983983
assert res is C
984984

985985

@@ -990,7 +990,7 @@ class C(Generic[T]): ...
990990

991991
def func[U](x: C[U]) -> C[U]: ...
992992

993-
res = eval_type_call(func, C[int])
993+
res = eval_call_with_types(func, C[int])
994994
assert res == C[int]
995995

996996

@@ -1005,9 +1005,9 @@ class E(D): ...
10051005

10061006
def func[U](x: C[U]) -> U: ...
10071007

1008-
res = eval_type_call(func, D)
1008+
res = eval_call_with_types(func, D)
10091009
assert res is int
1010-
res = eval_type_call(func, E)
1010+
res = eval_call_with_types(func, E)
10111011
assert res is int
10121012

10131013

@@ -1022,7 +1022,7 @@ class F(D, E): ...
10221022

10231023
def func[U](x: C[U]) -> U: ...
10241024

1025-
res = eval_type_call(func, F)
1025+
res = eval_call_with_types(func, F)
10261026
assert res is int
10271027

10281028

@@ -1031,7 +1031,7 @@ class C[T, U]: ...
10311031

10321032
def func[V](x: C[int, V]) -> V: ...
10331033

1034-
res = eval_type_call(func, C[int, str])
1034+
res = eval_call_with_types(func, C[int, str])
10351035
assert res is str
10361036

10371037

@@ -1041,7 +1041,7 @@ def test_type_call_bind_error_01():
10411041
with pytest.raises(
10421042
ValueError, match="Type variable T is already bound to int, but got str"
10431043
):
1044-
eval_type_call(
1044+
eval_call_with_types(
10451045
Callable[[Param[Literal["x"], T], Param[Literal["y"], T]], T],
10461046
int,
10471047
str,
@@ -1054,7 +1054,7 @@ def func[T](x: T, y: T) -> T: ...
10541054
with pytest.raises(
10551055
ValueError, match="Type variable T is already bound to int, but got str"
10561056
):
1057-
eval_type_call(func, int, str)
1057+
eval_call_with_types(func, int, str)
10581058

10591059

10601060
def test_type_call_bind_error_03():
@@ -1065,7 +1065,7 @@ class C(Generic[T]): ...
10651065
with pytest.raises(
10661066
ValueError, match="Type variable T is already bound to int, but got str"
10671067
):
1068-
eval_type_call(
1068+
eval_call_with_types(
10691069
Callable[[Param[Literal["x"], C[T]], Param[Literal["y"], C[T]]], T],
10701070
C[int],
10711071
C[str],
@@ -1080,7 +1080,7 @@ def func[T](x: C[T], y: C[T]) -> T: ...
10801080
with pytest.raises(
10811081
ValueError, match="Type variable T is already bound to int, but got str"
10821082
):
1083-
eval_type_call(func, C[int], C[str])
1083+
eval_call_with_types(func, C[int], C[str])
10841084

10851085

10861086
def test_type_call_bind_error_05():
@@ -1091,7 +1091,7 @@ class D[T]: ...
10911091
def func[T](x: C[T]) -> T: ...
10921092

10931093
with pytest.raises(ValueError, match="Argument type mismatch for x"):
1094-
eval_type_call(func, D[int])
1094+
eval_call_with_types(func, D[int])
10951095

10961096

10971097
type GetCallableMember[T, N: str] = GetArg[
@@ -1112,23 +1112,23 @@ def test_type_call_member_01():
11121112
class C:
11131113
def invoke(self, x: int) -> int: ...
11141114

1115-
res = eval_type_call(GetCallableMember[C, Literal["invoke"]], C, int)
1115+
res = eval_call_with_types(GetCallableMember[C, Literal["invoke"]], C, int)
11161116
assert res is int
11171117

11181118

11191119
def test_type_call_member_02():
11201120
class C:
11211121
def invoke[T](self, x: T) -> T: ...
11221122

1123-
res = eval_type_call(GetCallableMember[C, Literal["invoke"]], C, int)
1123+
res = eval_call_with_types(GetCallableMember[C, Literal["invoke"]], C, int)
11241124
assert res is int
11251125

11261126

11271127
def test_type_call_member_03():
11281128
class C[T]:
11291129
def invoke(self, x: str) -> str: ...
11301130

1131-
res = eval_type_call(
1131+
res = eval_call_with_types(
11321132
GetCallableMember[C[int], Literal["invoke"]], C[int], str
11331133
)
11341134
assert res is str
@@ -1138,7 +1138,7 @@ def test_type_call_member_04():
11381138
class C[T]:
11391139
def invoke(self, x: T) -> T: ...
11401140

1141-
res = eval_type_call(
1141+
res = eval_call_with_types(
11421142
GetCallableMember[C[int], Literal["invoke"]], C[int], int
11431143
)
11441144
assert res is int
@@ -1148,15 +1148,17 @@ def test_type_call_member_05():
11481148
class C[T]:
11491149
def invoke(self) -> C[T]: ...
11501150

1151-
res = eval_type_call(GetCallableMember[C[int], Literal["invoke"]], C[int])
1151+
res = eval_call_with_types(
1152+
GetCallableMember[C[int], Literal["invoke"]], C[int]
1153+
)
11521154
assert res == C[int]
11531155

11541156

11551157
def test_type_call_member_06():
11561158
class C[T]:
11571159
def invoke[U](self, x: U) -> C[U]: ...
11581160

1159-
res = eval_type_call(
1161+
res = eval_call_with_types(
11601162
GetCallableMember[C[int], Literal["invoke"]], C[int], str
11611163
)
11621164
assert res == C[str]

typemap/type_eval/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from ._apply_generic import flatten_class
88

99
# XXX: this needs to go second due to nasty circularity -- try to fix that!!
10-
from ._eval_call import eval_call, eval_type_call
10+
from ._eval_call import eval_call, eval_call_with_types
1111
from ._subtype import issubtype
1212
from ._subsim import issubsimilar
1313

@@ -19,7 +19,7 @@
1919
"eval_typing",
2020
"register_evaluator",
2121
"eval_call",
22-
"eval_type_call",
22+
"eval_call_with_types",
2323
"flatten_class",
2424
"issubtype",
2525
"issubsimilar",

0 commit comments

Comments
 (0)