Skip to content

Fix no-matching-overload false positive for inline generic constructor calls#3036

Open
Arths17 wants to merge 4 commits intofacebook:mainfrom
Arths17:Arths17/issue3002
Open

Fix no-matching-overload false positive for inline generic constructor calls#3036
Arths17 wants to merge 4 commits intofacebook:mainfrom
Arths17:Arths17/issue3002

Conversation

@Arths17
Copy link
Copy Markdown
Contributor

@Arths17 Arths17 commented Apr 7, 2026

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 example T | Box[T]), pyrefly could over-specialize class type arguments from the full contextual hint and incorrectly report no-matching-overload on __init__.

This change makes constructor hint pre-specialization opt-in and conservative:

  • only use a hint branch that is an instance of the same class being constructed
  • require that hint branch to be concrete (no type variables / solver vars)
  • otherwise skip hint pre-specialization and proceed with normal constructor inference

Tests

  • Added regression test test_init_overload_inline_constructor_with_union_hint in pyrefly/lib/test/constructors.rs
  • Ran:
    • cargo test test_init_overload_inline_constructor_with_union_hint --manifest-path pyrefly/Cargo.toml
    • cargo test test::constructors:: --manifest-path pyrefly/Cargo.toml

Both passed locally.

Fixes #3002

…orrectly constrains `__init__` overload resolution

Fixes facebook#3002
Copilot AI review requested due to automatic review settings April 7, 2026 00:18
@meta-cla meta-cla bot added the cla signed label Apr 7, 2026
@github-actions github-actions bot added the size/s label Apr 7, 2026
@github-actions github-actions bot added size/s and removed size/s labels Apr 7, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.
@github-actions github-actions bot added size/m and removed size/s labels Apr 7, 2026
Apply rustfmt's line wrapping to the constructor hint filtering change so the branch passes formatting checks without altering behavior.
@github-actions github-actions bot added size/s and removed size/m labels Apr 7, 2026
@stroxler
Copy link
Copy Markdown
Contributor

stroxler commented Apr 8, 2026

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

no-matching-overload false positive: expected type _T | C[_T] incorrectly constrains __init__ overload resolution

3 participants