Skip to content

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

Open
@bbatliner

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.

def fn() -> None:
    i: Optional[int] = None
    try:
        i = 1
    finally:
        if i is None:
            return  # error: Statement is unreachable

Expected Behavior

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

def fn() -> None:
    i: Optional[int] = None
    try:
        time.sleep(10)
        i = 1
    finally:
        if i is None:
            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