Skip to content

Cannot access "__init__" directly #13173

@gsakkis

Description

@gsakkis

Calling __init__ explicitly is a valid (albeit uncommon) pattern, for example after calling explicitly __new__; however mypy considers it an error:

from __future__ import annotations


class Foo:
    def __init__(self, a: int):
        self.a = a


class Bar(Foo):
    def __new__(cls, a: int) -> Bar:
        self = super().__new__(cls)
        # ....
        self.__init__(a)
        # ....
        return self


if __name__ == '__main__':
    print(Bar(1))

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions