@@ -99,8 +99,6 @@ class TypeChecker(NodeVisitor[Type]):
99
99
dynamic_funcs = None # type: List[bool]
100
100
# Stack of functions being type checked
101
101
function_stack = None # type: List[FuncItem]
102
- # Do weak type checking in this file
103
- weak_opts = set () # type: Set[str]
104
102
# Stack of collections of variables with partial types
105
103
partial_types = None # type: List[Dict[Var, Context]]
106
104
globals = None # type: SymbolTable
@@ -139,7 +137,6 @@ def __init__(self, errors: Errors, modules: Dict[str, MypyFile]) -> None:
139
137
self .type_context = []
140
138
self .dynamic_funcs = []
141
139
self .function_stack = []
142
- self .weak_opts = set () # type: Set[str]
143
140
self .partial_types = []
144
141
self .deferred_nodes = []
145
142
self .pass_num = 0
@@ -153,7 +150,6 @@ def visit_file(self, file_node: MypyFile, path: str, options: Options) -> None:
153
150
self .is_stub = file_node .is_stub
154
151
self .errors .set_file (path )
155
152
self .globals = file_node .names
156
- self .weak_opts = file_node .weak_opts
157
153
self .enter_partial_types ()
158
154
self .is_typeshed_stub = self .errors .is_typeshed_file (path )
159
155
self .module_type_map = {}
@@ -222,7 +218,7 @@ def accept(self, node: Node, type_context: Type = None) -> Type:
222
218
report_internal_error (err , self .errors .file , node .line , self .errors , self .options )
223
219
self .type_context .pop ()
224
220
self .store_type (node , typ )
225
- if self .typing_mode_none ():
221
+ if not self .in_checked_function ():
226
222
return AnyType ()
227
223
else :
228
224
return typ
@@ -1086,7 +1082,7 @@ def check_assignment(self, lvalue: Lvalue, rvalue: Expression, infer_lvalue_type
1086
1082
self .binder .assign_type (lvalue ,
1087
1083
rvalue_type ,
1088
1084
lvalue_type ,
1089
- self . typing_mode_weak () )
1085
+ False )
1090
1086
elif index_lvalue :
1091
1087
self .check_indexed_assignment (index_lvalue , rvalue , rvalue )
1092
1088
@@ -1315,10 +1311,7 @@ def is_definition(self, s: Lvalue) -> bool:
1315
1311
def infer_variable_type (self , name : Var , lvalue : Lvalue ,
1316
1312
init_type : Type , context : Context ) -> None :
1317
1313
"""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 ):
1322
1315
self .check_usable_type (init_type , context )
1323
1316
self .set_inference_error_fallback_type (name , lvalue , init_type , context )
1324
1317
elif isinstance (init_type , DeletedType ):
@@ -1403,8 +1396,6 @@ def check_simple_assignment(self, lvalue_type: Type, rvalue: Expression,
1403
1396
rvalue_type = self .accept (rvalue , lvalue_type )
1404
1397
if isinstance (rvalue_type , DeletedType ):
1405
1398
self .msg .deleted_as_rvalue (rvalue_type , context )
1406
- if self .typing_mode_weak ():
1407
- return rvalue_type
1408
1399
if isinstance (lvalue_type , DeletedType ):
1409
1400
self .msg .deleted_as_lvalue (lvalue_type , context )
1410
1401
else :
@@ -1499,7 +1490,7 @@ def visit_return_stmt(self, s: ReturnStmt) -> Type:
1499
1490
if isinstance (return_type , (Void , NoneTyp , AnyType )):
1500
1491
return None
1501
1492
1502
- if self .typing_mode_full ():
1493
+ if self .in_checked_function ():
1503
1494
self .fail (messages .RETURN_VALUE_EXPECTED , s )
1504
1495
1505
1496
def wrap_generic_type (self , typ : Instance , rtyp : Instance , check_type :
@@ -1534,10 +1525,7 @@ def visit_if_stmt(self, s: IfStmt) -> Type:
1534
1525
for e , b in zip (s .expr , s .body ):
1535
1526
t = self .accept (e )
1536
1527
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 )
1541
1529
if if_map is None :
1542
1530
# The condition is always false
1543
1531
# XXX should issue a warning?
@@ -1593,10 +1581,7 @@ def visit_assert_stmt(self, s: AssertStmt) -> Type:
1593
1581
self .accept (s .expr )
1594
1582
1595
1583
# 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 )
1600
1585
1601
1586
if true_map :
1602
1587
for var , type in true_map .items ():
@@ -1818,7 +1803,7 @@ def flatten(t: Expression) -> List[Expression]:
1818
1803
self .binder .assign_type (elt ,
1819
1804
DeletedType (source = elt .name ),
1820
1805
self .binder .get_declaration (elt ),
1821
- self . typing_mode_weak () )
1806
+ False )
1822
1807
return None
1823
1808
1824
1809
def visit_decorator (self , e : Decorator ) -> Type :
@@ -2113,7 +2098,7 @@ def visit_yield_expr(self, e: YieldExpr) -> Type:
2113
2098
expected_item_type = self .get_generator_yield_type (return_type , False )
2114
2099
if e .expr is None :
2115
2100
if (not isinstance (expected_item_type , (Void , NoneTyp , AnyType ))
2116
- and self .typing_mode_full ()):
2101
+ and self .in_checked_function ()):
2117
2102
self .fail (messages .YIELD_VALUE_EXPECTED , e )
2118
2103
else :
2119
2104
actual_item_type = self .accept (e .expr , expected_item_type )
@@ -2224,32 +2209,17 @@ def store_type(self, node: Node, typ: Type) -> None:
2224
2209
if typ is not None :
2225
2210
self .module_type_map [node ] = typ
2226
2211
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?
2242
2214
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 ])
2253
2223
2254
2224
def lookup (self , name : str , kind : int ) -> SymbolTableNode :
2255
2225
"""Look up a definition from the symbol table with the given name.
@@ -2374,8 +2344,6 @@ def method_type(self, func: FuncBase) -> FunctionLike:
2374
2344
def conditional_type_map (expr : Expression ,
2375
2345
current_type : Optional [Type ],
2376
2346
proposed_type : Optional [Type ],
2377
- * ,
2378
- weak : bool = False
2379
2347
) -> Tuple [TypeMap , TypeMap ]:
2380
2348
"""Takes in an expression, the current type of the expression, and a
2381
2349
proposed type of that expression.
@@ -2397,10 +2365,7 @@ def conditional_type_map(expr: Expression,
2397
2365
return {expr : proposed_type }, {}
2398
2366
else :
2399
2367
# 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 {}, {}
2404
2369
2405
2370
2406
2371
def is_literal_none (n : Expression ) -> bool :
@@ -2453,7 +2418,6 @@ def or_conditional_maps(m1: TypeMap, m2: TypeMap) -> TypeMap:
2453
2418
2454
2419
def find_isinstance_check (node : Expression ,
2455
2420
type_map : Dict [Node , Type ],
2456
- weak : bool = False
2457
2421
) -> Tuple [TypeMap , TypeMap ]:
2458
2422
"""Find any isinstance checks (within a chain of ands). Includes
2459
2423
implicit and explicit checks for None.
@@ -2472,7 +2436,7 @@ def find_isinstance_check(node: Expression,
2472
2436
if expr .literal == LITERAL_TYPE :
2473
2437
vartype = type_map [expr ]
2474
2438
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 )
2476
2440
elif (isinstance (node , ComparisonExpr ) and any (is_literal_none (n ) for n in node .operands ) and
2477
2441
experiments .STRICT_OPTIONAL ):
2478
2442
# Check for `x is None` and `x is not None`.
@@ -2486,7 +2450,7 @@ def find_isinstance_check(node: Expression,
2486
2450
# two elements in node.operands, and at least one of them
2487
2451
# should represent a None.
2488
2452
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 ())
2490
2454
break
2491
2455
2492
2456
if is_not :
@@ -2503,41 +2467,23 @@ def find_isinstance_check(node: Expression,
2503
2467
else_map = {ref : else_type } if not isinstance (else_type , UninhabitedType ) else None
2504
2468
return if_map , else_map
2505
2469
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 )
2517
2472
2518
2473
# (e1 and e2) is true if both e1 and e2 are true,
2519
2474
# and false if at least one of e1 and e2 is false.
2520
2475
return (and_conditional_maps (left_if_vars , right_if_vars ),
2521
2476
or_conditional_maps (left_else_vars , right_else_vars ))
2522
2477
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 )
2534
2480
2535
2481
# (e1 or e2) is true if at least one of e1 or e2 is true,
2536
2482
# and false if both e1 and e2 are false.
2537
2483
return (or_conditional_maps (left_if_vars , right_if_vars ),
2538
2484
and_conditional_maps (left_else_vars , right_else_vars ))
2539
2485
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 )
2541
2487
return right , left
2542
2488
2543
2489
# Not a supported isinstance check
0 commit comments