22
33import itertools
44
5- from typing import Any , Dict , Set , List , cast , Tuple , Callable , TypeVar , Union
5+ from typing import (
6+ Any , Dict , Set , List , cast , Tuple , Callable , TypeVar , Union , Optional
7+ )
68
79from mypy .errors import Errors
810from mypy .nodes import (
4547from mypy .meet import meet_simple , meet_simple_away , nearest_builtin_ancestor , is_overlapping_types
4648
4749
48- # Kinds of isinstance checks.
49- ISINSTANCE_OVERLAPPING = 0
50- ISINSTANCE_ALWAYS_TRUE = 1
51- ISINSTANCE_ALWAYS_FALSE = 2
52-
5350T = TypeVar ('T' )
5451
5552
@@ -1433,16 +1430,21 @@ def visit_if_stmt(self, s: IfStmt) -> Type:
14331430 for e , b in zip (s .expr , s .body ):
14341431 t = self .accept (e )
14351432 self .check_not_void (t , e )
1436- var , type , elsetype , kind = find_isinstance_check (e , self .type_map ,
1437- self .typing_mode_weak ())
1438- if kind == ISINSTANCE_ALWAYS_FALSE :
1433+ if_map , else_map = find_isinstance_check (
1434+ e , self .type_map ,
1435+ self .typing_mode_weak ()
1436+ )
1437+ if if_map is None :
1438+ # The condition is always false
14391439 # XXX should issue a warning?
14401440 pass
14411441 else :
14421442 # Only type check body if the if condition can be true.
14431443 self .binder .push_frame ()
1444- if var :
1445- self .binder .push (var , type )
1444+ if if_map :
1445+ for var , type in if_map .items ():
1446+ self .binder .push (var , type )
1447+
14461448 self .accept (b )
14471449 _ , frame = self .binder .pop_frame ()
14481450 if not self .breaking_out :
@@ -1451,9 +1453,10 @@ def visit_if_stmt(self, s: IfStmt) -> Type:
14511453
14521454 self .breaking_out = False
14531455
1454- if var :
1455- self .binder .push (var , elsetype )
1456- if kind == ISINSTANCE_ALWAYS_TRUE :
1456+ if else_map :
1457+ for var , type in else_map .items ():
1458+ self .binder .push (var , type )
1459+ if else_map is None :
14571460 # The condition is always true => remaining elif/else blocks
14581461 # can never be reached.
14591462
@@ -2046,24 +2049,16 @@ def map_type_from_supertype(typ: Type, sub_info: TypeInfo,
20462049
20472050def find_isinstance_check (node : Node ,
20482051 type_map : Dict [Node , Type ],
2049- weak : bool = False ) -> Tuple [Node , Type , Type , int ]:
2050- """Check if node is an isinstance(variable, type) check.
2051-
2052- If successful, return tuple (variable, target-type, else-type,
2053- kind); otherwise, return (None, AnyType, AnyType, -1).
2052+ weak : bool = False ) -> Tuple [Optional [Dict [Node , Type ]], Optional [Dict [Node , Type ]]]:
2053+ """Find any isinstance checks (within a chain of ands)".
20542054
2055- When successful, the kind takes one of these values:
2055+ Return value is a map of variables to their types if the condition
2056+ is true and a map of variables to their types if the condition is false.
20562057
2057- ISINSTANCE_OVERLAPPING: The type of variable and the target type are
2058- partially overlapping => the test result can be True or False.
2059- ISINSTANCE_ALWAYS_TRUE: The target type at least as general as the
2060- variable type => the test is always True.
2061- ISINSTANCE_ALWAYS_FALSE: The target type and the variable type are not
2062- overlapping => the test is always False.
2058+ If either of the values in the tuple is None, then that particular
2059+ branch can never occur.
20632060
2064- If it is an isinstance check, but we don't understand the argument
2065- type, then in weak mode it is treated as Any and in non-weak mode
2066- it is not treated as an isinstance.
2061+ Guaranteed to not return None, None. (But may return {}, {})
20672062 """
20682063 if isinstance (node , CallExpr ):
20692064 if refers_to_fullname (node .callee , 'builtins.isinstance' ):
@@ -2072,46 +2067,45 @@ def find_isinstance_check(node: Node,
20722067 vartype = type_map [expr ]
20732068 type = get_isinstance_type (node .args [1 ], type_map )
20742069 if type :
2075- kind = ISINSTANCE_OVERLAPPING
20762070 elsetype = vartype
20772071 if vartype :
20782072 if is_proper_subtype (vartype , type ):
2079- kind = ISINSTANCE_ALWAYS_TRUE
20802073 elsetype = None
2074+ return {expr : type }, None
20812075 elif not is_overlapping_types (vartype , type ):
2082- kind = ISINSTANCE_ALWAYS_FALSE
2076+ return None , { expr : elsetype }
20832077 else :
20842078 elsetype = restrict_subtype_away (vartype , type )
2085- return expr , type , elsetype , kind
2079+ return { expr : type }, { expr : elsetype }
20862080 else :
20872081 # An isinstance check, but we don't understand the type
20882082 if weak :
2089- return expr , AnyType (), vartype , ISINSTANCE_OVERLAPPING
2083+ return { expr : AnyType ()}, { expr : vartype }
20902084 elif isinstance (node , OpExpr ) and node .op == 'and' :
2091- # XXX We should extend this to support two isinstance checks in the same
2092- # expression
2093- (var , type , elsetype , kind ) = find_isinstance_check (node .left , type_map , weak )
2094- if var is None :
2095- (var , type , elsetype , kind ) = find_isinstance_check (node .left , type_map , weak )
2096- if var :
2097- if kind == ISINSTANCE_ALWAYS_TRUE :
2098- kind = ISINSTANCE_OVERLAPPING
2099- return (var , type , AnyType (), kind )
2085+ left_if_vars , right_else_vars = find_isinstance_check (
2086+ node .left ,
2087+ type_map ,
2088+ weak ,
2089+ )
2090+
2091+ right_if_vars , right_else_vars = find_isinstance_check (
2092+ node .right ,
2093+ type_map ,
2094+ weak ,
2095+ )
2096+ if left_if_vars :
2097+ left_if_vars .update (right_if_vars )
2098+ else :
2099+ left_if_vars = right_if_vars
2100+
2101+ # Make no claim about the types in else
2102+ return left_if_vars , {}
21002103 elif isinstance (node , UnaryExpr ) and node .op == 'not' :
2101- ( var , type , elsetype , kind ) = find_isinstance_check (node .expr , type_map , weak )
2102- return ( var , elsetype , type , invert_isinstance_kind ( kind ))
2104+ left , right = find_isinstance_check (node .expr , type_map , weak )
2105+ return right , left
21032106
21042107 # Not a supported isinstance check
2105- return None , AnyType (), AnyType (), - 1
2106-
2107-
2108- def invert_isinstance_kind (kind : int ) -> int :
2109- if kind == ISINSTANCE_ALWAYS_TRUE :
2110- return ISINSTANCE_ALWAYS_FALSE
2111- elif kind == ISINSTANCE_ALWAYS_FALSE :
2112- return ISINSTANCE_ALWAYS_TRUE
2113- else :
2114- return kind
2108+ return {}, {}
21152109
21162110
21172111def get_isinstance_type (node : Node , type_map : Dict [Node , Type ]) -> Type :
0 commit comments