Skip to content

Commit 631ae34

Browse files
authored
Make all the type operations get interpreted only by the evaluator (#9)
1 parent daf8630 commit 631ae34

8 files changed

Lines changed: 489 additions & 318 deletions

File tree

tests/test_qblike.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import textwrap
22

3+
from typing import Literal
4+
35
from typemap.type_eval import eval_call, eval_typing
46
from typemap.typing import (
57
NewProtocol,
@@ -106,10 +108,15 @@ def test_qblike_3():
106108
class select[...]:
107109
x: tests.test_qblike.Property[int]
108110
w: tests.test_qblike.Property[list[str]]
109-
z: tests.test_qblike.Link[PropsOnly[tests.test_qblike.Tgt]]
111+
z: tests.test_qblike.Link[PropsOnly[typemap.typing.GetArg[\
112+
tests.test_qblike.Link[tests.test_qblike.Tgt], tests.test_qblike.Link, 0]]]
110113
""")
114+
# z: tests.test_qblike.Link[PropsOnly[tests.test_qblike.Tgt]]
111115

112-
tgt = eval_typing(GetAttr[ret, "z"].__args__[0])
116+
res = eval_typing(GetAttr[ret, Literal["z"]])
117+
tgt = res.__args__[0]
118+
# XXX: this should probably be pre-evaluated already?
119+
tgt = eval_typing(tgt)
113120
fmt = format_helper.format_class(tgt)
114121

115122
assert fmt == textwrap.dedent("""\

tests/test_type_dir.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,9 @@ class NoLiterals2[tests.test_type_dir.Final]:
232232

233233
def test_type_dir_7():
234234
d = eval_typing(Members[Final])
235-
foo = next(iter(m for m in Iter[d] if m.__args__[0].__args__[0] == "foo"))
235+
foo = next(
236+
iter(m for m in d.__args__ if m.__args__[0].__args__[0] == "foo")
237+
)
236238
# XXX: drop self?
237239
assert (
238240
str(foo)

tests/test_type_eval.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,17 @@ def test_eval_types_1():
6969
def test_eval_types_2():
7070
evaled = eval_typing(MapRecursive[Recursive])
7171

72-
# Validate that recursion worked properly and "Recursive" was only walked once
73-
assert evaled.__annotations__["a"].__args__[0] is evaled
72+
# FIXME, or think about: this doesn't work, we currently evaluate it to an
73+
# *unexpanded* type alias.
74+
# # Validate that recursion worked properly and "Recursive" was only walked once
75+
# assert evaled.__annotations__["a"].__args__[0] is evaled
7476

7577
assert format_helper.format_class(evaled) == textwrap.dedent("""\
7678
class MapRecursive[tests.test_type_eval.Recursive]:
7779
n: int | typing.Literal['gotcha!']
7880
m: str | typing.Literal['gotcha!']
7981
t: typing.Literal[False] | typing.Literal['gotcha!']
80-
a: tests.test_type_eval.MapRecursive[tests.test_type_eval.Recursive] | typing.Literal['gotcha!']
82+
a: MapRecursive[tests.test_type_eval.Recursive] | typing.Literal['gotcha!']
8183
fff: int | typing.Literal['gotcha!']
8284
control: float
8385
""")
@@ -159,3 +161,12 @@ def test_type_strings_5():
159161
def test_type_strings_6():
160162
d = eval_typing(StrSlice[Literal["abcd"], Literal[1], Literal[None]])
161163
assert d == Literal["bcd"]
164+
165+
166+
def test_type_asdf():
167+
from typemap.typing import FromUnion
168+
169+
d = eval_typing(FromUnion[int | bool])
170+
arg = FromUnion[int | str]
171+
d = eval_typing(arg)
172+
assert d == tuple[int, str] or d == tuple[str, int]

typemap/type_eval/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
1+
from ._eval_typing import (
2+
eval_typing,
3+
_get_current_context,
4+
register_evaluator,
5+
_EvalProxy,
6+
)
7+
8+
# XXX: this needs to go second due to nasty circularity -- try to fix that!!
19
from ._eval_call import eval_call
2-
from ._eval_typing import eval_typing, _get_current_context, _EvalProxy
310
from ._subtype import issubtype
411
from ._subsim import issubsimilar
512

13+
# This one is imported for registering handlers
14+
from . import _eval_operators # noqa
15+
616

717
__all__ = (
818
"eval_typing",
19+
"register_evaluator",
920
"eval_call",
1021
"issubtype",
1122
"issubsimilar",

typemap/type_eval/_eval_call.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ def _eval_call(
2727
params = func.__type_params__
2828
for p in params:
2929
if hasattr(p, "__bound__") and p.__bound__ is next.CallSpec:
30-
vars[p.__name__] = next._CallSpecWrapper(args, kwargs, func)
30+
vars[p.__name__] = next._CallSpecWrapper(
31+
args, tuple(kwargs.items()), func
32+
)
3133
else:
3234
vars[p.__name__] = p
3335

0 commit comments

Comments
 (0)