Skip to content

Commit 53e9e5f

Browse files
committed
Make the explicit one work again
1 parent 3f6ee47 commit 53e9e5f

2 files changed

Lines changed: 26 additions & 7 deletions

File tree

typemap/type_eval/_apply_generic.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ def get_local_defns(boxed: Boxed) -> tuple[dict[str, Any], dict[str, Any]]:
281281
return annos, dct
282282

283283

284-
def flatten_class(cls: type) -> type:
284+
def flatten_class_new_proto(cls: type) -> type:
285285
# This is a hacky version of flatten_class that works by using
286286
# NewProtocol on Members!
287287
#
@@ -311,8 +311,9 @@ def _type_repr(t: Any) -> str:
311311
return repr(t)
312312

313313

314-
# XXX: This is all dead now???
315-
def apply(
314+
# TODO: Potentially most of this could be ripped out. The internals
315+
# don't use this at all, it's only used by format_class.
316+
def _flatten_class_explicit(
316317
cls: type[Any], ctx: _eval_typing.EvalContext
317318
) -> type[_eval_typing._EvalProxy]:
318319
cls_boxed = box(cls)
@@ -349,7 +350,7 @@ def apply(
349350
"__local_args__": args,
350351
},
351352
)
352-
ctx.seen[boxed.alias_type()] = new[boxed] = cboxed
353+
new[boxed] = cboxed
353354

354355
annos: dict[str, Any] = {}
355356
dct: dict[str, Any] = {}
@@ -383,3 +384,11 @@ def apply(
383384
setattr(cboxed, k, _eval_typing._eval_types(v, ctx=ctx))
384385

385386
return new[cls_boxed]
387+
388+
389+
def flatten_class_explicit(obj: typing.Any):
390+
with _eval_typing._ensure_context() as ctx:
391+
return _flatten_class_explicit(obj, ctx)
392+
393+
394+
flatten_class = flatten_class_explicit

typemap/type_eval/_eval_typing.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,16 @@ def _is_type_alias_type(obj: typing.Any) -> bool:
187187
)
188188

189189

190+
def _apply_type(base, args):
191+
# Some type aliases (like Final) get mad if they get a 1-ary tuple...
192+
# TODO: Should we special case 0?
193+
# (Should we fill in Anys???)
194+
if len(args) == 1:
195+
return base[args[0]]
196+
else:
197+
return base[*args]
198+
199+
190200
def _eval_types(obj: typing.Any, ctx: EvalContext):
191201
# Found a recursive alias, we need to unwind it
192202
if obj in ctx.alias_stack:
@@ -285,7 +295,7 @@ def _eval_applied_type_alias(obj: types.GenericAlias, ctx: EvalContext):
285295
"""
286296
new_args = tuple(_eval_types(arg, ctx) for arg in obj.__args__)
287297

288-
new_obj = obj.__origin__[new_args] # type: ignore[index]
298+
new_obj = _apply_type(obj.__origin__, new_args)
289299
if isinstance(obj.__origin__, type):
290300
# This is a GenericAlias over a Python class, e.g. `dict[str, int]`
291301
# Let's reconstruct it by evaluating all arguments
@@ -326,7 +336,7 @@ def _eval_applied_class(obj: typing_GenericAlias, ctx: EvalContext):
326336
# return _eval_types(ret, ctx) # ???
327337
return ret
328338
else:
329-
return obj.__origin__[new_args] # type: ignore[index]
339+
return _apply_type(obj.__origin__, new_args)
330340

331341

332342
@_eval_types_impl.register
@@ -341,7 +351,7 @@ def _eval_ty_or_list(obj):
341351

342352
new_args = tuple(_eval_ty_or_list(arg) for arg in typing.get_args(obj))
343353
# origin for Callable is collections.abc.Callable which kind of annoying
344-
return typing.Callable[*new_args]
354+
return _apply_type(typing.Callable, new_args)
345355

346356

347357
@_eval_types_impl.register

0 commit comments

Comments
 (0)