Skip to content

type[Self] converted to a callable drops constructor signature #5591

Closed

Description

@crypt0lith

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

Activity

  1. crypt0lith commented on Nov 9, 2026

    @crypt0lith
    ContributorAuthor

    #claim

    The fix would be adding a new arm for SelfType after the existing one:

    // Constructors as callables
    (Type::Type(inner), Type::BoundMethod(_) | Type::Callable(_) | Type::Function(_))
    if let Type::ClassType(got_cls) = &**inner =>

  2. github-actions commented on Nov 9, 2026

    @github-actions

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

  3. shahhamdihassan commented on Nov 9, 2026

    @shahhamdihassan
    Contributor

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

    @crypt0lith thanks for picking it up!

  4. added a commit that references this issue on Nov 11, 2026
    905e2d5
  5. added a commit that references this issue on Nov 11, 2026
    70dcfe7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions