Skip to content

Commit 5535d13

Browse files
author
hauntsaninja
committed
anal: generalise TypeVarList, TypeVariableQuery
1 parent f43e25b commit 5535d13

File tree

2 files changed

+22
-21
lines changed

2 files changed

+22
-21
lines changed

mypy/semanal.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
from mypy.nodes import implicit_module_attrs
9898
from mypy.typeanal import (
9999
TypeAnalyser, analyze_type_alias, no_subscript_builtin_alias,
100-
TypeVariableQuery, TypeVarList, remove_dups, has_any_from_unimported_type,
100+
TypeVarLikeQuery, TypeVarLikeList, remove_dups, has_any_from_unimported_type,
101101
check_for_explicit_any, type_constructors, fix_instance_types
102102
)
103103
from mypy.exprtotype import expr_to_unanalyzed_type, TypeTranslationError
@@ -1212,7 +1212,7 @@ class Foo(Bar, Generic[T]): ...
12121212
Returns (remaining base expressions, inferred type variables, is protocol).
12131213
"""
12141214
removed = [] # type: List[int]
1215-
declared_tvars = [] # type: TypeVarList
1215+
declared_tvars = [] # type: TypeVarLikeList
12161216
is_protocol = False
12171217
for i, base_expr in enumerate(base_type_exprs):
12181218
self.analyze_type_expr(base_expr)
@@ -1266,7 +1266,7 @@ class Foo(Bar, Generic[T]): ...
12661266
tvar_defs.append(tvar_def)
12671267
return base_type_exprs, tvar_defs, is_protocol
12681268

1269-
def analyze_class_typevar_declaration(self, base: Type) -> Optional[Tuple[TypeVarList, bool]]:
1269+
def analyze_class_typevar_declaration(self, base: Type) -> Optional[Tuple[TypeVarLikeList, bool]]:
12701270
"""Analyze type variables declared using Generic[...] or Protocol[...].
12711271
12721272
Args:
@@ -1285,7 +1285,7 @@ def analyze_class_typevar_declaration(self, base: Type) -> Optional[Tuple[TypeVa
12851285
sym.node.fullname == 'typing.Protocol' and base.args or
12861286
sym.node.fullname == 'typing_extensions.Protocol' and base.args):
12871287
is_proto = sym.node.fullname != 'typing.Generic'
1288-
tvars = [] # type: TypeVarList
1288+
tvars = [] # type: TypeVarLikeList
12891289
for arg in unbound.args:
12901290
tag = self.track_incomplete_refs()
12911291
tvar = self.analyze_unbound_tvar(arg)
@@ -1315,17 +1315,17 @@ def analyze_unbound_tvar(self, t: Type) -> Optional[Tuple[str, TypeVarExpr]]:
13151315

13161316
def get_all_bases_tvars(self,
13171317
base_type_exprs: List[Expression],
1318-
removed: List[int]) -> TypeVarList:
1318+
removed: List[int]) -> TypeVarLikeList:
13191319
"""Return all type variable references in bases."""
1320-
tvars = [] # type: TypeVarList
1320+
tvars = [] # type: TypeVarLikeList
13211321
for i, base_expr in enumerate(base_type_exprs):
13221322
if i not in removed:
13231323
try:
13241324
base = expr_to_unanalyzed_type(base_expr)
13251325
except TypeTranslationError:
13261326
# This error will be caught later.
13271327
continue
1328-
base_tvars = base.accept(TypeVariableQuery(self.lookup_qualified, self.tvar_scope))
1328+
base_tvars = base.accept(TypeVarLikeQuery(self.lookup_qualified, self.tvar_scope))
13291329
tvars.extend(base_tvars)
13301330
return remove_dups(tvars)
13311331

