Skip to content

Commit cdc4938

Browse files
committed
Use the calling frame's name to produce a name for the NewProtocol
1 parent 60e02ed commit cdc4938

4 files changed

Lines changed: 24 additions & 7 deletions

File tree

tests/test_call.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +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?
2021
assert fmt == textwrap.dedent("""\
21-
class Protocol:
22+
class __annotate__:
2223
a: int
2324
b: int
2425
c: int

tests/test_type_dir.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ 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: the class should probably be named something like "AllOptional__T"
84+
# XXX: should the class name be further mangled to something like
85+
# "AllOptional__T"?
8586
# XXX: `DirProperties` skips methods, true to its name. Perhaps we just need
8687
# `Dir` that would iterate over everything
8788
assert format_helper.format_class(d) == textwrap.dedent("""\
88-
class Protocol:
89+
class AllOptional:
8990
last: int | typing.Literal[True] | None
9091
iii: str | int | typing.Literal['gotcha!'] | None
9192
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 Protocol:
55+
class MapRecursive:
5656
n: int | typing.Literal['gotcha!']
5757
m: str | typing.Literal['gotcha!']
5858
t: typing.Literal[False] | typing.Literal['gotcha!']
59-
a: abc.Protocol | typing.Literal['gotcha!']
59+
a: tests.test_type_eval.MapRecursive | typing.Literal['gotcha!']
6060
fff: int | typing.Literal['gotcha!']
6161
control: float
6262
""")

typemap/typing.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,24 @@ 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+
93+
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??
103+
104+
dct["__module__"] = module_name
105+
91106
mcls = type(typing.Protocol)
92-
# TODO: Replace the "Protocol" name with the type alias name
93-
return mcls("Protocol", (typing.Protocol,), dct)
107+
cls = mcls(name, (typing.Protocol,), dct)
108+
return cls
94109

95110

96111
class NewProtocol(metaclass=NewProtocolMeta):

0 commit comments

Comments
 (0)