Skip to content

Use isinstance information when type checking 'or' expressions #1709

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

Merged
merged 1 commit into from
Jun 17, 2016
Merged
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
13 changes: 8 additions & 5 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1078,16 +1078,19 @@ def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
left_type = self.accept(e.left, ctx)

if e.op == 'and':
# else_map unused
if_map, else_map = \
right_map, _ = \
mypy.checker.find_isinstance_check(e.left, self.chk.type_map,
self.chk.typing_mode_weak())
elif e.op == 'or':
_, right_map = \
mypy.checker.find_isinstance_check(e.left, self.chk.type_map,
self.chk.typing_mode_weak())
else:
if_map = None
right_map = None

self.chk.binder.push_frame()
if if_map:
for var, type in if_map.items():
if right_map:
for var, type in right_map.items():
self.chk.binder.push(var, type)

right_type = self.accept(e.right, left_type)
Expand Down
6 changes: 6 additions & 0 deletions test-data/unit/check-isinstance.test
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,12 @@ x = B() # type: A

if isinstance(x, B) and x.flag:
pass
if isinstance(x, B) or x.flag: # E: "A" has no attribute "flag"
pass
if not isinstance(x, B) or x.flag:
pass
if not isinstance(x, B) and x.flag: # E: "A" has no attribute "flag"
pass
[builtins fixtures/isinstancelist.py]
[case testIsinstanceExpression]
class A:
Expand Down