Skip to content

Conformance: class constructor to callable conversion doesn't fully work #2450

@migeed-z

Description

@migeed-z

Describe the Bug

The following code should fully type check (unittest can be found in pyrefly/pyrefly/lib/test/callable.rs)

from typing import Callable, ParamSpec, TypeVar, Self, assert_type, overload, Generic

P = ParamSpec("P")
R = TypeVar("R")
T = TypeVar("T")

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

class Class3:
    def __new__(cls, *args, **kwargs) -> Self: ...
    def __init__(self, x: int) -> None: ...

r3 = accepts_callable(Class3)

class Class7(Generic[T]):
    @overload
    def __init__(self: "Class7[int]", x: int) -> None: ...
    @overload
    def __init__(self: "Class7[str]", x: str) -> None: ...
    def __init__(self, x: int | str) -> None:
        pass

r7 = accepts_callable(Class7)
# pyrefly incorrectly errors on these - should be OK
assert_type(r7(""), Class7[str])  # E: assert_type(Class7[int], Class7[str]) failed 

class Class8(Generic[T]):
    def __new__(cls, x: list[T], y: list[T]) -> Self:
        return super().__new__(cls)

r8 = accepts_callable(Class8)
# pyrefly incorrectly errors on this - should be OK
assert_type(r8([""], [""]), Class8[str])  # E: assert_type(Class8[Any], Class8[str]) failed

Sandbox Link

https://pyrefly.org/sandbox/?project=N4IgZglgNgpgziAXKOBDAdgEwEYHsAeAdAA4CeS4ATrgLYAEALqcROgOZ0Q3G6UN0BhVFCipssADR0ACqkqoaAZWIwAxlIAqzGADU5UxTChgpqOHBh8A%2BkxVTcAN0tRcqTFIDiMdJYiqAOuiB0nQAvDJyCspqABT%2BINLxAJSBAEphdFoqepRxIKnJgRoZWbpyeRqFQVgwYHSoqqowxAxwVqrCouIwMarYiIKdYrAA2tJSqQC6SXQAtAB8gyLDMGMTk4iBdNt0lDAMAK6U6HR9gYGqouaCV3AAzJsnO5i1dFZWPgDu771QcFIAKjkbH%2BdABAIA1p9gXAZgs6IZjANCCits9Xu9WBAGD8LMYpPgBqwGHDFgA5XA%2BZGo6qUO4ZBpNFptDrLboxAS3O4paqXMxwG78gDsMS8PkofhGGmmjx2dAAAo5nK5MGjti86pj0NjcUYwAN4pzhSNiZN4gSiegSXNyZSYNTCGqFUrKC43E6NW8rFicVYYnj9XRDbchSM4AxKGaQBa6OHKKS6BSqXQUY6nuqMd7tb7-XqY8S6AAfWMRhNJ%2B1OuXEfnndCUIUMxrNVrtIbso3mIU8gDEdDIezAUFInHQql4e1UDCHdEs1EoAspjAAFvAYHNY0vcAcoJg6Ng1wB5ADSgX5lhxth69byySkHbgobj022vYAogMz9ZLxyQyarZM71-J8ZjAVBoBgXdaz5a57wADlFbxfFUKUZQ9TMvh%2BS5QUJOgoAgcMUKkUgBjwgjpQTRF9UrHY9kOY5YwOFRciSQh3gwv0sJ5QJKFgxsmRbVkulgH9%2BVgns%2B1IAdp1YMdKAnKdh1nXgFxOBgl3w9c4E3bdd33Ohj1PcxzxsbQYh4mIRnieIALoSyQGspJANEsMI2fOg3w-Iyv1MuCRgAQXQUgbN84C6FA8DMGjEADgYaA4BIchEBAXsAFVYrwpgwoOUdYspOBa09MBeBoVAcXQA4aH3XIcOJBM41lGj9iOE4wHiMkKqqgZgHwABfKz0Ci-taiHQgGFoKAKF7aRJOG4c0CwPB8FOSlIDYI5SogSk017Qw1yXBgGGIOBEAAehOobB1IQheDYE7vBOzBcFUOATrHdBVvW3L0BOsLeHqBwwKEtc3o%2B%2BQvroXAWk29B4sCMg1MpWYnHnaGMniO5CAARgAJn6kAeokEAGlipwADFwIoeacAIBL8aAA

(Only applicable for extension issues) IDE Information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions