Skip to content

[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
unsound-upcast-2
Mar 12, 2026
Merged

[ty] Ensure a type[] type T is always considered assignable to a union that holds T and a protocol P#23901
AlexWaygood merged 2 commits intomainfrom
unsound-upcast-2

Conversation

@AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Mar 11, 2026

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.rs in #23834 into Type::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 a policy argument to try_upcast_to_callable(), I instead added a new method, try_upcast_to_callable_with_policy, which is now called by Type::try_upcast_to_callable.

Test Plan

mdtests extended

@AlexWaygood AlexWaygood added the ty Multi-file analysis & type inference label Mar 11, 2026
@astral-sh-bot
Copy link

astral-sh-bot bot commented Mar 11, 2026

Typing conformance results

No changes detected ✅

Current numbers
The 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.

@astral-sh-bot
Copy link

astral-sh-bot bot commented Mar 11, 2026

mypy_primer results

Changes were detected when running on open source projects
scikit-build-core (https://github.com/scikit-build/scikit-build-core)
- src/scikit_build_core/build/wheel.py:99:20: error[no-matching-overload] No overload of bound method `__init__` matches arguments
- Found 60 diagnostics
+ Found 59 diagnostics

@astral-sh-bot
Copy link

astral-sh-bot bot commented Mar 11, 2026

Memory usage report

Memory usage unchanged ✅

@AlexWaygood AlexWaygood added the bug Something isn't working label Mar 11, 2026
@AlexWaygood AlexWaygood marked this pull request as ready for review March 11, 2026 20:06
Comment on lines +253 to +254
/// Allow unsound upcasts to callables, such as treating `type[]` as
/// `Callable[<constructor signature>, <instance of class>]`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! I agree the docs weren't great. I spruced 'em up a bit

@AlexWaygood AlexWaygood enabled auto-merge (squash) March 12, 2026 14:31
@AlexWaygood AlexWaygood merged commit 88d1eec into main Mar 12, 2026
50 checks passed
@AlexWaygood AlexWaygood deleted the unsound-upcast-2 branch March 12, 2026 14:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[failing property test] subclass-of types cannot be considered subtypes of the Callable type of their constructors

2 participants