Skip to content

Commit be8fdc4

Browse files
committed
fix docs nits
1 parent 79ab8b8 commit be8fdc4

File tree

2 files changed

+10
-15
lines changed

2 files changed

+10
-15
lines changed

docs/source/metaclasses.rst

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,33 +91,27 @@ so it's better not to combine metaclasses and class hierarchies:
9191

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

94-
For some builtin types, mypy assumes that their metaclass is :py:class:`abc.ABCMeta`
95-
even if it's :py:class:`type`. In those cases, you can either
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:
9696

9797
* 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]``, or
100-
* compute the metaclass' superclass dynamically, which mypy doesn't understand
101-
so it will also need to be muted.
98+
superclass of your metaclass if that works in your use-case
99+
* mute the error with ``# type: ignore[metaclass]``
102100

103101
.. code-block:: python
104102
105103
import abc
106104
107-
assert type(tuple) is type # metaclass of tuple is type
105+
assert type(tuple) is type # metaclass of tuple is type at runtime
108106
109-
# the problem:
107+
# The problem:
110108
class M0(type): pass
111-
class A0(tuple, metaclass=M1): pass # Mypy Error: metaclass conflict
109+
class A0(tuple, metaclass=M0): pass # Mypy Error: metaclass conflict
112110
113-
# option 1: use ABCMeta instead of type
111+
# Option 1: use ABCMeta instead of type
114112
class M1(abc.ABCMeta): pass
115113
class A1(tuple, metaclass=M1): pass
116114
117-
# option 2: mute the error
115+
# Option 2: mute the error
118116
class M2(type): pass
119117
class A2(tuple, metaclass=M2): pass # type: ignore[metaclass]
120-
121-
# option 3: compute the metaclass dynamically
122-
class M3(type(tuple)): pass # type: ignore[metaclass]
123-
class A3(tuple, metaclass=M3): pass

mypy/nodes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3403,6 +3403,7 @@ def calculate_metaclass_type(self) -> mypy.types.Instance | None:
34033403
return winner
34043404

34053405
def explain_metaclass_conflict(self) -> str | None:
3406+
# Compare to logic in calculate_metaclass_type
34063407
declared = self.declared_metaclass
34073408
if declared is not None and not declared.type.has_base("builtins.type"):
34083409
return None

0 commit comments

Comments
 (0)