@@ -297,29 +297,26 @@ def check_first_pass(self) -> None:
297
297
self .recurse_into_functions = True
298
298
with state .strict_optional_set (self .options .strict_optional ):
299
299
self .errors .set_file (self .path , self .tree .fullname , scope = self .tscope )
300
- self .tscope .enter_file (self .tree .fullname )
301
- with self .enter_partial_types ():
302
- with self .binder .top_frame_context ():
300
+ with self .tscope .module_scope (self .tree .fullname ):
301
+ with self .enter_partial_types (), self .binder .top_frame_context ():
303
302
for d in self .tree .defs :
304
303
self .accept (d )
305
304
306
- assert not self .current_node_deferred
305
+ assert not self .current_node_deferred
307
306
308
- all_ = self .globals .get ('__all__' )
309
- if all_ is not None and all_ .type is not None :
310
- all_node = all_ .node
311
- assert all_node is not None
312
- seq_str = self .named_generic_type ('typing.Sequence' ,
313
- [self .named_type ('builtins.str' )])
314
- if self .options .python_version [0 ] < 3 :
307
+ all_ = self .globals .get ('__all__' )
308
+ if all_ is not None and all_ .type is not None :
309
+ all_node = all_ .node
310
+ assert all_node is not None
315
311
seq_str = self .named_generic_type ('typing.Sequence' ,
316
- [self .named_type ('builtins.unicode' )])
317
- if not is_subtype (all_ .type , seq_str ):
318
- str_seq_s , all_s = format_type_distinctly (seq_str , all_ .type )
319
- self .fail (message_registry .ALL_MUST_BE_SEQ_STR .format (str_seq_s , all_s ),
320
- all_node )
321
-
322
- self .tscope .leave ()
312
+ [self .named_type ('builtins.str' )])
313
+ if self .options .python_version [0 ] < 3 :
314
+ seq_str = self .named_generic_type ('typing.Sequence' ,
315
+ [self .named_type ('builtins.unicode' )])
316
+ if not is_subtype (all_ .type , seq_str ):
317
+ str_seq_s , all_s = format_type_distinctly (seq_str , all_ .type )
318
+ self .fail (message_registry .ALL_MUST_BE_SEQ_STR .format (str_seq_s , all_s ),
319
+ all_node )
323
320
324
321
def check_second_pass (self ,
325
322
todo : Optional [Sequence [Union [DeferredNode ,
@@ -334,25 +331,24 @@ def check_second_pass(self,
334
331
if not todo and not self .deferred_nodes :
335
332
return False
336
333
self .errors .set_file (self .path , self .tree .fullname , scope = self .tscope )
337
- self .tscope .enter_file (self .tree .fullname )
338
- self .pass_num += 1
339
- if not todo :
340
- todo = self .deferred_nodes
341
- else :
342
- assert not self .deferred_nodes
343
- self .deferred_nodes = []
344
- done : Set [Union [DeferredNodeType , FineGrainedDeferredNodeType ]] = set ()
345
- for node , active_typeinfo in todo :
346
- if node in done :
347
- continue
348
- # This is useful for debugging:
349
- # print("XXX in pass %d, class %s, function %s" %
350
- # (self.pass_num, type_name, node.fullname or node.name))
351
- done .add (node )
352
- with self .tscope .class_scope (active_typeinfo ) if active_typeinfo else nothing ():
353
- with self .scope .push_class (active_typeinfo ) if active_typeinfo else nothing ():
354
- self .check_partial (node )
355
- self .tscope .leave ()
334
+ with self .tscope .module_scope (self .tree .fullname ):
335
+ self .pass_num += 1
336
+ if not todo :
337
+ todo = self .deferred_nodes
338
+ else :
339
+ assert not self .deferred_nodes
340
+ self .deferred_nodes = []
341
+ done : Set [Union [DeferredNodeType , FineGrainedDeferredNodeType ]] = set ()
342
+ for node , active_typeinfo in todo :
343
+ if node in done :
344
+ continue
345
+ # This is useful for debugging:
346
+ # print("XXX in pass %d, class %s, function %s" %
347
+ # (self.pass_num, type_name, node.fullname or node.name))
348
+ done .add (node )
349
+ with self .tscope .class_scope (active_typeinfo ) if active_typeinfo else nothing ():
350
+ with self .scope .push_class (active_typeinfo ) if active_typeinfo else nothing ():
351
+ self .check_partial (node )
356
352
return True
357
353
358
354
def check_partial (self , node : Union [DeferredNodeType , FineGrainedDeferredNodeType ]) -> None :
@@ -874,7 +870,7 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: Optional[str])
874
870
if isinstance (typ .ret_type , TypeVarType ):
875
871
if typ .ret_type .variance == CONTRAVARIANT :
876
872
self .fail (message_registry .RETURN_TYPE_CANNOT_BE_CONTRAVARIANT ,
877
- typ .ret_type )
873
+ typ .ret_type )
878
874
879
875
# Check that Generator functions have the appropriate return type.
880
876
if defn .is_generator :
@@ -992,7 +988,7 @@ def check_func_def(self, defn: FuncItem, typ: CallableType, name: Optional[str])
992
988
self .accept (item .body )
993
989
unreachable = self .binder .is_unreachable ()
994
990
995
- if ( self .options .warn_no_return and not unreachable ) :
991
+ if self .options .warn_no_return and not unreachable :
996
992
if (defn .is_generator or
997
993
is_named_instance (self .return_types [- 1 ], 'typing.AwaitableGenerator' )):
998
994
return_type = self .get_generator_return_type (self .return_types [- 1 ],
@@ -1083,7 +1079,7 @@ def is_unannotated_any(t: Type) -> bool:
1083
1079
code = codes .NO_UNTYPED_DEF )
1084
1080
elif fdef .is_generator :
1085
1081
if is_unannotated_any (self .get_generator_return_type (ret_type ,
1086
- fdef .is_coroutine )):
1082
+ fdef .is_coroutine )):
1087
1083
self .fail (message_registry .RETURN_TYPE_EXPECTED , fdef ,
1088
1084
code = codes .NO_UNTYPED_DEF )
1089
1085
elif fdef .is_coroutine and isinstance (ret_type , Instance ):
@@ -2641,8 +2637,7 @@ def check_rvalue_count_in_assignment(self, lvalues: List[Lvalue], rvalue_count:
2641
2637
len (lvalues ) - 1 , context )
2642
2638
return False
2643
2639
elif rvalue_count != len (lvalues ):
2644
- self .msg .wrong_number_values_to_unpack (rvalue_count ,
2645
- len (lvalues ), context )
2640
+ self .msg .wrong_number_values_to_unpack (rvalue_count , len (lvalues ), context )
2646
2641
return False
2647
2642
return True
2648
2643
@@ -2896,8 +2891,7 @@ def check_lvalue(self, lvalue: Lvalue) -> Tuple[Optional[Type],
2896
2891
elif isinstance (lvalue , IndexExpr ):
2897
2892
index_lvalue = lvalue
2898
2893
elif isinstance (lvalue , MemberExpr ):
2899
- lvalue_type = self .expr_checker .analyze_ordinary_member_access (lvalue ,
2900
- True )
2894
+ lvalue_type = self .expr_checker .analyze_ordinary_member_access (lvalue , True )
2901
2895
self .store_type (lvalue , lvalue_type )
2902
2896
elif isinstance (lvalue , NameExpr ):
2903
2897
lvalue_type = self .expr_checker .analyze_ref_expr (lvalue , lvalue = True )
@@ -4144,6 +4138,7 @@ def is_type_call(expr: CallExpr) -> bool:
4144
4138
"""Is expr a call to type with one argument?"""
4145
4139
return (refers_to_fullname (expr .callee , 'builtins.type' )
4146
4140
and len (expr .args ) == 1 )
4141
+
4147
4142
# exprs that are being passed into type
4148
4143
exprs_in_type_calls : List [Expression ] = []
4149
4144
# type that is being compared to type(expr)
@@ -4194,6 +4189,7 @@ def combine_maps(list_maps: List[TypeMap]) -> TypeMap:
4194
4189
if d is not None :
4195
4190
result_map .update (d )
4196
4191
return result_map
4192
+
4197
4193
if_map = combine_maps (if_maps )
4198
4194
# type(x) == T is only true when x has the same type as T, meaning
4199
4195
# that it can be false if x is an instance of a subclass of T. That means
0 commit comments