Skip to content

Recognize functions like exit() as not returning #2280

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
JukkaL opened this issue Oct 19, 2016 · 9 comments
Closed

Recognize functions like exit() as not returning #2280

JukkaL opened this issue Oct 19, 2016 · 9 comments

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented Oct 19, 2016

This generates a warning when using --warn-no-return, even though it's safe:

def f(x: int) -> int:
    if x == 0:
        return x
    exit(1)

It would be nice if mypy knew about at least the most common stdlib functions that never return such as sys.exit.

@elazarg
Copy link
Contributor

elazarg commented Oct 19, 2016

Something like this?

NoReturn = Union[()]
def exit(n: int) -> NoReturn: ...

@JukkaL
Copy link
Collaborator Author

JukkaL commented Oct 19, 2016

That is one way to do it, but it may be a little confusing.

@elazarg
Copy link
Contributor

elazarg commented Oct 19, 2016

It's better than special casing exit(), which will not be robust and cannot be as flexible.

If naming is the problem, it can be solved (as above) from outside of mypy. Converging to naming conventions of another language such as [[noreturn]] in C++ and Nothing in Scala may benefit both languages.

@JukkaL
Copy link
Collaborator Author

JukkaL commented Oct 20, 2016

Having a general mechanism would be more flexible, but that's harder since we'll need to standardize on a solution (this has been discussed before and needs a decision here: python/typing#165). Special casing a few functions like sys.exit would be a reasonable incremental improvement, at least for now.

@ddfisher
Copy link
Collaborator

Let's start by special casing the exit functions in the standard library. That might be good enough for a while.

@gvanrossum gvanrossum added this to the 0.5 milestone Oct 20, 2016
@gvanrossum
Copy link
Member

gvanrossum commented Oct 25, 2016 via email

@gvanrossum
Copy link
Member

We're going to spell this as NoReturn, initially in mypy_extensions. See python/typing#165 (comment) and #2637.

@ilevkivskyi
Copy link
Member

It looks like this issue is fixed now. At least the original example works correctly.

@ddfisher
Copy link
Collaborator

Yeah, this is fixed as of #2798.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants