Fix no-matching-overload false positive for inline generic constructor calls#3036
Open
Arths17 wants to merge 4 commits intofacebook:mainfrom
Open
Fix no-matching-overload false positive for inline generic constructor calls#3036Arths17 wants to merge 4 commits intofacebook:mainfrom
Arths17 wants to merge 4 commits intofacebook:mainfrom
Conversation
…orrectly constrains `__init__` overload resolution Fixes facebook#3002
There was a problem hiding this comment.
Pull request overview
Fixes a no-matching-overload false positive during constructor overload resolution when an inline generic constructor call is type-checked under a union contextual hint, by making constructor hint pre-specialization conservative/opt-in.
Changes:
- Filters contextual constructor hints to only pre-specialize from union branches that are instances of the same class being constructed.
- Requires the selected hint branch to be “concrete” (no type variables / quantified vars) before applying subset constraints to class type arguments.
- Skips hint pre-specialization entirely when no suitable hint branch is found.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
When a contextual hint contains multiple concrete branches of the same class (for example Box[int] | Box[str]), selecting the first branch makes constructor inference dependent on union member ordering and can reintroduce overload false positives. Only pre-specialize constructor class type arguments when there is exactly one concrete same-class hint branch, and add regression coverage for both the original inline union-hint case and the multi-branch ambiguity case.
Apply rustfmt's line wrapping to the constructor hint filtering change so the branch passes formatting checks without altering behavior.
Contributor
|
Thanks for the work @Arths17! I think a test changed - this might be an okay change, but I think it's worth taking a look and updating the expectations if so. Also, if you rebase I think the mypy_primer (7) failure should be fixed now |
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 a constructor overload resolution false positive reported in #3002.
When a constructor call like
Box({1: 1})appears inline in a context with a union hint (for exampleT | Box[T]), pyrefly could over-specialize class type arguments from the full contextual hint and incorrectly reportno-matching-overloadon__init__.This change makes constructor hint pre-specialization opt-in and conservative:
Tests
test_init_overload_inline_constructor_with_union_hintinpyrefly/lib/test/constructors.rscargo test test_init_overload_inline_constructor_with_union_hint --manifest-path pyrefly/Cargo.tomlcargo test test::constructors:: --manifest-path pyrefly/Cargo.tomlBoth passed locally.
Fixes #3002