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.
Change Summary
I am working on
mypy
type narrowing bug. And our CI identified that this repo is also affected.python/mypy#11273 (comment)
What bug am I talking about?
We were narrowing
if
conditions incorrectly. In your case:In
else
caseitems
were not narrowed to be<nothing>
, becauseMapping
andSet
cases are already covered. It was still something likeMappingIntStrAny
. This is why it was possible to useitems.__class__
.But, after new
mypy
version will be released (and if this fix is merged), this line would raise an error:Because
NoReturn
does not have__class__
attribute defined.So, here's my solution on how to fix it: intoduce
assert_never
helper, docs: https://mypy.readthedocs.io/en/latest/literal_types.html?highlight=assert_never#exhaustive-checksAnd type-ignore msg argument.
Checklist
changes/<pull request or issue id>-<github username>.md
file added describing change(see changes/README.md for details)
I think, that this is a pretty trivial change to go into changelog / news. If not, feel free to ping me to add it 🙂