[ty] fix assigning a typevar to a union with itself#17901
Closed
[ty] fix assigning a typevar to a union with itself#17901
Conversation
Contributor
|
Contributor
Author
|
Not a huge number of false positives from this, but the ecosystem results all look like fixing false positives. |
Contributor
|
What does this |
Contributor
Author
|
Member
|
It would be awesome if we could add a property test for this, but that obviously doesn't need to be done in this PR |
AlexWaygood
reviewed
May 7, 2025
| // implicit upper bound of `object` (which is handled above). | ||
| // | ||
| // This must be handled here, only if the above failed, because a typevar `T: int` needs to | ||
| // both a subtype of `int` and a subtype of e.g. `T | None`, so we need to also try |
Member
There was a problem hiding this comment.
Suggested change
| // both a subtype of `int` and a subtype of e.g. `T | None`, so we need to also try | |
| // be both a subtype of `int` and a subtype of e.g. `T | None`, so we need to also try |
Member
|
I proposed a more targeted fix which I like somewhat more in #17910 -- WDYT? |
Contributor
Author
|
Closing in favor of #17910 |
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
Noticed in #17800 that a typevar was not considered assignable to a union containing itself.
A bound typevar
T: intmust be assignable to e.g.int | Nonebut must also be assignable to e.g.T | None. There's no way to order the bound/constrained-typevar and union arms ofis_subtype_ofandis_assignable_tothat would achieve both of these; it requires trying the recursive union treatment for one possibility (treat the typevar as itself), and then if that fails, also trying it for the other possibility (treat the typevar as its bound/constraints).It doesn't work to just put the union case first and let the typevar bound/constraint handling happen one level deeper, because then we fail to consider e.g.
T: (X, Y)a subtype ofX | Y-- we would separately tryX | Y <: XandX | Y <: Y, and both fail. (There's already a test for this, fortunately, or I probably would have pushed the simpler fix that breaks it.)Test Plan
Added mdtest.