[ty] Ensure a type[] type T is always considered assignable to a union that holds T and a protocol P#23901
Merged
AlexWaygood merged 2 commits intomainfrom Mar 12, 2026
Merged
[ty] Ensure a type[] type T is always considered assignable to a union that holds T and a protocol P#23901AlexWaygood merged 2 commits intomainfrom
type[] type T is always considered assignable to a union that holds T and a protocol P#23901AlexWaygood merged 2 commits intomainfrom
Conversation
Typing conformance resultsNo changes detected ✅Current numbersThe percentage of diagnostics emitted that were expected errors held steady at 85.29%. The percentage of expected errors that received a diagnostic held steady at 78.13%. The number of fully passing files held steady at 64/132. |
|
Memory usage reportMemory usage unchanged ✅ |
dcreager
approved these changes
Mar 12, 2026
Comment on lines
+253
to
+254
| /// Allow unsound upcasts to callables, such as treating `type[]` as | ||
| /// `Callable[<constructor signature>, <instance of class>]`. |
Member
There was a problem hiding this comment.
No concerns with the approach or the implementation. But this describes how some upcasts are unsound, but doesn't describe why or when we want those. Do we have a good handle on that that we could persist as a comment here? (If that would take too much analysis I'm fine punting on that, though it might still deserve a TODO comment to figure out whether any existing unsound uses could migrate to sound)
Member
Author
There was a problem hiding this comment.
Thank you! I agree the docs weren't great. I spruced 'em up a bit
…union that holds `T` and a protocol `P`
812ef18 to
840d674
Compare
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 astral-sh/ty#2981. Since the bug exists for both protocols and Callables, I pulled out the special-case logic inserted into
relation.rsin #23834 intoType::try_upcast_to_callable()itself. But there are many uses of that method across the codebase, and nearly all uses do not care about unsound upcasts, so rather than forcing all callers to pass apolicyargument totry_upcast_to_callable(), I instead added a new method,try_upcast_to_callable_with_policy, which is now called byType::try_upcast_to_callable.Test Plan
mdtests extended