Fix union of T | type[T] failing to match the type[T] branch#2471
Closed
kinto0 wants to merge 2 commits intofacebook:mainfrom
Closed
Fix union of T | type[T] failing to match the type[T] branch#2471kinto0 wants to merge 2 commits intofacebook:mainfrom
kinto0 wants to merge 2 commits intofacebook:mainfrom
Conversation
Summary: When a function parameter has type `T | type[T]` and a class (not an instance) is passed, pyrefly incorrectly matches against the `T` branch (solving T=type[Sub] which violates the bound) instead of the `type[T]` branch (which would correctly solve T=Sub). This test documents the bug. Reviewed By: stroxler Differential Revision: D93779358
Summary: When matching a value against a union containing both bare TypeVars and wrapped TypeVars (e.g. `T | type[T]`), the solver tried them in arbitrary order. If the bare `T` was tried first, it would eagerly pin the TypeVar to a type that violates bounds (e.g. `type[Sub]` instead of `Sub`), and since var-solving always returns Ok even on bound violations, the more specific `type[T]` branch was never tried. The fix partitions var-containing union members into bare vars (`T`) and wrapped vars (`type[T]`, `list[T]`, etc.), trying wrapped vars first. Wrapped forms are more specific — they extract the inner type from the argument (e.g. `type[T]` extracts the instance type from a class object), producing better TypeVar solutions that are more likely to satisfy bounds. fixes facebook#2398 Differential Revision: D93779356
|
Diff from mypy_primer, showing the effect of this PR on open source code: |
samwgoldman
approved these changes
Feb 20, 2026
Member
samwgoldman
left a comment
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
|
This pull request has been merged in c249117. |
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:
When matching a value against a union containing both bare TypeVars and wrapped TypeVars (e.g.
T | type[T]), the solver tried them in arbitrary order. If the bareTwas tried first, it would eagerly pin the TypeVar to a type that violates bounds (e.g.type[Sub]instead ofSub), and since var-solving always returns Ok even on bound violations, the more specifictype[T]branch was never tried.The fix partitions var-containing union members into bare vars (
T) and wrapped vars (type[T],list[T], etc.), trying wrapped vars first. Wrapped forms are more specific — they extract the inner type from the argument (e.g.type[T]extracts the instance type from a class object), producing better TypeVar solutions that are more likely to satisfy bounds.fixes #2398
Differential Revision: D93779356