Skip to content

Commit 5442926

Browse files
committed
Make _apply_generic return a type instead of a dict
1 parent b859937 commit 5442926

2 files changed

Lines changed: 25 additions & 23 deletions

File tree

typemap/type_eval/_apply_generic.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,26 @@ def make_func(
159159
return new_func
160160

161161

162-
def apply(cls: type[Any]) -> dict[str, Any]:
162+
def apply(
163+
cls: type[Any], ctx: _eval_typing.EvalContext
164+
) -> type[_eval_typing._EvalProxy]:
163165
mro_boxed = compute_mro(cls)
164166

165167
annos: dict[str, Any] = {}
166168
dct: dict[str, Any] = {}
167169

170+
# We create it early so we can add it to seen, to handle recursion
171+
ctx.seen[cls] = ret = type(
172+
cls.__name__,
173+
(_eval_typing._EvalProxy,),
174+
{
175+
"__module__": cls.__module__,
176+
"__name__": cls.__name__,
177+
"__origin__": cls,
178+
},
179+
)
180+
181+
# Run through the mro
168182
for boxed in reversed(mro_boxed):
169183
if af := getattr(boxed.cls, "__annotate__", None):
170184
# Class has annotations, let's resolve generic arguments
@@ -235,11 +249,15 @@ def apply(cls: type[Any]) -> dict[str, Any]:
235249
dct[name] = stuff
236250

237251
for k, v in annos.items():
238-
annos[k] = _eval_typing.eval_typing(v)
252+
annos[k] = _eval_typing._eval_types(v, ctx=ctx)
239253

240254
for k, v in dct.items():
241-
dct[k] = _eval_typing.eval_typing(v)
255+
dct[k] = _eval_typing._eval_types(v, ctx=ctx)
242256

243257
dct["__annotations__"] = annos
244258
dct["__generalized_mro__"] = mro_boxed
245-
return dct
259+
260+
for k, v in dct.items():
261+
setattr(ret, k, v)
262+
263+
return ret

typemap/type_eval/_eval_typing.py

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -256,29 +256,13 @@ def _eval_func(
256256
@_eval_types_impl.register
257257
def _eval_type_type(obj: type, ctx: EvalContext):
258258
if isinstance(obj, type) and issubclass(obj, typing.Generic):
259-
ret = type(
260-
obj.__name__,
261-
(_EvalProxy,),
262-
{
263-
"__module__": obj.__module__,
264-
"__name__": obj.__name__,
265-
"__origin__": obj,
266-
},
267-
)
268-
269-
# Need to add it to `seen` to handle recursion
270-
ctx.seen[obj] = ret
271259
try:
272-
ns = _apply_generic.apply(obj)
260+
return _apply_generic.apply(obj, ctx)
273261
except Exception:
274-
ctx.seen.pop(obj)
262+
# XXX: should apply handle this?
263+
ctx.seen.pop(obj, None)
275264
raise
276265

277-
for k, v in ns.items():
278-
setattr(ret, k, v)
279-
280-
return ret
281-
282266
return obj
283267

284268

0 commit comments

Comments
 (0)