49
49
50
50
from mypy .nodes import (
51
51
MypyFile , TypeInfo , Node , AssignmentStmt , FuncDef , OverloadedFuncDef ,
52
- ClassDef , Var , GDEF , MODULE_REF , FuncItem , Import ,
52
+ ClassDef , Var , GDEF , MODULE_REF , FuncItem , Import , Expression , Lvalue ,
53
53
ImportFrom , ImportAll , Block , LDEF , NameExpr , MemberExpr ,
54
54
IndexExpr , TupleExpr , ListExpr , ExpressionStmt , ReturnStmt ,
55
55
RaiseStmt , AssertStmt , OperatorAssignmentStmt , WhileStmt ,
@@ -405,7 +405,7 @@ def find_type_variables_in_type(
405
405
assert False , 'Unsupported type %s' % type
406
406
return result
407
407
408
- def is_defined_type_var (self , tvar : str , context : Node ) -> bool :
408
+ def is_defined_type_var (self , tvar : str , context : Context ) -> bool :
409
409
return self .lookup_qualified (tvar , context ).kind == BOUND_TVAR
410
410
411
411
def visit_overloaded_func_def (self , defn : OverloadedFuncDef ) -> None :
@@ -606,7 +606,7 @@ def unbind_class_type_vars(self) -> None:
606
606
if self .bound_tvars :
607
607
enable_typevars (self .bound_tvars )
608
608
609
- def analyze_class_decorator (self , defn : ClassDef , decorator : Node ) -> None :
609
+ def analyze_class_decorator (self , defn : ClassDef , decorator : Expression ) -> None :
610
610
decorator .accept (self )
611
611
612
612
def setup_is_builtinclass (self , defn : ClassDef ) -> None :
@@ -801,7 +801,7 @@ def analyze_base_classes(self, defn: ClassDef) -> None:
801
801
if info .mro and info .mro [- 1 ].fullname () != 'builtins.object' :
802
802
info .mro .append (self .object_type ().type )
803
803
804
- def expr_to_analyzed_type (self , expr : Node ) -> Type :
804
+ def expr_to_analyzed_type (self , expr : Expression ) -> Type :
805
805
if isinstance (expr , CallExpr ):
806
806
expr .accept (self )
807
807
info = self .check_namedtuple (expr )
@@ -1135,7 +1135,7 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
1135
1135
isinstance (s .rvalue , (ListExpr , TupleExpr ))):
1136
1136
self .add_exports (* s .rvalue .items )
1137
1137
1138
- def analyze_simple_literal_type (self , rvalue : Node ) -> Optional [Type ]:
1138
+ def analyze_simple_literal_type (self , rvalue : Expression ) -> Optional [Type ]:
1139
1139
"""Return builtins.int if rvalue is an int literal, etc."""
1140
1140
if self .weak_opts or self .options .semantic_analysis_only or self .function_stack :
1141
1141
# Skip this if any weak options are set.
@@ -1177,7 +1177,7 @@ def check_and_set_up_type_alias(self, s: AssignmentStmt) -> None:
1177
1177
# just an alias for the type.
1178
1178
self .globals [lvalue .name ].node = node
1179
1179
1180
- def analyze_lvalue (self , lval : Node , nested : bool = False ,
1180
+ def analyze_lvalue (self , lval : Lvalue , nested : bool = False ,
1181
1181
add_global : bool = False ,
1182
1182
explicit_type : bool = False ) -> None :
1183
1183
"""Analyze an lvalue or assignment target.
@@ -1300,11 +1300,11 @@ def is_self_member_ref(self, memberexpr: MemberExpr) -> bool:
1300
1300
node = memberexpr .expr .node
1301
1301
return isinstance (node , Var ) and node .is_self
1302
1302
1303
- def check_lvalue_validity (self , node : Node , ctx : Context ) -> None :
1303
+ def check_lvalue_validity (self , node : Expression , ctx : Context ) -> None :
1304
1304
if isinstance (node , (TypeInfo , TypeVarExpr )):
1305
1305
self .fail ('Invalid assignment target' , ctx )
1306
1306
1307
- def store_declared_types (self , lvalue : Node , typ : Type ) -> None :
1307
+ def store_declared_types (self , lvalue : Lvalue , typ : Type ) -> None :
1308
1308
if isinstance (typ , StarType ) and not isinstance (lvalue , StarExpr ):
1309
1309
self .fail ('Star type only allowed for starred expressions' , lvalue )
1310
1310
if isinstance (lvalue , RefExpr ):
@@ -1508,7 +1508,7 @@ def get_typevar_declaration(self, s: AssignmentStmt) -> Optional[CallExpr]:
1508
1508
return None
1509
1509
return call
1510
1510
1511
- def process_typevar_parameters (self , args : List [Node ],
1511
+ def process_typevar_parameters (self , args : List [Expression ],
1512
1512
names : List [Optional [str ]],
1513
1513
kinds : List [int ],
1514
1514
has_values : bool ,
@@ -1585,7 +1585,7 @@ def process_namedtuple_definition(self, s: AssignmentStmt) -> None:
1585
1585
# TODO call.analyzed
1586
1586
node .node = named_tuple
1587
1587
1588
- def check_namedtuple (self , node : Node , var_name : str = None ) -> TypeInfo :
1588
+ def check_namedtuple (self , node : Expression , var_name : str = None ) -> TypeInfo :
1589
1589
"""Check if a call defines a namedtuple.
1590
1590
1591
1591
The optional var_name argument is the name of the variable to
@@ -1665,7 +1665,7 @@ def parse_namedtuple_args(self, call: CallExpr,
1665
1665
+ ', ' .join (underscore ), call )
1666
1666
return items , types , ok
1667
1667
1668
- def parse_namedtuple_fields_with_types (self , nodes : List [Node ],
1668
+ def parse_namedtuple_fields_with_types (self , nodes : List [Expression ],
1669
1669
context : Context ) -> Tuple [List [str ], List [Type ], bool ]:
1670
1670
items = [] # type: List[str]
1671
1671
types = [] # type: List[Type]
@@ -1770,7 +1770,7 @@ def add_method(funcname: str, ret: Type, args: List[Argument], name=None,
1770
1770
def make_argument (self , name : str , type : Type ) -> Argument :
1771
1771
return Argument (Var (name ), type , None , ARG_POS )
1772
1772
1773
- def analyze_types (self , items : List [Node ]) -> List [Type ]:
1773
+ def analyze_types (self , items : List [Expression ]) -> List [Type ]:
1774
1774
result = [] # type: List[Type]
1775
1775
for node in items :
1776
1776
try :
@@ -1931,7 +1931,7 @@ def visit_del_stmt(self, s: DelStmt) -> None:
1931
1931
if not self .is_valid_del_target (s .expr ):
1932
1932
self .fail ('Invalid delete target' , s )
1933
1933
1934
- def is_valid_del_target (self , s : Node ) -> bool :
1934
+ def is_valid_del_target (self , s : Expression ) -> bool :
1935
1935
if isinstance (s , (IndexExpr , NameExpr , MemberExpr )):
1936
1936
return True
1937
1937
elif isinstance (s , TupleExpr ):
@@ -2502,7 +2502,7 @@ def add_local(self, node: Union[Var, FuncBase], ctx: Context) -> None:
2502
2502
node ._fullname = name
2503
2503
self .locals [- 1 ][name ] = SymbolTableNode (LDEF , node )
2504
2504
2505
- def add_exports (self , * exps : Node ) -> None :
2505
+ def add_exports (self , * exps : Expression ) -> None :
2506
2506
for exp in exps :
2507
2507
if isinstance (exp , StrExpr ):
2508
2508
self .all_exports .add (exp .value )
@@ -2753,7 +2753,7 @@ def visit_if_stmt(self, s: IfStmt) -> None:
2753
2753
def visit_try_stmt (self , s : TryStmt ) -> None :
2754
2754
self .sem .analyze_try_stmt (s , self , add_global = True )
2755
2755
2756
- def analyze_lvalue (self , lvalue : Node , explicit_type : bool = False ) -> None :
2756
+ def analyze_lvalue (self , lvalue : Lvalue , explicit_type : bool = False ) -> None :
2757
2757
self .sem .analyze_lvalue (lvalue , add_global = True , explicit_type = explicit_type )
2758
2758
2759
2759
@@ -2919,12 +2919,12 @@ def set_callable_name(sig: Type, fdef: FuncDef) -> Type:
2919
2919
return sig
2920
2920
2921
2921
2922
- def refers_to_fullname (node : Node , fullname : str ) -> bool :
2922
+ def refers_to_fullname (node : Expression , fullname : str ) -> bool :
2923
2923
"""Is node a name or member expression with the given full name?"""
2924
2924
return isinstance (node , RefExpr ) and node .fullname == fullname
2925
2925
2926
2926
2927
- def refers_to_class_or_function (node : Node ) -> bool :
2927
+ def refers_to_class_or_function (node : Expression ) -> bool :
2928
2928
"""Does semantically analyzed node refer to a class?"""
2929
2929
return (isinstance (node , RefExpr ) and
2930
2930
isinstance (node .node , (TypeInfo , FuncDef , OverloadedFuncDef )))
@@ -2997,7 +2997,7 @@ def infer_reachability_of_if_statement(s: IfStmt,
2997
2997
break
2998
2998
2999
2999
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 :
3001
3001
"""Infer whether if condition is always true/false.
3002
3002
3003
3003
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
3034
3034
return result
3035
3035
3036
3036
3037
- def consider_sys_version_info (expr : Node , pyversion : Tuple [int , ...]) -> int :
3037
+ def consider_sys_version_info (expr : Expression , pyversion : Tuple [int , ...]) -> int :
3038
3038
"""Consider whether expr is a comparison involving sys.version_info.
3039
3039
3040
3040
Return ALWAYS_TRUE, ALWAYS_FALSE, or TRUTH_VALUE_UNKNOWN.
@@ -3076,7 +3076,7 @@ def consider_sys_version_info(expr: Node, pyversion: Tuple[int, ...]) -> int:
3076
3076
return TRUTH_VALUE_UNKNOWN
3077
3077
3078
3078
3079
- def consider_sys_platform (expr : Node , platform : str ) -> int :
3079
+ def consider_sys_platform (expr : Expression , platform : str ) -> int :
3080
3080
"""Consider whether expr is a comparison involving sys.platform.
3081
3081
3082
3082
Return ALWAYS_TRUE, ALWAYS_FALSE, or TRUTH_VALUE_UNKNOWN.
@@ -3135,7 +3135,8 @@ def fixed_comparison(left: Targ, op: str, right: Targ) -> int:
3135
3135
return TRUTH_VALUE_UNKNOWN
3136
3136
3137
3137
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 , ...]]:
3139
3140
if isinstance (expr , IntExpr ):
3140
3141
return expr .value
3141
3142
if isinstance (expr , TupleExpr ):
@@ -3149,7 +3150,8 @@ def contains_int_or_tuple_of_ints(expr: Node) -> Union[None, int, Tuple[int], Tu
3149
3150
return None
3150
3151
3151
3152
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 ]]]:
3153
3155
if is_sys_attr (expr , 'version_info' ):
3154
3156
return (None , None ) # Same as sys.version_info[:]
3155
3157
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
3173
3175
return None
3174
3176
3175
3177
3176
- def is_sys_attr (expr : Node , name : str ) -> bool :
3178
+ def is_sys_attr (expr : Expression , name : str ) -> bool :
3177
3179
# TODO: This currently doesn't work with code like this:
3178
3180
# - import sys as _sys
3179
3181
# - from sys import version_info
@@ -3211,7 +3213,7 @@ def is_identity_signature(sig: Type) -> bool:
3211
3213
return False
3212
3214
3213
3215
3214
- def returns_any_if_called (expr : Node ) -> bool :
3216
+ def returns_any_if_called (expr : Expression ) -> bool :
3215
3217
"""Return True if we can predict that expr will return Any if called.
3216
3218
3217
3219
This only uses information available during semantic analysis so this
@@ -3234,7 +3236,7 @@ def returns_any_if_called(expr: Node) -> bool:
3234
3236
return False
3235
3237
3236
3238
3237
- def find_fixed_callable_return (expr : Node ) -> Optional [CallableType ]:
3239
+ def find_fixed_callable_return (expr : Expression ) -> Optional [CallableType ]:
3238
3240
if isinstance (expr , RefExpr ):
3239
3241
if isinstance (expr .node , FuncDef ):
3240
3242
typ = expr .node .type
0 commit comments