Repository navigation
Conversation
Contributor
|
This pull request has been imported. If you are a Meta employee, you can view this in D124639375. (Because this pull request was imported automatically, there will not be any future comments.) |
i098
force-pushed
the
fix/pyrefly-5053
branch
from
October 11, 2026 13:24
c707463 to
ef60120
Compare
When a class with overloaded `__new__` is converted to a callable, as in `Callable[[str], C[int]] = C[str]` or `partial(C[str], "x")`, the overloads whose `cls` annotation does not match the specialized class are still kept. This test captures the expected behavior from issue 5053 and currently fails.
…lable Converting a class with an overloaded `__new__` to a callable kept every overload, even those whose `cls` annotation cannot accept the class being converted. For `class C[T]` with `__new__` overloads on `type[C[int]]` and `type[C[str]]`, `C[str]` was accepted as `Callable[[str], C[int]]`, and `partial(C[str], "x")()` was inferred as `C[int]`. `bind_dunder_new` now drops the overloads whose first parameter does not accept the class object. Type parameters on either side are treated as `Any`, so overloads without a `cls` annotation, overloads that use their own type variables, and bare generic classes keep their overloads. If no overload would remain, the original overload is kept. Fixes facebook#5053
i098
force-pushed
the
fix/pyrefly-5053
branch
from
October 20, 2026 06:36
ef60120 to
18e5dff
Compare
This branch has not been deployed
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
Converting a class with an overloaded
__new__to a callable kept every overload, even when theclsannotation of an overload cannot accept the class being converted. For the class in the issue,C[str]was treated as the constructor ofC[int]as well.bind_dunder_new(used byconstructor_to_callable) now drops the overloads whose first parameter does not accept the class object. Type parameters are treated asAnyon both sides, so these cases keep all their overloads: noclsannotation,cls: type[S]with the overload's own type variable, and a bare generic class such asC. If no overload would remain, the original overload is kept.Repro, with
preset = "default"inpyproject.toml:Before: no error on
wrong, andrevealed type: C[int].After:
Fixes #5053
Test Plan
test_overloaded_new_cls_annotation_as_callable, which fails without the fix (thewrongassignment is accepted andpartial(...)()isC[int]). The second commit fixes it.test_overloaded_new_cls_annotation_as_callable_variantscovers the cases that must keep working: noclsannotation, a bare generic class,type[C[Any]], a type-variablecls, an__init__-only class, and a subclass that inherits the overloaded__new__.cargo test -p pyrefly --lib: 9069 passed, 0 failed.python3 test.py: passes in full (exit 0), including scrut (204 of 204), tensor shapes, conformance with no generated-file changes, jsonschema and the extension helper tests.I am new to open source, and this pull request was made with AI assistance.