Skip to content

Commit feaabde

Browse files
rwbartonJukkaL
authored andcommitted
Use isinstance information when type checking 'or' expressions (#1709)
Note: This is not the same as #942. It does address a pattern which occurs at least once in mypy itself though: ``` if res and (not isinstance(res, Instance) or cast(Instance, res).args): ```
1 parent 9cd1954 commit feaabde

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

mypy/checkexpr.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,16 +1080,19 @@ def check_boolean_op(self, e: OpExpr, context: Context) -> Type:
10801080
left_type = self.accept(e.left, ctx)
10811081

10821082
if e.op == 'and':
1083-
# else_map unused
1084-
if_map, else_map = \
1083+
right_map, _ = \
1084+
mypy.checker.find_isinstance_check(e.left, self.chk.type_map,
1085+
self.chk.typing_mode_weak())
1086+
elif e.op == 'or':
1087+
_, right_map = \
10851088
mypy.checker.find_isinstance_check(e.left, self.chk.type_map,
10861089
self.chk.typing_mode_weak())
10871090
else:
1088-
if_map = None
1091+
right_map = None
10891092

10901093
self.chk.binder.push_frame()
1091-
if if_map:
1092-
for var, type in if_map.items():
1094+
if right_map:
1095+
for var, type in right_map.items():
10931096
self.chk.binder.push(var, type)
10941097

10951098
right_type = self.accept(e.right, left_type)

test-data/unit/check-isinstance.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,12 @@ x = B() # type: A
731731

732732
if isinstance(x, B) and x.flag:
733733
pass
734+
if isinstance(x, B) or x.flag: # E: "A" has no attribute "flag"
735+
pass
736+
if not isinstance(x, B) or x.flag:
737+
pass
738+
if not isinstance(x, B) and x.flag: # E: "A" has no attribute "flag"
739+
pass
734740
[builtins fixtures/isinstancelist.py]
735741
[case testIsinstanceExpression]
736742
class A:

0 commit comments

Comments
 (0)