@@ -99,8 +99,6 @@ class TypeChecker(NodeVisitor[Type]):
9999 dynamic_funcs = None # type: List[bool]
100100 # Stack of functions being type checked
101101 function_stack = None # type: List[FuncItem]
102- # Do weak type checking in this file
103- weak_opts = set () # type: Set[str]
104102 # Stack of collections of variables with partial types
105103 partial_types = None # type: List[Dict[Var, Context]]
106104 globals = None # type: SymbolTable
@@ -139,7 +137,6 @@ def __init__(self, errors: Errors, modules: Dict[str, MypyFile]) -> None:
139137 self .type_context = []
140138 self .dynamic_funcs = []
141139 self .function_stack = []
142- self .weak_opts = set () # type: Set[str]
143140 self .partial_types = []
144141 self .deferred_nodes = []
145142 self .pass_num = 0
@@ -153,7 +150,6 @@ def visit_file(self, file_node: MypyFile, path: str, options: Options) -> None:
153150 self .is_stub = file_node .is_stub
154151 self .errors .set_file (path )
155152 self .globals = file_node .names
156- self .weak_opts = file_node .weak_opts
157153 self .enter_partial_types ()
158154 self .is_typeshed_stub = self .errors .is_typeshed_file (path )
159155 self .module_type_map = {}
@@ -222,7 +218,7 @@ def accept(self, node: Node, type_context: Type = None) -> Type:
222218 report_internal_error (err , self .errors .file , node .line , self .errors , self .options )
223219 self .type_context .pop ()
224220 self .store_type (node , typ )
225- if self .typing_mode_none ():
221+ if not self .in_checked_function ():
226222 return AnyType ()
227223 else :
228224 return typ
@@ -1086,7 +1082,7 @@ def check_assignment(self, lvalue: Lvalue, rvalue: Expression, infer_lvalue_type
10861082 self .binder .assign_type (lvalue ,
10871083 rvalue_type ,
10881084 lvalue_type ,
1089- self . typing_mode_weak () )
1085+ False )
10901086 elif index_lvalue :
10911087 self .check_indexed_assignment (index_lvalue , rvalue , rvalue )
10921088
@@ -1315,10 +1311,7 @@ def is_definition(self, s: Lvalue) -> bool:
13151311 def infer_variable_type (self , name : Var , lvalue : Lvalue ,
13161312 init_type : Type , context : Context ) -> None :
13171313 """Infer the type of initialized variables from initializer type."""
1318- if self .typing_mode_weak ():
1319- self .set_inferred_type (name , lvalue , AnyType ())
1320- self .binder .assign_type (lvalue , init_type , self .binder .get_declaration (lvalue ), True )
1321- elif self .is_unusable_type (init_type ):
1314+ if self .is_unusable_type (init_type ):
13221315 self .check_usable_type (init_type , context )
13231316 self .set_inference_error_fallback_type (name , lvalue , init_type , context )
13241317 elif isinstance (init_type , DeletedType ):
@@ -1403,8 +1396,6 @@ def check_simple_assignment(self, lvalue_type: Type, rvalue: Expression,
14031396 rvalue_type = self .accept (rvalue , lvalue_type )
14041397 if isinstance (rvalue_type , DeletedType ):
14051398 self .msg .deleted_as_rvalue (rvalue_type , context )
1406- if self .typing_mode_weak ():
1407- return rvalue_type
14081399 if isinstance (lvalue_type , DeletedType ):
14091400 self .msg .deleted_as_lvalue (lvalue_type , context )
14101401 else :
@@ -1499,7 +1490,7 @@ def visit_return_stmt(self, s: ReturnStmt) -> Type:
14991490 if isinstance (return_type , (Void , NoneTyp , AnyType )):
15001491 return None
15011492
1502- if self .typing_mode_full ():
1493+ if self .in_checked_function ():
15031494 self .fail (messages .RETURN_VALUE_EXPECTED , s )
15041495
15051496 def wrap_generic_type (self , typ : Instance , rtyp : Instance , check_type :
@@ -1534,10 +1525,7 @@ def visit_if_stmt(self, s: IfStmt) -> Type:
15341525 for e , b in zip (s .expr , s .body ):
15351526 t = self .accept (e )
15361527 self .check_usable_type (t , e )
1537- if_map , else_map = find_isinstance_check (
1538- e , self .type_map ,
1539- self .typing_mode_weak ()
1540- )
1528+ if_map , else_map = find_isinstance_check (e , self .type_map )
15411529 if if_map is None :
15421530 # The condition is always false
15431531 # XXX should issue a warning?
@@ -1593,10 +1581,7 @@ def visit_assert_stmt(self, s: AssertStmt) -> Type:
15931581 self .accept (s .expr )
15941582
15951583 # If this is asserting some isinstance check, bind that type in the following code
1596- true_map , _ = find_isinstance_check (
1597- s .expr , self .type_map ,
1598- self .typing_mode_weak ()
1599- )
1584+ true_map , _ = find_isinstance_check (s .expr , self .type_map )
16001585
16011586 if true_map :
16021587 for var , type in true_map .items ():
@@ -1818,7 +1803,7 @@ def flatten(t: Expression) -> List[Expression]:
18181803 self .binder .assign_type (elt ,
18191804 DeletedType (source = elt .name ),
18201805 self .binder .get_declaration (elt ),
1821- self . typing_mode_weak () )
1806+ False )
18221807 return None
18231808
18241809 def visit_decorator (self , e : Decorator ) -> Type :
@@ -2113,7 +2098,7 @@ def visit_yield_expr(self, e: YieldExpr) -> Type:
21132098 expected_item_type = self .get_generator_yield_type (return_type , False )
21142099 if e .expr is None :
21152100 if (not isinstance (expected_item_type , (Void , NoneTyp , AnyType ))
2116- and self .typing_mode_full ()):
2101+ and self .in_checked_function ()):
21172102 self .fail (messages .YIELD_VALUE_EXPECTED , e )
21182103 else :
21192104 actual_item_type = self .accept (e .expr , expected_item_type )
@@ -2224,32 +2209,17 @@ def store_type(self, node: Node, typ: Type) -> None:
22242209 if typ is not None :
22252210 self .module_type_map [node ] = typ
22262211
2227- def typing_mode_none (self ) -> bool :
2228- if self .is_dynamic_function () and not self .options .check_untyped_defs :
2229- return not self .weak_opts
2230- elif self .function_stack :
2231- return False
2232- else :
2233- return False
2234-
2235- def typing_mode_weak (self ) -> bool :
2236- if self .is_dynamic_function () and not self .options .check_untyped_defs :
2237- return bool (self .weak_opts )
2238- elif self .function_stack :
2239- return False
2240- else :
2241- return 'global' in self .weak_opts
2212+ def in_checked_function (self ) -> bool :
2213+ """Should we type-check the current function?
22422214
2243- def typing_mode_full (self ) -> bool :
2244- if self .is_dynamic_function () and not self .options .check_untyped_defs :
2245- return False
2246- elif self .function_stack :
2247- return True
2248- else :
2249- return 'global' not in self .weak_opts
2250-
2251- def is_dynamic_function (self ) -> bool :
2252- return len (self .dynamic_funcs ) > 0 and self .dynamic_funcs [- 1 ]
2215+ - Yes if --check-untyped-defs is set.
2216+ - Yes outside functions.
2217+ - Yes in annotated functions.
2218+ - No otherwise.
2219+ """
2220+ return (self .options .check_untyped_defs
2221+ or not self .dynamic_funcs
2222+ or not self .dynamic_funcs [- 1 ])
22532223
22542224 def lookup (self , name : str , kind : int ) -> SymbolTableNode :
22552225 """Look up a definition from the symbol table with the given name.
@@ -2374,8 +2344,6 @@ def method_type(self, func: FuncBase) -> FunctionLike:
23742344def conditional_type_map (expr : Expression ,
23752345 current_type : Optional [Type ],
23762346 proposed_type : Optional [Type ],
2377- * ,
2378- weak : bool = False
23792347 ) -> Tuple [TypeMap , TypeMap ]:
23802348 """Takes in an expression, the current type of the expression, and a
23812349 proposed type of that expression.
@@ -2397,10 +2365,7 @@ def conditional_type_map(expr: Expression,
23972365 return {expr : proposed_type }, {}
23982366 else :
23992367 # An isinstance check, but we don't understand the type
2400- if weak :
2401- return {expr : AnyType ()}, {expr : current_type }
2402- else :
2403- return {}, {}
2368+ return {}, {}
24042369
24052370
24062371def is_literal_none (n : Expression ) -> bool :
@@ -2453,7 +2418,6 @@ def or_conditional_maps(m1: TypeMap, m2: TypeMap) -> TypeMap:
24532418
24542419def find_isinstance_check (node : Expression ,
24552420 type_map : Dict [Node , Type ],
2456- weak : bool = False
24572421 ) -> Tuple [TypeMap , TypeMap ]:
24582422 """Find any isinstance checks (within a chain of ands). Includes
24592423 implicit and explicit checks for None.
@@ -2472,7 +2436,7 @@ def find_isinstance_check(node: Expression,
24722436 if expr .literal == LITERAL_TYPE :
24732437 vartype = type_map [expr ]
24742438 type = get_isinstance_type (node .args [1 ], type_map )
2475- return conditional_type_map (expr , vartype , type , weak = weak )
2439+ return conditional_type_map (expr , vartype , type )
24762440 elif (isinstance (node , ComparisonExpr ) and any (is_literal_none (n ) for n in node .operands ) and
24772441 experiments .STRICT_OPTIONAL ):
24782442 # Check for `x is None` and `x is not None`.
@@ -2486,7 +2450,7 @@ def find_isinstance_check(node: Expression,
24862450 # two elements in node.operands, and at least one of them
24872451 # should represent a None.
24882452 vartype = type_map [expr ]
2489- if_vars , else_vars = conditional_type_map (expr , vartype , NoneTyp (), weak = weak )
2453+ if_vars , else_vars = conditional_type_map (expr , vartype , NoneTyp ())
24902454 break
24912455
24922456 if is_not :
@@ -2503,41 +2467,23 @@ def find_isinstance_check(node: Expression,
25032467 else_map = {ref : else_type } if not isinstance (else_type , UninhabitedType ) else None
25042468 return if_map , else_map
25052469 elif isinstance (node , OpExpr ) and node .op == 'and' :
2506- left_if_vars , left_else_vars = find_isinstance_check (
2507- node .left ,
2508- type_map ,
2509- weak ,
2510- )
2511-
2512- right_if_vars , right_else_vars = find_isinstance_check (
2513- node .right ,
2514- type_map ,
2515- weak ,
2516- )
2470+ left_if_vars , left_else_vars = find_isinstance_check (node .left , type_map )
2471+ right_if_vars , right_else_vars = find_isinstance_check (node .right , type_map )
25172472
25182473 # (e1 and e2) is true if both e1 and e2 are true,
25192474 # and false if at least one of e1 and e2 is false.
25202475 return (and_conditional_maps (left_if_vars , right_if_vars ),
25212476 or_conditional_maps (left_else_vars , right_else_vars ))
25222477 elif isinstance (node , OpExpr ) and node .op == 'or' :
2523- left_if_vars , left_else_vars = find_isinstance_check (
2524- node .left ,
2525- type_map ,
2526- weak ,
2527- )
2528-
2529- right_if_vars , right_else_vars = find_isinstance_check (
2530- node .right ,
2531- type_map ,
2532- weak ,
2533- )
2478+ left_if_vars , left_else_vars = find_isinstance_check (node .left , type_map )
2479+ right_if_vars , right_else_vars = find_isinstance_check (node .right , type_map )
25342480
25352481 # (e1 or e2) is true if at least one of e1 or e2 is true,
25362482 # and false if both e1 and e2 are false.
25372483 return (or_conditional_maps (left_if_vars , right_if_vars ),
25382484 and_conditional_maps (left_else_vars , right_else_vars ))
25392485 elif isinstance (node , UnaryExpr ) and node .op == 'not' :
2540- left , right = find_isinstance_check (node .expr , type_map , weak )
2486+ left , right = find_isinstance_check (node .expr , type_map )
25412487 return right , left
25422488
25432489 # Not a supported isinstance check
0 commit comments