Skip to content

ty should infer the type of the cls parameter of __new__ #2489

@arjenzorgdoc

Description

@arjenzorgdoc

The cls parameter of __new__ should be inferred as type[Self]. ty says it's Unknown

from typing import reveal_type

class C:
    def __new__(cls):
        reveal_type(cls)
        return super().__new__(cls)
  • pyright: Type of "cls" is "type[Self@C]"
  • ty: Revealed type: Unknown (revealed-type) [Ln 6, Col 21]
  • mypy: main.py:5: note: Revealed type is "type[__main__.C]"
My actual use case is code like this:
from typing import Self


class C:
    def __new__(cls: type[Self], x: object) -> Self:
        if type(x) is cls:
            return x
        return super().__new__(cls)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions