Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions docs/source/metaclasses.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ Mypy supports the lookup of attributes in the metaclass:

.. code-block:: python

from typing import ClassVar, Self
from typing import ClassVar, TypeVar

S = TypeVar("S")

class M(type):
count: ClassVar[int] = 0

def make(cls) -> Self:
def make(cls: type[S]) -> S:
M.count += 1
return cls()

Expand All @@ -55,9 +57,6 @@ Mypy supports the lookup of attributes in the metaclass:
b: B = B.make() # metaclasses are inherited
print(B.count + " objects were created") # Error: Unsupported operand types for + ("int" and "str")

.. note::
In Python 3.10 and earlier, ``Self`` is available in ``typing_extensions``.

.. _limitations:

Gotchas and limitations of metaclass support
Expand Down Expand Up @@ -88,3 +87,6 @@ so it's better not to combine metaclasses and class hierarchies:
such as ``class A(metaclass=f()): ...``
* Mypy does not and cannot understand arbitrary metaclass code.
* Mypy only recognizes subclasses of :py:class:`type` as potential metaclasses.
* ``Self`` is not allowed as annotation in metaclasses as per `PEP 673`_.

.. _PEP 673: https://peps.python.org/pep-0673/#valid-locations-for-self