@@ -2419,7 +2419,7 @@ def analyze_alias(self, rvalue: Expression,
24192419
typ = None # type: Optional[Type]
24202420
if res:
24212421
typ, depends_on = res
2422-
found_type_vars = typ.accept(TypeVariableQuery(self.lookup_qualified, self.tvar_scope))
2422+
found_type_vars = typ.accept(TypeVarLikeQuery(self.lookup_qualified, self.tvar_scope))
24232423
alias_tvars = [name for (name, node) in found_type_vars]
24242424
qualified_tvars = [node.fullname for (name, node) in found_type_vars]
24252425
else:

mypy/typeanal.py

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from mypy.nodes import (
2323
TypeInfo, Context, SymbolTableNode, Var, Expression,
2424
nongen_builtins, check_arg_names, check_arg_kinds, ARG_POS, ARG_NAMED,
25-
ARG_OPT, ARG_NAMED_OPT, ARG_STAR, ARG_STAR2, TypeVarExpr,
25+
ARG_OPT, ARG_NAMED_OPT, ARG_STAR, ARG_STAR2, TypeVarExpr, TypeVarLikeExpr,
2626
TypeAlias, PlaceholderNode, SYMBOL_FUNCBASE_TYPES, Decorator, MypyFile
2727
)
2828
from mypy.typetraverser import TypeTraverserVisitor
@@ -792,13 +792,14 @@ def tvar_scope_frame(self) -> Iterator[None]:
792792
self.tvar_scope = old_scope
793793

794794
def infer_type_variables(self,
795-
type: CallableType) -> List[Tuple[str, TypeVarExpr]]:
795+
type: CallableType) -> List[Tuple[str, TypeVarLikeExpr]]:
796796
"""Return list of unique type variables referred to in a callable."""
797797
names = [] # type: List[str]
798-
tvars = [] # type: List[TypeVarExpr]
798+
tvars = [] # type: List[TypeVarLikeExpr]
799799
for arg in type.arg_types:
800-
for name, tvar_expr in arg.accept(TypeVariableQuery(self.lookup_qualified,
801-
self.tvar_scope)):
800+
for name, tvar_expr in arg.accept(
801+
TypeVarLikeQuery(self.lookup_qualified, self.tvar_scope)
802+
):
802803
if name not in names:
803804
names.append(name)
804805
tvars.append(tvar_expr)
@@ -807,8 +808,8 @@ def infer_type_variables(self,
807808
# functions in the return type belong to those functions, not the
808809
# function we're currently analyzing.
809810
for name, tvar_expr in type.ret_type.accept(
810-
TypeVariableQuery(self.lookup_qualified, self.tvar_scope,
811-
include_callables=False)):
811+
TypeVarLikeQuery(self.lookup_qualified, self.tvar_scope, include_callables=False)
812+
):
812813
if name not in names:
813814
names.append(name)
814815
tvars.append(tvar_expr)
@@ -905,7 +906,7 @@ def tuple_type(self, items: List[Type]) -> TupleType:
905906
return TupleType(items, fallback=self.named_type('builtins.tuple', [any_type]))
906907

907908

908-
TypeVarList = List[Tuple[str, TypeVarExpr]]
909+
TypeVarLikeList = List[Tuple[str, TypeVarLikeExpr]]
909910

910911
# Mypyc doesn't support callback protocols yet.
911912
MsgCallback = Callable[[str, Context, DefaultNamedArg(Optional[ErrorCode], 'code')], None]
@@ -1066,7 +1067,7 @@ def flatten_tvars(ll: Iterable[List[T]]) -> List[T]:
10661067
return remove_dups(chain.from_iterable(ll))
10671068

10681069

1069-
class TypeVariableQuery(TypeQuery[TypeVarList]):
1070+
class TypeVarLikeQuery(TypeQuery[TypeVarLikeList]):
10701071

10711072
def __init__(self,
10721073
lookup: Callable[[str, Context], Optional[SymbolTableNode]],
@@ -1087,12 +1088,12 @@ def _seems_like_callable(self, type: UnboundType) -> bool:
10871088
return True
10881089
return False
10891090

1090-
def visit_unbound_type(self, t: UnboundType) -> TypeVarList:
1091+
def visit_unbound_type(self, t: UnboundType) -> TypeVarLikeList:
10911092
name = t.name
10921093
node = self.lookup(name, t)
1093-
if node and isinstance(node.node, TypeVarExpr) and (
1094+
if node and isinstance(node.node, TypeVarLikeExpr) and (
10941095
self.include_bound_tvars or self.scope.get_binding(node) is None):
1095-
assert isinstance(node.node, TypeVarExpr)
1096+
assert isinstance(node.node, TypeVarLikeExpr)
10961097
return [(name, node.node)]
10971098
elif not self.include_callables and self._seems_like_callable(t):
10981099
return []
@@ -1101,7 +1102,7 @@ def visit_unbound_type(self, t: UnboundType) -> TypeVarList:
11011102
else:
11021103
return super().visit_unbound_type(t)
11031104

1104-
def visit_callable_type(self, t: CallableType) -> TypeVarList:
1105+
def visit_callable_type(self, t: CallableType) -> TypeVarLikeList:
11051106
if self.include_callables:
11061107
return super().visit_callable_type(t)
11071108
else:

0 commit comments

Comments
 (0)