-
-
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
warn_no_return = False: handle implicit "return None" (not ignoring return type) #7511
Comments
This is by design. When the check is disabled, mypy effectively behaves as if there Python had no implicit At the very least we could make the documentation more detailed. |
The main problem I see here is that you are forced to use the default def f(a) -> int:
if a:
return 1 Disabling the warning is useful/necessary to not change code unnecessarily Therefore I think it would be great if the behavior in this regard could be While I'd still prefer to have Docs for reference: https://mypy.readthedocs.io/en/latest/command_line.html#cmdoption-mypy-no-warn-no-return |
FWIW, I am fine with the current behaviour -- "explicit is better than implicit". The error message and the docs however can be improved. |
@ilevkivskyi |
I don't see a problem if someone opts-in to unsafety and gets an unsafety. (Also this is my final opinion here, I don't have time to write an essay about why/what/etc.) |
Ok, fair enough - I do not want to waste anyone's time of course. |
I agree wholeheartedly. From #3974 (comment):
Indeed, Mypy is overreaching here and enforcing a stylistic choice at the cost of correct language semantics. This is the only reason I'm not using Mypy 😢 |
The most likely way around this that I can see is adding support for enabling/disabling individual error codes, and giving the relevant error message a specific error code that can be disabled (which would hide all these messages). Now anybody who doesn't like this error can disable the relevant error code. I'm happy to accept a PR that implements enabling/disabling error codes (we should first discuss how this would be configured in a separate issue). |
There's actually a few scenarios relevant here: Scenario 1def f1(a) -> int:
if a:
return 1
Scenario 2def f2(a) -> Optional[int]:
if a:
return 1
Scenario 3def f3(a) -> Optional[int]:
if a:
return 1
return None
Scenario 4def f4(a) -> None:
pass
|
|
My issue here is that I have to be explicit twice. I'm more of the DRY mentality: Initial code: def f2(a) -> int:
if a:
return 1 Be explicit about the return value possibly being def f2(a) -> Optional[int]:
if a:
return 1 Be explicit about the return value possibly being def f2(a) -> Optional[int]:
if a:
return 1
return None It's very annoying that 1/3 of my function is now just redundant code that does nothing. It just hurts readability. It was obvious from the start it could return |
Whatever. |
While I agree in general with the Zen of Python, Specifically, |
I once read a beautiful code styling principle on some forum. It goes like this: Reading this affected my code styling in a very profound way since then. |
I had a quick go at using the new return codes functionality (#8975) to get the behavior requested by OP. I.e. to accept an implicit Unfortunately it seems that both situations result in the same |
If your code works and you are confident about it, although mypy is nagging you about not having an explicit return statement in a function/method, just ignore mypy. What mypy is giving you are only suggestions (which you can ignore); it's not like mypy is giving you messages about critical security vulnerabilities and you must make your code mypy compliant and follow its suggestions to the teeth or else you will die on the spot. Having your code mypy-friendly is just like having your code PEP8-friendly. Who among you follow PEP8 to the teeth? Just have code that you are happy with and that it does exactly what you need it to do. |
@PedanticHacker that's a reasonable approach in principle, though not always implementable in practice. Specifically, we're making a push to integrate mypy checks into our CI/CD pipelines. Which means that the mypy checks need to pass, otherwise "computer says no" and the code doesn't get deployed. Just like others who commented above, we'd love to have a way for mypy to check and enforce typing, without enforcing style choices. We experimented with adding explicit For the time being we've added |
I understand. |
Normally I'm in agreement with GvR on this. I find that implicitly returning However, I can think of one exception: import sys
def main() -> Optional[int]:
if something_bad:
return 1
print('ok :)')
if __name__ == '__main__':
sys.exit(main()) In this case, it would be nice to selectively recognize the implicit |
Its a shame GvR is married to "return None" python/mypy#7511
Hi, I believe this must be fixed because if the semantics of the language are that function/methods which don't return anything return None by default, then my types are correct even if I avoid explicit None returns. I understand that mypy must check all paths to make sure to comply with types, hence it is correct to warn when a path is missing a return statement. But in the None case, the return statment is there but hidden. To me it's clearly a Mypy error (or feature creep) just because of this logic and I don't think it can be denied. Thanks |
I'm surprised that I'm doing this, but here's the fix #13219 . Enjoy your |
See python/mypy#7511 though it seems this isn't fully fixed, as it still warns for returns with no explicit return None...
As discussed in python#7511, mypy's `--warn-no-return` isn't really a question of type safety. It does however enforce a rule that is "dear to Guido's heart", and I think we should enforce it for functions that explicitly return Any as well. Fixes python#16095
warn_no_return
is on by default, but does not take into account thatNone
is returned explicitly.I think it should maybe only warn if the return type does not include
None
(i.e. usestyping.Optional
).Given t_mypy.py:
mypy --warn-no-return
shows:mypy --no-warn-no-return
does not show any error, but should warn/error about the missing return statement / wrong return value forno_return
(which is annotated to returnstr
, but might returnNone
).mypy 0.730+dev.28423cd78062b81365ec9aaa591b4e823af1606d
The text was updated successfully, but these errors were encountered: