Skip to content

Fix TypeIs narrowing for covariant generic Any#21662

Open
cjc0013 wants to merge 1 commit into
python:masterfrom
cjc0013:cjc/typeis-covariant-any-narrowing
Open

Fix TypeIs narrowing for covariant generic Any#21662
cjc0013 wants to merge 1 commit into
python:masterfrom
cjc0013:cjc/typeis-covariant-any-narrowing

Conversation

@cjc0013

@cjc0013 cjc0013 commented Jun 30, 2026

Copy link
Copy Markdown

Fixes #21641.

Summary

  • Remove statically-covered generic instances from the false branch of TypeIs checks according to declared variance.
  • This lets TypeIs[Source[object]] narrow away Source[Any] when Source is covariant.
  • Keep invariant generics conservative, so Box[Any] is not removed by TypeIs[Box[str]].

Tests

  • python runtests.py check-typeis
  • python runtests.py check-typeguard check-narrowing
  • python -m pytest mypy/test/testsubtypes.py -q
  • python -m mypy --config-file mypy_self_check.ini mypy/subtypes.py
  • python -m ruff check mypy/subtypes.py
  • python -m black --check mypy/subtypes.py
  • git diff --check

Prepared with AI assistance.

@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing TypeIs narrowing with covariant generic

1 participant