Skip to content

Use information from find_isinstance_check in short-circuit case too #1751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2159,7 +2159,8 @@ def find_isinstance_check(node: Node,
weak: bool=False
) -> Tuple[Optional[Dict[Node, Type]], Optional[Dict[Node, Type]]]:
"""Find any isinstance checks (within a chain of ands). Includes
implicit and explicit checks for None.
implicit and explicit checks for None. `node` must already have
been type checked so that its subexpressions have types in type_map.

Return value is a map of variables to their types if the condition
is true and a map of variables to their types if the condition is false.
Expand Down
36 changes: 29 additions & 7 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1077,25 +1077,47 @@ def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
# inference of the right operand so that expressions such as
# '[1] or []' are inferred correctly.
ctx = self.chk.type_context[-1]
left_type = self.accept(e.left, ctx)

# The left operand will always be evaluated, so make sure it
# type checks unconditionally. (We have to type check it before
# calling find_isinstance_check anyways.)
ctx = left_type = self.accept(e.left, ctx)

# If this is an 'and' or 'or' operation, then left_map and right_map
# are the typing conditions that must hold for the expression to
# evaluate to its left or right operand, respectively.
if e.op == 'and':
right_map, _ = \
right_map, left_map = \
mypy.checker.find_isinstance_check(e.left, self.chk.type_map,
self.chk.typing_mode_weak())
elif e.op == 'or':
_, right_map = \
left_map, right_map = \
mypy.checker.find_isinstance_check(e.left, self.chk.type_map,
self.chk.typing_mode_weak())
else:
right_map = None
assert False, "check_boolean_op can only process 'and' and 'or' expressions"

with self.chk.binder.frame_context():
if right_map:
for var, type in right_map.items():
if left_map == {}:
# optimization: we learned nothing from
# find_isinstance_check, so no need to re-check.
# (The old parser generates left-nested 'and' trees
# for 'e1 and ... and eN'; fixed in fast parser.)
pass
elif left_map is not None:
for var, type in left_map.items():
self.chk.binder.push(var, type)
left_type = self.accept(e.left, ctx)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have to recheck e.left?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the tests includes an example where we learn more about the type of an expression when we find out it is true. isinstance(x, A) and x.a has type Union[bool, A] (where x.a : A) but isinstance(x, A) and x.a or A() has type A because for it to evaluate to isinstance(x, A) and x.a, that expression must be true and then it will evaluate to x.a.

In fact, that logic applies to any expression p and q or r even if p is not an isinstance check, so perhaps this isn't a very compelling example.

else:
left_type = UninhabitedType()

right_type = self.accept(e.right, left_type)
with self.chk.binder.frame_context():
if right_map is not None:
for var, type in right_map.items():
self.chk.binder.push(var, type)
right_type = self.accept(e.right, ctx)
else:
right_type = UninhabitedType()

self.check_not_void(left_type, context)
self.check_not_void(right_type, context)
Expand Down
30 changes: 30 additions & 0 deletions test-data/unit/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -1099,3 +1099,33 @@ def f(x: Union[List[int], str]) -> None:
[out]
main: note: In function "f":
main:4: error: "int" not callable

[case testIsinstanceInWrongOrderInBooleanOp]
class A:
m = 1
def f(x: object) -> None:
if x.m and isinstance(x, A) or False: # E: "object" has no attribute "m"
pass
[builtins fixtures/isinstance.py]
[out]
main: note: In function "f":

[case testIsinstanceAndOr]
class A:
a = None # type: A

def f(x: object) -> None:
b = isinstance(x, A) and x.a or A()
reveal_type(b) # E: Revealed type is '__main__.A'
[builtins fixtures/isinstance.py]
[out]
main: note: In function "f":

[case testLeftNestedAnds]
one = 1
assert ((((((((((((((((((((((((((((((one
and one) and one) and one) and one) and one) and one) and one)
and one) and one) and one) and one) and one) and one) and one)
and one) and one) and one) and one) and one) and one) and one)
and one) and one) and one) and one) and one) and one) and one)
and one) and one)
24 changes: 24 additions & 0 deletions test-data/unit/check-optional.test
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,30 @@ else:
reveal_type(x) # E: Revealed type is 'builtins.int'
[builtins fixtures/bool.py]

[case testOrCases]
from typing import Optional
x = None # type: Optional[str]
y1 = x or 'a'
reveal_type(y1) # E: Revealed type is 'builtins.str'
y2 = x or 1
reveal_type(y2) # E: Revealed type is 'Union[builtins.str, builtins.int]'
z1 = 'a' or x
reveal_type(z1) # E: Revealed type is 'Union[builtins.str, builtins.None]'
z2 = 1 or x
reveal_type(z2) # E: Revealed type is 'Union[builtins.int, builtins.str, builtins.None]'

[case testAndCases]
from typing import Optional
x = None # type: Optional[str]
y1 = x and 'b'
reveal_type(y1) # E: Revealed type is 'Union[builtins.None, builtins.str]'
y2 = x and 1 # x could be '', so...
reveal_type(y2) # E: Revealed type is 'Union[builtins.str, builtins.None, builtins.int]'
z1 = 'b' and x
reveal_type(z1) # E: Revealed type is 'Union[builtins.str, builtins.None]'
z2 = 1 and x
reveal_type(z2) # E: Revealed type is 'Union[builtins.int, builtins.str, builtins.None]'

[case testLambdaReturningNone]
f = lambda: None
x = f()
Expand Down