Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #2350
Fixes #8005
The implementation is mostly simple, except overloads are a bit tricky and there is still little unsafety can slip through (we can fix it later with another function flag if it will look like worth it, see TODO item in a test). The actual diff is quite small but there are tons of tests, because there are many possible combinations and corner cases, and I wanted to test as much of them as possible.
I am following the logic I previously proposed in #2350. I copy it here FTR:
--allow-empty-bodies
flag, we need it for tests (hundreds would fail otherwise, also in other projects likesqlalchemy-stubs
anddjango-stubs
)@abstractmethod
super()
(unless they come from a stub)Note that the stub exceptions in two last points can be summarized as: since we don't know the actual body for something that comes from a stub file, we choose to not give a false positive and stay silent.
Another note is that after #8102 is merged we can turn the hidden flag into an error code, but TBH I am not sure it is necessary. It looks like the flag is more handy, and it makes sense (and feels natural) to use the existing error code
[return]
(missing return statement) for the unsafe empty bodies.I am going to try this soon against internal codebases to see if there are some problematic corner cases missing and make any adjustment + tests if needed. Note that
mypy/visitor.py
has an existing unsafety that this new logic uncovers (there is an existing TODO about this). This requires turningwarn_no_return
flag off for this file.Finally, there were couple tiny bugs discovered by tests I added, so I just fixed them in this same PR.