Skip to content

False positive "Missing return statement" when exception is raised from function in try-except block #8964

Closed
@jamesbraza

Description

@jamesbraza

I am reporting what I think is a bug in mypy. I think it raises a false positive error: Missing return statement when one has a function that is guaranteed to raise an exception (never return) while in the except clause of a try statement.


Code Sample

false_pos_no_return.py:

def raise_exception():
    raise Exception


def test() -> int:
    try:
        return 1
    except Exception:
        raise_exception()

What mypy reports:

path/to/false_pos_no_return.py: note: In function "test":
path/to/false_pos_no_return.py:5: error: Missing return statement  [return]

Workarounds

One can add either of the following statements at the end of the except clause:

  • raise
  • assert False

And the error goes away: Success: no issues found in 1 source file


Expectations

My expectations are that mypy should be able to detect that raise_exception will never actually return.


Similar Issues

#1229 seems to be similar, but it seems a decisive conclusion wasn't reached.


Versions

python==3.6.5
mypy==0.780

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions