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