Fix iterable membership with subclass item types#21655
Open
w-Jessamine wants to merge 2 commits into
Open
Conversation
This comment has been minimized.
This comment has been minimized.
Contributor
|
Diff from mypy_primer, showing the effect of this PR on open source code: PyWinCtl (https://github.com/Kalmat/PyWinCtl)
+ src/pywinctl/_main.py:833: error: Unused "type: ignore" comment [unused-ignore]
+ src/pywinctl/_main.py:837: error: Unused "type: ignore" comment [unused-ignore]
pytest (https://github.com/pytest-dev/pytest)
+ src/_pytest/_code/code.py:404: error: Unused "type: ignore" comment [unused-ignore]
static-frame (https://github.com/static-frame/static-frame)
+ static_frame/core/node_fill_value.py:218: error: Unused "type: ignore" comment [unused-ignore]
+ static_frame/core/node_fill_value.py:231: error: Unused "type: ignore" comment [unused-ignore]
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 #21601.
This updates the fallback path for membership checks against iterables that do not implement
__contains__. Previously, mypy required the left operand to be a subtype of the iterable item type. That is too strict for membership checks where the iterable item type is a subtype of the left operand, such as anIteratorof anintsubclass.The change accepts either subtype direction for the iterable item compatibility check, while preserving the existing strict-equality container diagnostics for actual containers.
Validation:
uv run pytest -n0 mypy/test/testcheck.py::TypeCheckSuite::check-expressions.test -quv run python -m mypy /tmp/mypy_21601.py --show-error-codes --hide-error-context --no-incremental