| 1 |
.. _metaclasses: |
| 2 |
|
| 3 |
Metaclasses |
| 4 |
=========== |
| 5 |
|
| 6 |
A :ref:`metaclass <python:metaclasses>` is a class that describes |
| 7 |
the construction and behavior of other classes, similarly to how classes |
| 8 |
describe the construction and behavior of objects. |
| 9 |
The default metaclass is :py:class:`type`, but it's possible to use other metaclasses. |
| 10 |
Metaclasses allows one to create "a different kind of class", such as |
| 11 |
:py:class:`~enum.Enum`\s, :py:class:`~typing.NamedTuple`\s and singletons. |
| 12 |
|
| 13 |
Mypy has some special understanding of :py:class:`~abc.ABCMeta` and ``EnumMeta``. |
| 14 |
|
| 15 |
.. _defining: |
| 16 |
|
| 17 |
Defining a metaclass |
| 18 |
******************** |
| 19 |
|
| 20 |
.. code-block:: python |
| 21 |
|
| 22 |
class M(type): |
| 23 |
pass |
| 24 |
|
| 25 |
class A(metaclass=M): |
| 26 |
pass |
| 27 |
|
| 28 |
.. _examples: |
| 29 |
|
| 30 |
Metaclass usage example |
| 31 |
*********************** |
| 32 |
|
| 33 |
Mypy supports the lookup of attributes in the metaclass: |
| 34 |
|
| 35 |
.. code-block:: python |
| 36 |
|
| 37 |
from typing import ClassVar, TypeVar |
| 38 |
|
| 39 |
S = TypeVar("S") |
| 40 |
|
| 41 |
class M(type): |
| 42 |
count: ClassVar[int] = 0 |
| 43 |
|
| 44 |
def make(cls: type[S]) -> S: |
| 45 |
M.count += 1 |
| 46 |
return cls() |
| 47 |
|
| 48 |
class A(metaclass=M): |
| 49 |
pass |
| 50 |
|
| 51 |
a: A = A.make() # make() is looked up at M; the result is an object of type A |
| 52 |
print(A.count) |
| 53 |
|
| 54 |
class B(A): |
| 55 |
pass |
| 56 |
|
| 57 |
b: B = B.make() # metaclasses are inherited |
| 58 |
print(B.count + " objects were created") # Error: Unsupported operand types for + ("int" and "str") |
| 59 |
|
| 60 |
.. _limitations: |
| 61 |
|
| 62 |
Gotchas and limitations of metaclass support |
| 63 |
******************************************** |
| 64 |
|
| 65 |
Note that metaclasses pose some requirements on the inheritance structure, |
| 66 |
so it's better not to combine metaclasses and class hierarchies: |
| 67 |
|
| 68 |
.. code-block:: python |
| 69 |
|
| 70 |
class M1(type): pass |
| 71 |
class M2(type): pass |
| 72 |
|
| 73 |
class A1(metaclass=M1): pass |
| 74 |
class A2(metaclass=M2): pass |
| 75 |
|
| 76 |
class B1(A1, metaclass=M2): pass # Mypy Error: metaclass conflict |
| 77 |
# At runtime the above definition raises an exception |
| 78 |
# TypeError: metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases |
| 79 |
|
| 80 |
class B12(A1, A2): pass # Mypy Error: metaclass conflict |
| 81 |
|
| 82 |
# This can be solved via a common metaclass subtype: |
| 83 |
class CorrectMeta(M1, M2): pass |
| 84 |
class B2(A1, A2, metaclass=CorrectMeta): pass # OK, runtime is also OK |
| 85 |
|
| 86 |
* Mypy does not understand dynamically-computed metaclasses, |
| 87 |
such as ``class A(metaclass=f()): ...`` |
| 88 |
* Mypy does not and cannot understand arbitrary metaclass code. |
| 89 |
* Mypy only recognizes subclasses of :py:class:`type` as potential metaclasses. |
| 90 |
* ``Self`` is not allowed as annotation in metaclasses as per `PEP 673`_. |
| 91 |
|
| 92 |
.. _PEP 673: https://peps.python.org/pep-0673/#valid-locations-for-self |
| 93 |
|
| 94 |
For some builtin types, mypy may think their metaclass is :py:class:`abc.ABCMeta` |
| 95 |
even if it is :py:class:`type` at runtime. In those cases, you can either: |
| 96 |
|
| 97 |
* use :py:class:`abc.ABCMeta` instead of :py:class:`type` as the |
| 98 |
superclass of your metaclass if that works in your use-case |
| 99 |
* mute the error with ``# type: ignore[metaclass]`` |
| 100 |
|
| 101 |
.. code-block:: python |
| 102 |
|
| 103 |
import abc |
| 104 |
|
| 105 |
assert type(tuple) is type # metaclass of tuple is type at runtime |
| 106 |
|
| 107 |
# The problem: |
| 108 |
class M0(type): pass |
| 109 |
class A0(tuple, metaclass=M0): pass # Mypy Error: metaclass conflict |
| 110 |
|
| 111 |
# Option 1: use ABCMeta instead of type |
| 112 |
class M1(abc.ABCMeta): pass |
| 113 |
class A1(tuple, metaclass=M1): pass |
| 114 |
|
| 115 |
# Option 2: mute the error |
| 116 |
class M2(type): pass |
| 117 |
class A2(tuple, metaclass=M2): pass # type: ignore[metaclass] |