Narrow negative TypeIs branches through Any in variant positions - #22041
Open
Dextheking1 wants to merge 1 commit into
Open
Dextheking1 wants to merge 1 commit into
Dextheking1 wants to merge 1 commit into
Conversation
Add allow_any_in_variant_positions to is_proper_subtype() so that restrict_subtype_away() recognizes C[Any] as covered by C[object] for covariant/contravariant C. Invariant positions still require exact matches. Fixes python#21641.
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
Dextheking1
marked this pull request as ready for review
September 24, 2026 20:27
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.
Summary
Fixes #21641.
A negative
TypeIscheck failed to narrow a generic withAnyin acovariant/contravariant position:
Root cause
restrict_subtype_away()inmypy/subtypes.pyuses strict proper subtypechecks, under which
Anyis only compatible with itself. SoSource[Any]was not recognised as covered bySource[object]purely becauseof the
Anyargument, even thoughAnyis compatible with every type in avariant position. (The contravariant case already worked because its check goes
through a different path.)
Fix
allow_any_in_variant_positionsoption tois_proper_subtype()/SubtypeContext: covariant/contravariant type arguments fall back to ordinary(Any-compatible) subtype checks, while invariant positions still require exact
matches. The flag is part of the subtype cache key, so cached results stay
correct.
restrict_subtype_away()now retries with this option before giving up, soC[Any]is correctly treated as covered byC[object]for variantC.Invariant generics (e.g.
Box[Any]vsBox[str]) are still not narrowed.Testing
testTypeIsNegativeNarrowCovariantGenericAnyintest-data/unit/check-typeis.test: fails before, passes after. It coverscovariant (narrowed), contravariant (still narrowed) and invariant (still kept).
mypy/test/testsubtypes.py: 26 passed.check-typeis,check-narrowing,check-isinstance,check-generics,check-protocols,check-inference,check-bound,check-typevar-values,check-typeguard: 1269 passed, 6 skipped, 2 xfailed.black --checkclean; mypy self-check clean.Note: this is a first-time contributor PR, so some workflow runs may show as
action_requiredpending maintainer approval.