Skip to content

Commit

Permalink
Spec: Fix formatting in constructors chapter (#1709)
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra committed Apr 11, 2024
1 parent 7a70cd6 commit 30283a1
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions docs/spec/constructors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ Constructor Calls

At runtime, a call to a class' constructor typically results in the invocation of
three methods in the following order:
1. The ``__call__`` method of the metaclass (which is typically supplied by the
``type`` class but can be overridden by a custom metaclass and which is
responsible for calling the next two methods)
2. The ``__new__`` static method of the class
3. The ``__init__`` instance method of the class

#. The ``__call__`` method of the metaclass (which is typically supplied by the
``type`` class but can be overridden by a custom metaclass and which is
responsible for calling the next two methods)
#. The ``__new__`` static method of the class
#. The ``__init__`` instance method of the class

Type checkers should mirror this runtime behavior when analyzing a constructor
call.
Expand All @@ -31,7 +32,7 @@ assume that the metaclass ``__call__`` method is overriding ``type.__call__``
in some special manner, and it should not attempt to evaluate the ``__new__``
or ``__init__`` methods on the class. For example, some metaclass ``__call__``
methods are annotated to return ``NoReturn`` to indicate that constructor
calls are not supported for that class.
calls are not supported for that class.

::

Expand Down Expand Up @@ -285,7 +286,7 @@ does not inherit either of these methods from a base class other than

class MyClass5:
pass

MyClass5() # OK
MyClass5(1) # Type error

Expand Down Expand Up @@ -417,15 +418,15 @@ constructor calls:
class A:
""" No __new__ or __init__ """
pass

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

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

class C:
""" __new__ but no __init__ """
def __new__(cls, x: int) -> int:
Expand All @@ -451,7 +452,7 @@ constructor calls:
def __init__(self, x: int) -> None:
""" This __init__ is ignored for purposes of conversion """
...


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

0 comments on commit 30283a1

Please sign in to comment.