[ty] Fix false positive for bounded type parameters with NewType#22542
Merged
[ty] Fix false positive for bounded type parameters with NewType#22542
Conversation
…king When calling a method on an instance of a generic class with bounded type parameters (e.g., `C[T: K]` where `K` is a NewType), ty was incorrectly reporting: "Argument type `C[K]` does not satisfy upper bound `C[T@C]` of type variable `Self`" The issue was introduced by PR #22105, which moved the catch-all case for NewType assignments that falls back to the concrete base type. This case was placed before the TypeVar handling cases, so when checking `K <: T@C` (where K is a NewType and T@C is a TypeVar with upper bound K): 1. The NewType fallback matched first 2. It delegated to `int` (K's concrete base type) 3. Then checked `int <: T@C`, which checks if `int` satisfies bound `K` 4. But `int` is not assignable to `K` (NewTypes are distinct from their bases) The fix moves the NewType fallback case after the TypeVar cases, so TypeVar handling takes precedence. Fixes astral-sh/ty#2467
Diagnostic diff on typing conformance testsNo changes detected when running ty on typing conformance tests ✅ |
|
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 astral-sh/ty#2467
When calling a method on an instance of a generic class with bounded type parameters (e.g.,
C[T: K]whereKis a NewType), ty was incorrectly reporting: "Argument typeC[K]does not satisfy upper boundC[T@C]of type variableSelf"The issue was introduced by PR #22105, which moved the catch-all case for NewType assignments that falls back to the concrete base type. This case was moved before the TypeVar handling cases, so when checking
K <: T@C(where K is a NewType and T@C is a TypeVar with upper bound K):int(K's concrete base type)int <: T@C, which checks ifintsatisfies boundKintis not assignable toK(NewTypes are distinct from their bases)The fix moves the NewType fallback case after the TypeVar cases, so TypeVar handling takes precedence. Now when checking
K <: T@C, we use the TypeVar case at line 828 which returnsfalsefor non-inferable typevars - but this is correct because the other direction (T@C <: K) passes, and for the overall specialization comparison both directions are checked.