Skip to content

Allow subclass receivers on overloaded constructors - #4722

Open
Divyansh151005 wants to merge 1 commit into
facebook:mainfrom
Divyansh151005:fix/constructor-overload-subclass-receivers
Open

Allow subclass receivers on overloaded constructors#4722
Divyansh151005 wants to merge 1 commit into
facebook:mainfrom
Divyansh151005:fix/constructor-overload-subclass-receivers

Conversation

@Divyansh151005

@Divyansh151005 Divyansh151005 commented Aug 29, 2026

Copy link
Copy Markdown

Summary

Overloaded ordinary methods and classmethods already skip self/cls superclass validation on overload stubs, so concrete subclass receivers can participate in overload selection. Overloaded __new__ and __init__ were still always validated (should_validate = is_constructor || !is_overload), which rejected valid patterns such as:

class Path:
    @overload
    def __new__(cls: type[AbsPath], path: str) -> AbsPath: ...
    ...

This change treats constructor overload stubs like other overload stubs: skip per-overload receiver superclass checks, while still validating non-overload constructors and implementation signatures.

Fixes #4701

Test Plan

$ export CARGO_TARGET_DIR=/Users/divyanshbarodiya/Desktop/pyrefly-cargo-target
$ cargo test -p pyrefly --lib subclass_receiver
test test::constructors::test_new_overload_subclass_receiver ... ok
test test::constructors::test_init_overload_subclass_receiver ... ok
test result: ok. 2 passed; 0 failed

$ cargo test -p pyrefly --lib bad_receiver_annotation
test test::constructors::test_init_bad_receiver_annotation ... ok
test test::constructors::test_new_bad_receiver_annotation ... ok
test result: ok. 2 passed; 0 failed

$ cargo test -p pyrefly --lib test::constructors::
test result: ok. 90 passed; 0 failed

$ python3 test.py --no-test --no-tensor-shapes --no-conformance --no-jsonschema
Finished successfully

Overloaded ordinary methods already skip self/cls superclass checks so
receiver annotations can participate in overload selection. Apply the
same rule to overloaded __new__/__init__ stubs while still validating
non-overload constructors and implementations.

Co-authored-by: Cursor <cursoragent@cursor.com>
@meta-cla

meta-cla Bot commented Aug 29, 2026

Copy link
Copy Markdown

Hi @Divyansh151005!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla

meta-cla Bot commented Aug 29, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

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.

Constructor overloads reject subclass receiver annotations accepted on ordinary overloads

2 participants