Skip to content

False positive statement is unreachable in try-finally related to KeyboardInterrupt #13104

Description

@bbatliner

Bug Report

A KeyboardInterrupt may interleave between the start of a try block and its contained statements. mypy seems to assume that the flow of execution will not be asynchronously interrupted.

deffn() ->None:
i: Optional[int] =Nonetry:
i=1finally:
ifiisNone:
return# error: Statement is unreachable

Expected Behavior

It's trivial to show that this statement is reachable. Insert a sleep before the assignment:

deffn() ->None:
i: Optional[int] =Nonetry:
time.sleep(10)
i=1finally:
ifiisNone:
print("reachable")
return

And then Ctrl+C (KeyboardInterrupt) the function during its execution. The print will run.

Your Environment

  • Mypy version used: 0.960
  • Mypy command-line flags: --warn-unreachable seems to be the only flag necessary to reproduce this

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-reachabilityDetecting unreachable code

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions