Skip to content

Fix iterable membership with subclass item types#21655

Open
w-Jessamine wants to merge 2 commits into
python:masterfrom
w-Jessamine:fix/generator-membership-enum
Open

Fix iterable membership with subclass item types#21655
w-Jessamine wants to merge 2 commits into
python:masterfrom
w-Jessamine:fix/generator-membership-enum

Conversation

@w-Jessamine

Copy link
Copy Markdown

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 an Iterator of an int subclass.

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 -q
  • uv run python -m mypy /tmp/mypy_21601.py --show-error-codes --hide-error-context --no-incremental
@github-actions

This comment has been minimized.

@github-actions

Copy link
Copy Markdown
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]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant