Skip to content

Commit decc3d6

Browse files
committed
Try sticking it in the context instead
1 parent cdc4938 commit decc3d6

6 files changed

Lines changed: 25 additions & 24 deletions

File tree

tests/test_call.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ def test_call_1():
1717
ret = eval_call(func, a=1, b=2, c="aaa")
1818
fmt = format_helper.format_class(ret)
1919

20-
# XXX: Do we like this name?
20+
# XXX: We want better than this for a name
2121
assert fmt == textwrap.dedent("""\
22-
class __annotate__:
22+
class NewProtocol:
2323
a: int
2424
b: int
2525
c: int

tests/test_type_dir.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,10 @@ def sbase[Z](cls, a: int | Literal['gotcha!'] | Z | None, b: ~K) -> dict[str, in
8181
def test_type_dir_2():
8282
d = eval_typing(OptionalFinal)
8383

84-
# XXX: should the class name be further mangled to something like
85-
# "AllOptional__T"?
8684
# XXX: `DirProperties` skips methods, true to its name. Perhaps we just need
8785
# `Dir` that would iterate over everything
8886
assert format_helper.format_class(d) == textwrap.dedent("""\
89-
class AllOptional:
87+
class AllOptional[tests.test_type_dir.Final]:
9088
last: int | typing.Literal[True] | None
9189
iii: str | int | typing.Literal['gotcha!'] | None
9290
t: dict[str, str | int | typing.Literal['gotcha!']] | None

tests/test_type_eval.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ def test_eval_types_2():
5252
assert evaled.__annotations__["a"].__args__[0] is evaled
5353

5454
assert format_helper.format_class(evaled) == textwrap.dedent("""\
55-
class MapRecursive:
55+
class MapRecursive[tests.test_type_eval.Recursive]:
5656
n: int | typing.Literal['gotcha!']
5757
m: str | typing.Literal['gotcha!']
5858
t: typing.Literal[False] | typing.Literal['gotcha!']
59-
a: tests.test_type_eval.MapRecursive | typing.Literal['gotcha!']
59+
a: tests.test_type_eval.MapRecursive[tests.test_type_eval.Recursive] | typing.Literal['gotcha!']
6060
fff: int | typing.Literal['gotcha!']
6161
control: float
6262
""")

typemap/type_eval/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from ._eval_call import eval_call
2-
from ._eval_typing import eval_typing
2+
from ._eval_typing import eval_typing, _current_context
33

44

5-
__all__ = ("eval_typing", "eval_call")
5+
__all__ = ("eval_typing", "eval_call", "_current_context")
66
1

typemap/type_eval/_eval_typing.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
@dataclasses.dataclass
2222
class EvalContext:
2323
seen: dict[Any, Any]
24+
current_alias: types.GenericAlias | None = None
2425

2526

2627
# `eval_types()` calls can be nested, context must be preserved
@@ -131,15 +132,21 @@ def _eval_generic(obj: types.GenericAlias, ctx: EvalContext):
131132

132133
args = tuple(types.CellType(_eval_types(arg, ctx)) for arg in obj.__args__)
133134
mod = sys.modules[obj.__module__]
134-
ff = types.FunctionType(func.__code__, mod.__dict__, None, None, args)
135-
unpacked = ff(annotationlib.Format.VALUE)
136135

137-
ctx.seen[obj] = unpacked
136+
old_obj = ctx.current_alias
137+
ctx.current_alias = obj
138+
138139
try:
140+
ff = types.FunctionType(func.__code__, mod.__dict__, None, None, args)
141+
unpacked = ff(annotationlib.Format.VALUE)
142+
143+
ctx.seen[obj] = unpacked
139144
evaled = _eval_types(unpacked, ctx)
140145
except Exception:
141-
ctx.seen.pop(obj)
146+
ctx.seen.pop(obj, None)
142147
raise
148+
finally:
149+
ctx.current_alias = old_obj
143150

144151
return evaled
145152

typemap/typing.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,14 @@ def __getitem__(cls, val: list[Property]):
8888
dct = {}
8989
dct["__annotations__"] = {prop.name: prop.type for prop in val}
9090

91-
frame = inspect.currentframe()
92-
9391
module_name = __name__
94-
name = "Protocol"
95-
96-
# Peak at the calling frame and use that to produce
97-
# a better name than Protocol.
98-
# This is probably a 3.14 special?
99-
if frame and (prev := frame.f_back):
100-
name = prev.f_code.co_name
101-
module_name = inspect.getmodule(prev.f_code).__name__
102-
# qualname??
92+
name = "NewProtocol"
93+
94+
# If the type evaluation context
95+
ctx = type_eval._current_context.get()
96+
if ctx and ctx.current_alias:
97+
name = str(ctx.current_alias)
98+
module_name = ctx.current_alias.__module__
10399

104100
dct["__module__"] = module_name
105101

0 commit comments

Comments
 (0)