Skip to content

Check consistency of __new__ and __init__ #5648

Closed

Description

@msullivan

We permit the following program:

class A:
    def __init__(self, x: int) -> None:
        pass

class B(A):
    def __new__(cls) -> 'B':
        return object.__new__(cls)

B()

even though it crashes at runtime.

Per #5642 (review):
The rule CPython uses is this (for a class C with superclass B, where B is not object):

  • If both C.__new__ and C.__init__ refer to their versions in object, the signature is def () -> C.
  • If exactly one of C.__new__ and C.__object__ refers to its object version, the other determines the signature.
  • If both override their corresponding object versions, they must match -- even if they don't come from the same class (say, if one comes from C and the other from B).

Activity

  1. msullivan commented on Sep 19, 2018

    @msullivan
    CollaboratorAuthor

    Actually closing as a dupe of #1019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions