@@ -2437,6 +2437,21 @@ def and_conditional_maps(m1: Optional[TypeMap], m2: Optional[TypeMap]) -> Option
2437
2437
return result
2438
2438
2439
2439
2440
+ def or_conditional_maps (m1 : Optional [TypeMap ], m2 : Optional [TypeMap ]) -> Optional [TypeMap ]:
2441
+ if m1 is None :
2442
+ return m2
2443
+ if m2 is None :
2444
+ return m1
2445
+ # Both branches are possible. Combine information about variables
2446
+ # whose type is refined in both branches.
2447
+ result = {}
2448
+ for n1 in m1 :
2449
+ for n2 in m2 :
2450
+ if n1 .literal_hash == n2 .literal_hash :
2451
+ result [n1 ] = UnionType .make_simplified_union ([m1 [n1 ], m2 [n2 ]])
2452
+ return result
2453
+
2454
+
2440
2455
def find_isinstance_check (node : Node ,
2441
2456
type_map : Dict [Node , Type ],
2442
2457
weak : bool = False
@@ -2484,7 +2499,22 @@ def find_isinstance_check(node: Node,
2484
2499
_ , if_vars = conditional_type_map (node , vartype , NoneTyp (), weak = weak )
2485
2500
return if_vars , {}
2486
2501
elif isinstance (node , OpExpr ) and node .op == 'and' :
2487
- left_if_vars , right_else_vars = find_isinstance_check (
2502
+ left_if_vars , left_else_vars = find_isinstance_check (
2503
+ node .left ,
2504
+ type_map ,
2505
+ weak ,
2506
+ )
2507
+
2508
+ right_if_vars , right_else_vars = find_isinstance_check (
2509
+ node .right ,
2510
+ type_map ,
2511
+ weak ,
2512
+ )
2513
+
2514
+ return (and_conditional_maps (left_if_vars , right_if_vars ),
2515
+ or_conditional_maps (left_else_vars , right_else_vars ))
2516
+ elif isinstance (node , OpExpr ) and node .op == 'or' :
2517
+ left_if_vars , left_else_vars = find_isinstance_check (
2488
2518
node .left ,
2489
2519
type_map ,
2490
2520
weak ,
@@ -2496,8 +2526,8 @@ def find_isinstance_check(node: Node,
2496
2526
weak ,
2497
2527
)
2498
2528
2499
- # Make no claim about the types in else
2500
- return and_conditional_maps (left_if_vars , right_if_vars ), {}
2529
+ return ( or_conditional_maps ( left_if_vars , right_if_vars ),
2530
+ and_conditional_maps (left_else_vars , right_else_vars ))
2501
2531
elif isinstance (node , UnaryExpr ) and node .op == 'not' :
2502
2532
left , right = find_isinstance_check (node .expr , type_map , weak )
2503
2533
return right , left
0 commit comments