Skip to content

Convert type[Self] to callable from class constructor - #5592

Open
crypt0lith wants to merge 1 commit into
meta:mainfrom
crypt0lith:fix/self-type-as-callable
Open

crypt0lith wants to merge 1 commit into
meta:mainfrom
crypt0lith:fix/self-type-as-callable

Conversation

@crypt0lith

Copy link
Copy Markdown

Summary

Converting type[Self], such as cls in a classmethod, to a callable fell back to type.__call__, (*args: Any, **kwds: Any) -> Any. Passing cls to a ParamSpec-generic function lost its signature, and assigning it to an incompatible callable type was allowed.

Convert type[Self] from the constructor of the class bound to Self. As in a call through type[Self], change a constructor return that is an instance of the class to Self, so the converted callable still returns the subclass when the classmethod is inherited.

Fixes #5591

  • I am a human, and I fully understand the code I am submitting.

Test Plan

cargo test -p pyrefly --lib test::constructors::test_type_of_self_as_callable

Converting type[Self], such as 'cls' in a classmethod, to a callable fell
back to 'type.__call__', '(*args: Any, **kwds: Any) -> Any'. Passing 'cls'
to a ParamSpec-generic function lost its signature, and assigning it to an
incompatible callable type was allowed.

Convert type[Self] from the constructor of the class bound to Self. As in
a call through type[Self], change a constructor return that is an instance
of the class to Self, so the converted callable still returns the subclass
when the classmethod is inherited.

Fixes meta#5591
@meta-codesync

meta-codesync Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D130651720. (Because this pull request was imported automatically, there will not be any future comments.)

This branch has not been deployed

No deployments
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.

type[Self] converted to a callable drops constructor signature

1 participant