Skip to content

Commit ffd3957

Browse files
committed
Simplify class_callable
We no longer need to renumber generic class type variables into the function type variable id range, because the class/function type variable distinction is no longer used.
1 parent fe473ab commit ffd3957

File tree

2 files changed

+6
-48
lines changed

2 files changed

+6
-48
lines changed

mypy/checkmember.py

+4-46
Original file line numberDiff line numberDiff line change
@@ -414,57 +414,15 @@ def class_callable(init_type: CallableType, info: TypeInfo, type_type: Instance,
414414
special_sig: Optional[str]) -> CallableType:
415415
"""Create a type object type based on the signature of __init__."""
416416
variables = [] # type: List[TypeVarDef]
417-
for i, tvar in enumerate(info.defn.type_vars):
418-
variables.append(TypeVarDef(tvar.name, i + 1, tvar.values, tvar.upper_bound,
419-
tvar.variance))
420-
421-
class_variables = variables
422-
initvars = init_type.variables
423-
variables.extend(initvars)
417+
variables.extend(info.defn.type_vars)
418+
variables.extend(init_type.variables)
424419

425420
callable_type = init_type.copy_modified(
426421
ret_type=self_type(info), fallback=type_type, name=None, variables=variables,
427422
special_sig=special_sig)
428423
c = callable_type.with_name('"{}"'.format(info.name()))
429-
cc = convert_class_tvars_to_func_tvars(c, class_variables, len(initvars))
430-
cc.is_classmethod_class = True
431-
return cc
432-
433-
434-
def convert_class_tvars_to_func_tvars(callable: CallableType,
435-
class_variables: List[TypeVarDef],
436-
num_func_tvars: int) -> CallableType:
437-
tvar_def_translation = {} # type: Dict[TypeVarId, TypeVarDef]
438-
for v in class_variables:
439-
tvar_def_translation[v.id] = \
440-
TypeVarDef(v.name, -v.id.raw_id - num_func_tvars,
441-
v.values, v.upper_bound, v.variance)
442-
443-
return cast(CallableType, callable.accept(TvarTranslator(tvar_def_translation)))
444-
445-
446-
class TvarTranslator(TypeTranslator):
447-
def __init__(self, translation: Dict[TypeVarId, TypeVarDef]) -> None:
448-
super().__init__()
449-
self.translation = translation
450-
451-
def visit_type_var(self, t: TypeVarType) -> Type:
452-
if t.id in self.translation:
453-
return TypeVarType(self.translation[t.id])
454-
else:
455-
return t
456-
457-
def translate_variables(self,
458-
variables: List[TypeVarDef]) -> List[TypeVarDef]:
459-
if not variables:
460-
return variables
461-
items = [] # type: List[TypeVarDef]
462-
for v in variables:
463-
if v.id in self.translation:
464-
items.append(self.translation[v.id])
465-
else:
466-
items.append(v)
467-
return items
424+
c.is_classmethod_class = True
425+
return c
468426

469427

470428
def map_type_from_supertype(typ: Type, sub_info: TypeInfo,

test-data/unit/typexport-basic.test

+2-2
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ def f():
550550
class A(Generic[T]): pass
551551
[out]
552552
CallExpr(4) : Any
553-
NameExpr(4) : def [T] () -> A[T`-1]
553+
NameExpr(4) : def [T] () -> A[T`1]
554554

555555
[case testGenericCallInDynamicallyTypedFunction2]
556556
from typing import TypeVar, Generic
@@ -561,7 +561,7 @@ class A(Generic[T]):
561561
def __init__(self, x: T) -> None: pass
562562
[out]
563563
CallExpr(4) : Any
564-
NameExpr(4) : def [T] (x: T`-1) -> A[T`-1]
564+
NameExpr(4) : def [T] (x: T`1) -> A[T`1]
565565
NameExpr(4) : def () -> Any
566566

567567
[case testGenericCallInDynamicallyTypedFunction3]

0 commit comments

Comments
 (0)