-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Docs: confirm that use of None-typed function return value is disallowed #3226
Comments
The example is a horrible hack and I'm glad mypy rejects it. Agreed that (2) and (3) are worth fixing. |
I think this behavior is incorrect, regardless of how one feels about the hackiness of the example. Consider (apologies for the length of the code below, but I wanted to work up to a relatively sane example that mypy rejects):
What's really surprising to me is that this error comes up for both |
I also think this should be allowed since |
@pbatko Well, this is your opinion, @gvanrossum expressed a different opinion above. I also think mypy should flag this as error. Here are some points:
|
Yeah, let’s just update the docs and the error message. |
I'm on the side of allowing functions that explicitly return import typing
def func_that_doesnt_return_anything() -> None:
...
def func_that_explicitly_returns_none() -> typing.Literal[None]:
return None
l = []
l.append(func_that_explicitly_returns_none()) # mypy should allow this.
l.append(func_that_doesnt_return_anything()) # mypy should still flag an error here. There may be other smarter ways but the point is that we can cater for a way to explicitly allow the return values from None-returning functions to be used as valid values within the caller's context. |
May I suggest changing the error message from |
@carlosgmartin I like the idea. |
Fixes #3226. Aims to provide better assistance to users who may be confused when their void functions technically return None. Co-authored-by: Ilya Priven <ilya.konstantinov@gmail.com> Co-authored-by: hauntsaninja <hauntsaninja@gmail.com>
Regarding an example provided by @Jdban:
There are a few issues:
@elazarg
I understand, but I think 2) and 3) are worth fixing. I don't have an opinion on whether special-casing 1) for this particular use case is worth it.
The text was updated successfully, but these errors were encountered: