Skip to content

Make binder handle unconditional ifs with no else #3567

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
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
10 changes: 8 additions & 2 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -4020,8 +4020,14 @@ def infer_reachability_of_if_statement(s: IfStmt,
mark_block_mypy_only(s.body[i])
for body in s.body[i + 1:]:
mark_block_unreachable(body)
if s.else_body:
mark_block_unreachable(s.else_body)

# Make sure else body always exists and is marked as
# unreachable so the type checker always knows that
# all control flow paths will flow through the if
# statement body.
if not s.else_body:
s.else_body = Block([])
mark_block_unreachable(s.else_body)
break


Expand Down
17 changes: 17 additions & 0 deletions test-data/unit/check-unreachable-code.test
Original file line number Diff line number Diff line change
Expand Up @@ -517,3 +517,20 @@ else:
y = 3
reveal_type(y) # E: Revealed type is 'builtins.str'
[builtins fixtures/ops.pyi]

[case testConditionalAssertWithoutElse]
import typing

class A: pass
class B(A): pass

x = A()
reveal_type(x) # E: Revealed type is '__main__.A'

if typing.TYPE_CHECKING:
assert isinstance(x, B)
reveal_type(x) # E: Revealed type is '__main__.B'

reveal_type(x) # E: Revealed type is '__main__.B'

[builtins fixtures/isinstancelist.pyi]