-
-
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
Optional return type is not correctly handled when the function returns without the 'None' value #11656
Comments
I've run into this myself, it's quite annoying.
|
Already discussed here |
thanks for the pointers, for both of you. but, I see the two problems slightly differently. #7511 discusses the implicit return. my personal view is (1) implicit return is not a bad thing. it is common in many different languages the problem is when a function mixes explicit and implicit returns. that is not a good practice, in my view but, the explicit |
Closing as per this specific comment on that issue: #7511 (comment) mypy could do worse than enforce rules that are dear to Guido's heart ;-) |
I don't think Guido's comment precludes the situation I'm referring to. The code using handling of this particular case by mypy is especially problematic since it does not honor the fundamental principle in python that a function that "does not return a value" is the same as a function "that returns None". mypy should be compliant with the python rules. the programmers do NOT have to change their perfectly valid pythonic coding practice just to use mypy (e.g., just to use add-hoc typing). Using mypy forces the user to use |
@realharry I think some mypy people are too fond of pylint ;) |
Bug Report
In Python, "return" and "return None" are equivalent. MyPy handles it correctly in simple situation. For example,
or
They both pass the mypy check.
However, if you use
Optional[T]
or the Union typeT | None
, mypy does not correctly handles thereturn
statement. For example,They both generate an error:
The lines 12 and 19 refer to the
return
statements without any specific return values.To Reproduce
(Write your steps here:)
None
.Expected Behavior
The
return
statement should be treated asreturn None
.Mypy should not produce an error on a valid Python code.
Actual Behavior
Mypy check fails. See the example above.
Your Environment
mypy.ini
(and other config files): noneThe text was updated successfully, but these errors were encountered: