Skip to content

Commit c8a9b52

Browse files
elazarggvanrossum
authored andcommitted
Tighten types for semanal (#2207)
1 parent 4a457fc commit c8a9b52

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

mypy/semanal.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
from mypy.nodes import (
5151
MypyFile, TypeInfo, Node, AssignmentStmt, FuncDef, OverloadedFuncDef,
52-
ClassDef, Var, GDEF, MODULE_REF, FuncItem, Import,
52+
ClassDef, Var, GDEF, MODULE_REF, FuncItem, Import, Expression, Lvalue,
5353
ImportFrom, ImportAll, Block, LDEF, NameExpr, MemberExpr,
5454
IndexExpr, TupleExpr, ListExpr, ExpressionStmt, ReturnStmt,
5555
RaiseStmt, AssertStmt, OperatorAssignmentStmt, WhileStmt,
@@ -405,7 +405,7 @@ def find_type_variables_in_type(
405405
assert False, 'Unsupported type %s' % type
406406
return result
407407

408-
def is_defined_type_var(self, tvar: str, context: Node) -> bool:
408+
def is_defined_type_var(self, tvar: str, context: Context) -> bool:
409409
return self.lookup_qualified(tvar, context).kind == BOUND_TVAR
410410

411411
def visit_overloaded_func_def(self, defn: OverloadedFuncDef) -> None:
@@ -606,7 +606,7 @@ def unbind_class_type_vars(self) -> None:
606606
if self.bound_tvars:
607607
enable_typevars(self.bound_tvars)
608608

609-
def analyze_class_decorator(self, defn: ClassDef, decorator: Node) -> None:
609+
def analyze_class_decorator(self, defn: ClassDef, decorator: Expression) -> None:
610610
decorator.accept(self)
611611

612612
def setup_is_builtinclass(self, defn: ClassDef) -> None:
@@ -801,7 +801,7 @@ def analyze_base_classes(self, defn: ClassDef) -> None:
801801
if info.mro and info.mro[-1].fullname() != 'builtins.object':
802802
info.mro.append(self.object_type().type)
803803

804-
def expr_to_analyzed_type(self, expr: Node) -> Type:
804+
def expr_to_analyzed_type(self, expr: Expression) -> Type:
805805
if isinstance(expr, CallExpr):
806806
expr.accept(self)
807807
info = self.check_namedtuple(expr)
@@ -1135,7 +1135,7 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
11351135
isinstance(s.rvalue, (ListExpr, TupleExpr))):
11361136
self.add_exports(*s.rvalue.items)
11371137

1138-
def analyze_simple_literal_type(self, rvalue: Node) -> Optional[Type]:
1138+
def analyze_simple_literal_type(self, rvalue: Expression) -> Optional[Type]:
11391139
"""Return builtins.int if rvalue is an int literal, etc."""
11401140
if self.weak_opts or self.options.semantic_analysis_only or self.function_stack:
11411141
# Skip this if any weak options are set.
@@ -1177,7 +1177,7 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> None:
11771177
# just an alias for the type.
11781178
self.globals[lvalue.name].node = node
11791179

1180-
def analyze_lvalue(self, lval: Node, nested: bool = False,
1180+
def analyze_lvalue(self, lval: Lvalue, nested: bool = False,
11811181
add_global: bool = False,
11821182
explicit_type: bool = False) -> None:
11831183
"""Analyze an lvalue or assignment target.
@@ -1300,11 +1300,11 @@ def is_self_member_ref(self, memberexpr: MemberExpr) -> bool:
13001300
node = memberexpr.expr.node
13011301
return isinstance(node, Var) and node.is_self
13021302

1303-
def check_lvalue_validity(self, node: Node, ctx: Context) -> None:
1303+
def check_lvalue_validity(self, node: Expression, ctx: Context) -> None:
13041304
if isinstance(node, (TypeInfo, TypeVarExpr)):
13051305
self.fail('Invalid assignment target', ctx)
13061306

1307-
def store_declared_types(self, lvalue: Node, typ: Type) -> None:
1307+
def store_declared_types(self, lvalue: Lvalue, typ: Type) -> None:
13081308
if isinstance(typ, StarType) and not isinstance(lvalue, StarExpr):
13091309
self.fail('Star type only allowed for starred expressions', lvalue)
13101310
if isinstance(lvalue, RefExpr):
@@ -1508,7 +1508,7 @@ def get_typevar_declaration(self, s: AssignmentStmt) -> Optional[CallExpr]:
15081508
return None
15091509
return call
15101510

1511-
def process_typevar_parameters(self, args: List[Node],
1511+
def process_typevar_parameters(self, args: List[Expression],
15121512
names: List[Optional[str]],
15131513
kinds: List[int],
15141514
has_values: bool,
@@ -1585,7 +1585,7 @@ def process_namedtuple_definition(self, s: AssignmentStmt) -> None:
15851585
# TODO call.analyzed
15861586
node.node = named_tuple
15871587

1588-
def check_namedtuple(self, node: Node, var_name: str = None) -> TypeInfo:
1588+
def check_namedtuple(self, node: Expression, var_name: str = None) -> TypeInfo:
15891589
"""Check if a call defines a namedtuple.
15901590
15911591
The optional var_name argument is the name of the variable to
@@ -1665,7 +1665,7 @@ def parse_namedtuple_args(self, call: CallExpr,
16651665
+ ', '.join(underscore), call)
16661666
return items, types, ok
16671667

1668-
def parse_namedtuple_fields_with_types(self, nodes: List[Node],
1668+
def parse_namedtuple_fields_with_types(self, nodes: List[Expression],
16691669
context: Context) -> Tuple[List[str], List[Type], bool]:
16701670
items = [] # type: List[str]
16711671
types = [] # type: List[Type]
@@ -1770,7 +1770,7 @@ def add_method(funcname: str, ret: Type, args: List[Argument], name=None,
17701770
def make_argument(self, name: str, type: Type) -> Argument:
17711771
return Argument(Var(name), type, None, ARG_POS)
17721772

1773-
def analyze_types(self, items: List[Node]) -> List[Type]:
1773+
def analyze_types(self, items: List[Expression]) -> List[Type]:
17741774
result = [] # type: List[Type]
17751775
for node in items:
17761776
try:
@@ -1931,7 +1931,7 @@ def visit_del_stmt(self, s: DelStmt) -> None:
19311931
if not self.is_valid_del_target(s.expr):
19321932
self.fail('Invalid delete target', s)
19331933

1934-
def is_valid_del_target(self, s: Node) -> bool:
1934+
def is_valid_del_target(self, s: Expression) -> bool:
19351935
if isinstance(s, (IndexExpr, NameExpr, MemberExpr)):
19361936
return True
19371937
elif isinstance(s, TupleExpr):
@@ -2502,7 +2502,7 @@ def add_local(self, node: Union[Var, FuncBase], ctx: Context) -> None:
25022502
node._fullname = name
25032503
self.locals[-1][name] = SymbolTableNode(LDEF, node)
25042504

2505-
def add_exports(self, *exps: Node) -> None:
2505+
def add_exports(self, *exps: Expression) -> None:
25062506
for exp in exps:
25072507
if isinstance(exp, StrExpr):
25082508
self.all_exports.add(exp.value)
@@ -2753,7 +2753,7 @@ def visit_if_stmt(self, s: IfStmt) -> None:
27532753
def visit_try_stmt(self, s: TryStmt) -> None:
27542754
self.sem.analyze_try_stmt(s, self, add_global=True)
27552755

2756-
def analyze_lvalue(self, lvalue: Node, explicit_type: bool = False) -> None:
2756+
def analyze_lvalue(self, lvalue: Lvalue, explicit_type: bool = False) -> None:
27572757
self.sem.analyze_lvalue(lvalue, add_global=True, explicit_type=explicit_type)
27582758

27592759

@@ -2919,12 +2919,12 @@ def set_callable_name(sig: Type, fdef: FuncDef) -> Type:
29192919
return sig
29202920

29212921

2922-
def refers_to_fullname(node: Node, fullname: str) -> bool:
2922+
def refers_to_fullname(node: Expression, fullname: str) -> bool:
29232923
"""Is node a name or member expression with the given full name?"""
29242924
return isinstance(node, RefExpr) and node.fullname == fullname
29252925

29262926

2927-
def refers_to_class_or_function(node: Node) -> bool:
2927+
def refers_to_class_or_function(node: Expression) -> bool:
29282928
"""Does semantically analyzed node refer to a class?"""
29292929
return (isinstance(node, RefExpr) and
29302930
isinstance(node.node, (TypeInfo, FuncDef, OverloadedFuncDef)))
@@ -2997,7 +2997,7 @@ def infer_reachability_of_if_statement(s: IfStmt,
29972997
break
29982998

29992999

3000-
def infer_if_condition_value(expr: Node, pyversion: Tuple[int, int], platform: str) -> int:
3000+
def infer_if_condition_value(expr: Expression, pyversion: Tuple[int, int], platform: str) -> int:
30013001
"""Infer whether if condition is always true/false.
30023002
30033003
Return ALWAYS_TRUE if always true, ALWAYS_FALSE if always false,
@@ -3034,7 +3034,7 @@ def infer_if_condition_value(expr: Node, pyversion: Tuple[int, int], platform: s
30343034
return result
30353035

30363036

3037-
def consider_sys_version_info(expr: Node, pyversion: Tuple[int, ...]) -> int:
3037+
def consider_sys_version_info(expr: Expression, pyversion: Tuple[int, ...]) -> int:
30383038
"""Consider whether expr is a comparison involving sys.version_info.
30393039
30403040
Return ALWAYS_TRUE, ALWAYS_FALSE, or TRUTH_VALUE_UNKNOWN.
@@ -3076,7 +3076,7 @@ def consider_sys_version_info(expr: Node, pyversion: Tuple[int, ...]) -> int:
30763076
return TRUTH_VALUE_UNKNOWN
30773077

30783078

3079-
def consider_sys_platform(expr: Node, platform: str) -> int:
3079+
def consider_sys_platform(expr: Expression, platform: str) -> int:
30803080
"""Consider whether expr is a comparison involving sys.platform.
30813081
30823082
Return ALWAYS_TRUE, ALWAYS_FALSE, or TRUTH_VALUE_UNKNOWN.
@@ -3135,7 +3135,8 @@ def fixed_comparison(left: Targ, op: str, right: Targ) -> int:
31353135
return TRUTH_VALUE_UNKNOWN
31363136

31373137

3138-
def contains_int_or_tuple_of_ints(expr: Node) -> Union[None, int, Tuple[int], Tuple[int, ...]]:
3138+
def contains_int_or_tuple_of_ints(expr: Expression
3139+
) -> Union[None, int, Tuple[int], Tuple[int, ...]]:
31393140
if isinstance(expr, IntExpr):
31403141
return expr.value
31413142
if isinstance(expr, TupleExpr):
@@ -3149,7 +3150,8 @@ def contains_int_or_tuple_of_ints(expr: Node) -> Union[None, int, Tuple[int], Tu
31493150
return None
31503151

31513152

3152-
def contains_sys_version_info(expr: Node) -> Union[None, int, Tuple[Optional[int], Optional[int]]]:
3153+
def contains_sys_version_info(expr: Expression
3154+
) -> Union[None, int, Tuple[Optional[int], Optional[int]]]:
31533155
if is_sys_attr(expr, 'version_info'):
31543156
return (None, None) # Same as sys.version_info[:]
31553157
if isinstance(expr, IndexExpr) and is_sys_attr(expr.base, 'version_info'):
@@ -3173,7 +3175,7 @@ def contains_sys_version_info(expr: Node) -> Union[None, int, Tuple[Optional[int
31733175
return None
31743176

31753177

3176-
def is_sys_attr(expr: Node, name: str) -> bool:
3178+
def is_sys_attr(expr: Expression, name: str) -> bool:
31773179
# TODO: This currently doesn't work with code like this:
31783180
# - import sys as _sys
31793181
# - from sys import version_info
@@ -3211,7 +3213,7 @@ def is_identity_signature(sig: Type) -> bool:
32113213
return False
32123214

32133215

3214-
def returns_any_if_called(expr: Node) -> bool:
3216+
def returns_any_if_called(expr: Expression) -> bool:
32153217
"""Return True if we can predict that expr will return Any if called.
32163218
32173219
This only uses information available during semantic analysis so this
@@ -3234,7 +3236,7 @@ def returns_any_if_called(expr: Node) -> bool:
32343236
return False
32353237

32363238

3237-
def find_fixed_callable_return(expr: Node) -> Optional[CallableType]:
3239+
def find_fixed_callable_return(expr: Expression) -> Optional[CallableType]:
32383240
if isinstance(expr, RefExpr):
32393241
if isinstance(expr.node, FuncDef):
32403242
typ = expr.node.type

0 commit comments

Comments
 (0)