Skip to content

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

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

Closed
phaker opened this issue Nov 7, 2022 · 3 comments
Labels
bug mypy got something wrong

Comments

@phaker
Copy link

phaker commented Nov 7, 2022

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
@phaker phaker added the bug mypy got something wrong label Nov 7, 2022
@phaker
Copy link
Author

phaker commented Nov 8, 2022

I'm happy to report I can't trigger this on mypy 0.990.

I upgraded to python 3.11 and the hot off the press mypy 0.990, new mypy works on both 3.10 and 3.11, checked mypy 0.982 on 3.11 and that still fails.

@phaker phaker closed this as completed Nov 8, 2022
@hauntsaninja
Copy link
Collaborator

Thanks, I think this was #13575

@phaker
Copy link
Author

phaker commented Nov 9, 2022

Thanks for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

2 participants