You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -19,6 +20,10 @@ However, the information necessary to determine this is not currently part of th
19
20
decorator, ``@typing.disjoint_base``, that indicates that a class is a "disjoint base". Two classes that have distinct, unrelated
20
21
disjoint bases cannot have a common child class.
21
22
23
+
This decorator is not expected to be used directly by most users. It is primarily intended for use in stub files for
24
+
standard library and extension-module classes, where it helps type checkers reflect runtime restrictions
25
+
consistently.
26
+
22
27
Motivation
23
28
==========
24
29
@@ -63,11 +68,11 @@ incorrect in general, as discussed in more detail :ref:`below <pep-800-mypy-inco
63
68
The experimental ``ty`` type checker uses a third approach that aligns more closely with the :ref:`runtime behavior of Python <pep-800-solid-bases-cpython>`:
64
69
it recognizes certain classes as "solid bases" that restrict multiple inheritance. Broadly speaking, every class must
65
70
inherit from at most one unique solid base, and if there is no unique solid base, the class cannot exist; we'll provide a more
66
-
precise definition below. However, ty's approach relies on hardcoded knowledge of particular built-in types. The term "solid base" derives from the
71
+
precise definition below. However, ty's current approach relies on hardcoded knowledge of particular built-in types. The term "solid base" derives from the
67
72
CPython implementation; this PEP uses the newly proposed term "disjoint base" instead.
68
73
69
74
This PEP proposes an extension to the type system that makes it possible to express when multiple inheritance is not
70
-
allowed at runtime: an ``@disjoint_base`` decorator that marks a classes as a *disjoint base*.
75
+
allowed at runtime: an ``@disjoint_base`` decorator that marks a class as a *disjoint base*.
71
76
This gives type checkers a more precise understanding of reachability, and helps in several concrete areas.
72
77
73
78
Invalid class definitions
@@ -256,7 +261,7 @@ Similarly, the concept of a "disjoint base" is not meaningful on ``TypedDict`` d
256
261
Although they receive some special treatment in the type system, ``NamedTuple`` definitions create real nominal classes that can
257
262
have child classes, so it makes sense to allow ``@disjoint_base`` on them and treat them like regular classes for the purposes
258
263
of the disjoint base mechanism. All ``NamedTuple`` classes have ``tuple``, a disjoint base, in their MRO, so they
259
-
cannot multiple inherit from other disjoint bases.
264
+
cannot use multiple inheritance with other disjoint bases.
260
265
261
266
Specification
262
267
=============
@@ -363,8 +368,9 @@ None known.
363
368
How to Teach This
364
369
=================
365
370
366
-
Most users will not have to directly use or understand the ``@disjoint_base`` decorator, as the expectation is that will be
367
-
primarily used in library stubs for low-level libraries. Teachers of Python can introduce
371
+
Most users will not have to directly use or understand the ``@disjoint_base`` decorator, as the expectation is that it will be
372
+
primarily used in library stubs for low-level libraries. It should not be taught as a decorator that users should routinely
373
+
add to classes. Teachers of Python can introduce
368
374
the concept of "disjoint bases" to explain why multiple inheritance is not allowed in certain cases. Teachers of
369
375
Python typing can introduce the decorator when teaching type narrowing constructs like ``isinstance()`` to
370
376
explain to users why type checkers treat certain branches as unreachable.
@@ -377,7 +383,8 @@ The runtime implementation of the ``@disjoint_base`` decorator is available in
377
383
Mypy and its stubtest tool support the decorator as of version 1.18.1; this was implemented in
0 commit comments