pyrefly icon indicating copy to clipboard operation
pyrefly copied to clipboard

`type[Self]` converted to a callable drops constructor signature

Open crypt0lith opened this issue 3 weeks ago • 3 comments

Describe the Bug

from typing import Callable


def identity[**P, R](cb: Callable[P, R]) -> Callable[P, R]:
    return cb


class Foo:
    def __init__(self, value: int) -> None: ...

    @classmethod
    def meth(cls) -> None:
        reveal_type(cls)    # type[Self@Foo]
        reveal_type(identity(cls))  # (*args: Any, **kwds: Any) -> Any
        cls("") # error, expected
        f: Callable[[str], Foo] = cls   # no error


def explicit(t: type[Foo]) -> None:
    reveal_type(identity(t))  # (value: int) -> Foo

When a variable declared type[Self] (such as the cls parameter of a classmethod) is used where a callable is expected, pyrefly treats it as type.__call__ instead of using the constructor of the class bound to Self.

Sandbox Link

https://pyrefly.org/sandbox/?project=v2.pZU_b4MwEMW_yqkMuFaEd6ZMnaOuqENq3NRqhKPi_sm3z_MdDgGhRFFYEJbf4fO9390dLTM3j1QCcapvXRd9PDZab1b0-qbse33e1vASO2C6lC9W7GWB-cjmSwg3uUSRR19RVVUZvjXHEPymOCq775dAvmidsgVPkdJ2TWrbaxwHJVvanVNnGXQFKQ2G0XF4smj99dfKB_8X7zEOJAo1fHqGin2xIvd_gBXccOz0fFzcJDcwjCacBiaAnI_ZBVGP9UCUvbc-qlhLEkkxz3sxizjkMLti6NlqM6MXtJEFGjyDFg9Cdj8YJHA-ClJghjj6jPHQ18ZkefjeGdeZNtjeTBSGEtPb363nhKfhKDCYPcI-QlgpgJXX-CoTXthxFZkT

(Only applicable for extension issues) IDE Information

No response

crypt0lith avatar Nov 09 '26 08:11 crypt0lith

#claim

The fix would be adding a new arm for SelfType after the existing one: https://github.com/meta/pyrefly/blob/7431fb02e1d2e9295d1489894d0e8fdbd30aba7c/pyrefly/lib/solver/subset.rs#L2594-L2596

crypt0lith avatar Nov 09 '26 08:11 crypt0lith

@crypt0lith you've claimed this issue — it's now assigned to you. Thanks for picking it up! 🎉

github-actions[bot] avatar Nov 09 '26 08:11 github-actions[bot]

Thanks for the report, this reproduces on the latest release and on main: sandbox

@crypt0lith thanks for picking it up!

shahhamdihassan avatar Nov 09 '26 22:11 shahhamdihassan