Skip to content

Spec and conformance tests disagree on converting constructors into callable for classes with __new__ and __init__ #2158

@fangyi-zhou

Description

@fangyi-zhou

For classes with both __new__ and __init__, when converting the constructor into a Callable, the spec says that

https://github.com/python/typing/blame/20096eeba502b1bf955fcf62b389adbaa63fc61b/docs/spec/constructors.rst#L457

    class B:
        """ __new__ and __init__ """
        def __new__(cls, *args, **kwargs) -> Self:
            ...

        def __init__(self, x: int) -> None:
            ...

    reveal_type(accepts_callable(B))  # ``def (*args, **kwargs) -> B | def (x: int) -> B``

The conformance test says that

class Class3:
"""__new__ and __init__"""
def __new__(cls, *args, **kwargs) -> Self:
raise NotImplementedError
def __init__(self, x: int) -> None: ...
r3 = accepts_callable(Class3)
reveal_type(r3) # `def (x: int) -> Class3`

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions