Repository navigation
Reconcile diamond-inherited generic bases with variance - #5100
Open
nitishagar wants to merge 1 commit into
Open
nitishagar wants to merge 1 commit into
nitishagar wants to merge 1 commit into
Conversation
Two base paths reaching the same ancestor class were compared with plain type-argument equality, so a diamond whose specializations differ only in a non-invariant position was rejected: `NarrowRange(RangeBase[int, int], WideParam)` reporting `Base[int, int]` against `Base[SupportsIndex | SupportsInt, int]` even though `Base` is contravariant in that position and the pair is therefore consistent (issue facebook#4842; pyright accepts). The conflict predicate in the C3 linearization now delegates to a comparison callback that checks each differing position under the ancestor class's variance: covariant or contravariant positions are compatible when either specialization is assignable to the other (the first-wins walk order is arbitrary), invariant and bivariant positions require two-way consistency as before, and `Any` stays gradual. Distinct type variables and unrelated invariant arguments keep erroring with the same message, and the `Iterable[int]` / `Iterable[int | str]` diamond that the old equality check flagged is now correctly accepted.
Contributor
|
This pull request has been imported. If you are a Meta employee, you can view this in D123340876. (Because this pull request was imported automatically, there will not be any future comments.) |
|
Diff from mypy_primer, showing the effect of this PR on open source code: ============================================================
SUMMARY
============================================================
Total: +0 new errors, -9 fixed errors
By preset: +0/-9 (default), +0/-9 (strict)
Projects with changes (2):
steam.py: +0 -8
meson: +0 -1
============================================================
FULL DIFF DETAILS
------------------------------------------------------------
steam.py (https://github.com/Gobot1234/steam.py)
- ERROR steam/ext/commands/converters.py:273:7-20: Class `UserConverter` has inconsistent type arguments for base class `ConverterBase`: `ConverterBase[PartialUser]` and `ConverterBase[User]` [invalid-inheritance]
- ERROR steam/ext/commands/converters.py:273:7-20: Class `UserConverter` has inconsistent type arguments for base class `Converter`: `Converter[PartialUser]` and `Converter[User]` [invalid-inheritance]
- ERROR steam/ext/commands/converters.py:294:7-29: Class `PartialMemberConverter` has inconsistent type arguments for base class `ConverterBase`: `ConverterBase[PartialUser]` and `ConverterBase[PartialMember]` [invalid-inheritance]
- ERROR steam/ext/commands/converters.py:294:7-29: Class `PartialMemberConverter` has inconsistent type arguments for base class `Converter`: `Converter[PartialUser]` and `Converter[PartialMember]` [invalid-inheritance]
- ERROR steam/ext/commands/converters.py:302:7-22: Class `MemberConverter` has inconsistent type arguments for base class `ConverterBase`: `ConverterBase[PartialUser]` and `ConverterBase[Member]` [invalid-inheritance]
- ERROR steam/ext/commands/converters.py:302:7-22: Class `MemberConverter` has inconsistent type arguments for base class `Converter`: `Converter[PartialUser]` and `Converter[Member]` [invalid-inheritance]
- ERROR steam/ext/commands/converters.py:389:7-26: Class `PartialAppConverter` has inconsistent type arguments for base class `ConverterBase`: `ConverterBase[App]` and `ConverterBase[PartialApp[str]]` [invalid-inheritance]
- ERROR steam/ext/commands/converters.py:389:7-26: Class `PartialAppConverter` has inconsistent type arguments for base class `Converter`: `Converter[App]` and `Converter[PartialApp[str]]` [invalid-inheritance]
meson (https://github.com/mesonbuild/meson)
- ERROR mesonbuild/utils/universal.py:716:7-33: Class `PerThreeMachineDefaultable` has inconsistent type arguments for base class `PerMachine`: `PerMachine[_T | None | None]` and `PerMachine[_T | None]` [invalid-inheritance] |
This branch has not been deployed
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 #4842
Problem
When two base paths reach the same generic ancestor, pyrefly compared the two specializations with plain type-argument equality:
Baseis contravariant in its first parameter, soBase[SupportsIndex | SupportsInt, int]is a subtype ofBase[int, int]and the diamond is consistent — pyright accepts it (and this pattern occurs in the wild, e.g. click'sParamTypehierarchy).Change
The conflict predicate inside the C3-linearization check now delegates to a variance-aware comparison callback (constructed where solver access exists):
VarianceMapthe subset check uses);Anyremains gradual; MRO construction (C3) and the first-wins ancestor selection are unchanged.One pre-existing expectation is updated deliberately: the
Iterable[int]vsIterable[int | str]diamond intuple.rsis a covariant-legal pair under the adopted semantics (pyright-compatible), so it no longer errors.Test plan
intvsstrin a contravariant position (still errors, same message), anAnydiamond (no-regression pin)generic_basic.rs,tuple.rsminus the covariant-legal case above) pass uneditedcargo test -p pyrefly --lib -- --skip test::lsp: 7744 passed, 0 failed (the twomissing-sourceLSP interaction failures also fail on unmodifiedmain)