You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently an empty function body is accepted if a function returns a non-None value (even with --strict-optional). This is mostly useful for abstract methods. This is unsound, since we don't prevent anybody from calling these empty functions.
Here are some ideas for making this safe:
Only allow empty function bodies for abstract methods. Other functions will need something like a return statement, a raise statement or assert False or # type: ignore.
Tag abstract methods with an empty body with a flag in the AST, and disallow calls to them via super().
This still wouldn't address empty functions defined in stubs, but perhaps can just assume that every function in a stub has a non-empty body.
The extensive use of functions with empty body and arbitrary type in the tests was another motivation for allowing empty bodies. Though it isn't a problem now, there would be a huge pain should warn-no-return (and perhaps strict-optional) be turned on by default. Not saying it should block this change, but it would be good to have some plan for dealing with this when it comes up.
I think it'd be reasonable not to worry about this too much for now: we're erring on the side of being more permissive (which is the better side to err on when introducing a new default), and I'd expect empty functions to be much more often intentional than not.
What if we modified the definition of a "trivial" function so that functions containing only pass are no longer considered to be trivial?
One additional benefit is that this change would now make the distinction between ... and pass meaningful, rather than a matter of style. You'll now need to deliberately use the former if you want mypy to ignore the body.
Activity
gvanrossum commentedon Oct 27, 2016
This would only apply to abstract methods whose return value cannot be None, right? I think this should be okay and allowed via super:
JukkaL commentedon Oct 27, 2016
Yeah, we could safely allow those examples.
rwbarton commentedon Oct 27, 2016
The extensive use of functions with empty body and arbitrary type in the tests was another motivation for allowing empty bodies. Though it isn't a problem now, there would be a huge pain should
warn-no-return(and perhapsstrict-optional) be turned on by default. Not saying it should block this change, but it would be good to have some plan for dealing with this when it comes up.gvanrossum commentedon Oct 27, 2016
ddfisher commentedon Oct 27, 2016
I think it'd be reasonable not to worry about this too much for now: we're erring on the side of being more permissive (which is the better side to err on when introducing a new default), and I'd expect empty functions to be much more often intentional than not.
ilevkivskyi commentedon Dec 2, 2018
This keeps coming, so I propose to raise priority at least to normal.
ilevkivskyi commentedon Apr 17, 2019
Whatever we decide here might require updating thousands of tests cases, so adding the tests label.
Michael0x2a commentedon Apr 17, 2019
What if we modified the definition of a "trivial" function so that functions containing only
passare no longer considered to be trivial?One additional benefit is that this change would now make the distinction between
...andpassmeaningful, rather than a matter of style. You'll now need to deliberately use the former if you want mypy to ignore the body.gvanrossum commentedon Apr 17, 2019
That won't work in Python 2 though --
...by itself is not valid there.ilevkivskyi commentedon Dec 3, 2019
This continues to confuse people, so I am going to fix this. Here is my plan:
--allow-empty-bodiesflag, we need it for tests (hundreds would fail otherwise, also in other projects likesqlalchemy-stubsanddjango-stubs)@abstractmethodsuper()(unless they come from a stub)is_trivial_body()withis_raising_or_empty()(potentially the former can be either a docstring or the latter)tusharsadhwani commentedon Jan 11, 2022
Does this need any help?
This has bitten me many times when I get too reliant on mypy:
Mypy is happy with this, but I don't think it should be.
Handle empty bodies safely (#13729)
Update for the latest version of mypy.