Skip to content

Always complain if returning a value if annotated to return None #1509

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

Open
JukkaL opened this issue May 9, 2016 · 3 comments
Open

Always complain if returning a value if annotated to return None #1509

JukkaL opened this issue May 9, 2016 · 3 comments

Comments

@JukkaL
Copy link
Collaborator

JukkaL commented May 9, 2016

Mypy doesn't complain about this code that returns a value in a function declared to return None, since the return value is Any:

def f() -> None:
    return g()   # oops, but no error

def g():
    return 1

I think that mypy could complain about the return statement even if the value has type Any. The current behavior is not inconsistent or incorrect, but probably not optimal.

@gvanrossum
Copy link
Member

If you're saying that in a function declared as -> None, any return statement with an expression should always be flagged as an error, I agree with you, though it's technically a style choice, and I've occasionally seen code that uses this knowing the function being called is also returning None, just as a shorthand for

g()
return

IIRC there was also some discussion with @ddfisher about the opposite -- whether in a function declared to return an Optional type, a bare return (or falling off the end) is acceptable style or not.

@gnprice
Copy link
Collaborator

gnprice commented Jul 29, 2016

Added label depends-on/strict-optional because I think anything we do here will probably look different in the strict-optional world from the no-strict-optional world, and any discussion should probably be in the context of the strict-optional world which we intend to be the only world in the future.

@AlexWaygood
Copy link
Member

Mypy now correctly emits an error if --warn-return-any and/or --strict are specified (though the default behavior is still that it emits no errors).

Is this sufficient? — can this issue now be closed?

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

4 participants