Skip to content

Commit d87f0b2

Browse files
authored
[docs] Fix metaclass usage example (#18686)
Fixes #18668 - Fixed the code example to _not_ include `Self` - Added a note about `Self` & metaclasses in Gotchas section with a link to the relevant PEP
1 parent 2aab130 commit d87f0b2

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

docs/source/metaclasses.rst

+7-5
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ Mypy supports the lookup of attributes in the metaclass:
3434

3535
.. code-block:: python
3636
37-
from typing import ClassVar, Self
37+
from typing import ClassVar, TypeVar
38+
39+
S = TypeVar("S")
3840
3941
class M(type):
4042
count: ClassVar[int] = 0
4143
42-
def make(cls) -> Self:
44+
def make(cls: type[S]) -> S:
4345
M.count += 1
4446
return cls()
4547
@@ -55,9 +57,6 @@ Mypy supports the lookup of attributes in the metaclass:
5557
b: B = B.make() # metaclasses are inherited
5658
print(B.count + " objects were created") # Error: Unsupported operand types for + ("int" and "str")
5759
58-
.. note::
59-
In Python 3.10 and earlier, ``Self`` is available in ``typing_extensions``.
60-
6160
.. _limitations:
6261

6362
Gotchas and limitations of metaclass support
@@ -88,3 +87,6 @@ so it's better not to combine metaclasses and class hierarchies:
8887
such as ``class A(metaclass=f()): ...``
8988
* Mypy does not and cannot understand arbitrary metaclass code.
9089
* 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

0 commit comments

Comments
 (0)