Skip to content

under some conditions mypy thinks a function that always calls sys.exit() returns #14025

@phaker

Description

@phaker

Bug Report

I managed to make mypy think error in the following has implicit return, changing pretty much anything makes it work correctly:

  • moving definition of the class C before that of error
  • removing the use of field declared in __init__, even if there's other code there
  • adding a declaration of x within class body
  • moving the call to sys.exit() before first use of x
  • throwing an exception instead of calling sys.exit()

And I couldn't make similar code fail in any way when the function returns something. Swapping "C" with C doesn't fix anything either of course.

To Reproduce

Code with (most of) the changes mentioned above commented out:

import sys
from typing import NoReturn


# def error(c: "C") -> int:
def error(c: "C") -> NoReturn:
    x = c.x
    # x = 2 + 2

    sys.exit(1)
    # raise Exception()
    # return x


class C:
    # x: int

    def __init__(self, x: int):
        self.x = x

Tried it after deleting caches and with --no-incremental and it still bugs out. (though i definitely didn't bisect all working cases like that)

Expected Behavior

It sees that error doesn't return, as in all other variants.

Actual Behavior

bug.py:6: error: Implicit return in function which does not return
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.982
  • Mypy command-line flags: with and without --strict, with and without --incremental
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions