-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Handle empty function bodies safely #2350
Comments
This would only apply to abstract methods whose return value cannot be None, right? I think this should be okay and allowed via super: class C:
@abstractmethod
def f(self) -> Optional[int]:
pass
class D(C):
def f(self) -> Optional[int]:
return super().f() |
Yeah, we could safely allow those examples. |
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 |
I think @ddfisher would like both of these to become the default. David,
can you come up with a plan?
|
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. |
This keeps coming, so I propose to raise priority at least to normal. |
Whatever we decide here might require updating thousands of tests cases, so adding the tests label. |
What if we modified the definition of a "trivial" function so that functions containing only One additional benefit is that this change would now make the distinction between |
That won't work in Python 2 though -- |
This continues to confuse people, so I am going to fix this. Here is my plan:
|
Does this need any help? def some_function() -> int:
"""Does a foo to a bar."""
# whoops... i got distracted and forgot to write a body Mypy is happy with this, but I don't think it should be. |
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:
assert False
or# type: ignore
.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.
See also discussion in #2339.
The text was updated successfully, but these errors were encountered: