Text of peps.python.org › pep-0800 as Styx crawled it on Nov 18, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

PEP 800 – Disjoint bases in the type system | peps.python.org

PEP 800 – Disjoint bases in the type system PEP 800 – Disjoint bases in the type system Author : Jelle Zijlstra <jelle.zijlstra at gmail.com> Discussions-To : Discourse thread Status : Final Type : Standards Track Topic : Typing Created : 21-Jul-2025 Python-Version : 3.15 Post-History : 18-Jul-2025 , 23-Jul-2025 Resolution : 15-Apr-2026 Table of Contents Abstract Motivation Invalid class definitions Reachability Overloads Intersection types Overview Rationale Naming Runtime restrictions on multiple inheritance @disjoint_base in implementation files @disjoint_base on special classes Specification Runtime implementation Backward compatibility Security Implications How to Teach This Reference Implementation Appendix Solid bases in CPython Mypy’s incompatibility check Copyright Important This PEP is a historical document: see @disjoint_base and @typing.disjoint_base for up-to-date specs and documentation.

Canonical typing specs are maintained at the typing specs site ; runtime typing behaviour is described in the CPython documentation. × See the typing specification update process for how to propose changes to the typing spec. Abstract To analyze Python programs precisely, type checkers need to know when two classes can and cannot have a common child class. However, the information necessary to determine this is not currently part of the type system.

This PEP adds a new decorator, @typing.disjoint_base , that indicates that a class is a “disjoint base”. Two classes that have distinct, unrelated disjoint bases cannot have a common child class. This decorator is not expected to be used directly by most users. It is primarily intended for use in stub files for standard library and extension-module classes, where it helps type checkers reflect runtime restrictions consistently.

Motivation In type checking Python, an important concept is that of reachability. Python type checkers generally detect when a branch of code can never be reached, and they warn users about such code. This is useful because unreachable code unnecessarily complicates the program, and its presence can be an indication of a bug. For example, in this program: def f ( x : bool ) -> None : if isinstance ( x , str ): print ( "It's both!" ) both pyright and mypy (with --warn-unreachable ), two popular type checkers, will warn that the body of the if block is unreachable, because if x is a bool , it cannot also be a str .

Reachability is complicated in Python by the presence of multiple inheritance. If instead of bool and str , we use two user-defined classes, mypy and pyright do not show any warnings: class A : pass class B : pass def f ( x : A ): if isinstance ( x , B ): print ( "It's both!" ) This is correct, because a class that inherits from both A and B could exist. We see a divergence between type checkers in another case, where we use int and str : def f ( x : int ): if isinstance ( x , str ): print ( "It's both!" ) For this code, pyright shows no errors but mypy will claim that the branch is unreachable.

Mypy is technically correct here: CPython does not allow a class to inherit from both int and str , so the branch is unreachable. However, the information necessary to determine that these base classes are incompatible is not currently available in the type system. Mypy, in fact, uses a heuristic based on the presence of incompatible methods; this heuristic works reasonably well in practice, especially for built-in types, but it is incorrect in general, as discussed in more detail below .

The experimental ty type checker uses a third approach that aligns more closely with the runtime behavior of Python : it recognizes certain classes as “solid bases” that restrict multiple inheritance. Broadly speaking, every class must 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 precise definition below. However, ty’s current approach relies on hardcoded knowledge of particular built-in types.

The term “solid base” derives from the CPython implementation; this PEP uses the newly proposed term “disjoint base” instead. This PEP proposes an extension to the type system that makes it possible to express when multiple inheritance is not allowed at runtime: an @disjoint_base decorator that marks a class as a disjoint base . This gives type checkers a more precise understanding of reachability, and helps in several concrete areas.

Invalid class definitions The following class definition raises an error at runtime, because int and str are distinct disjoint bases: class C ( int , str ): pass Without knowledge of disjoint bases, type checkers are not currently able to detect the reason why this class definition is invalid, though they may detect that if this class were to exist, some of its methods would be incompatible. (When it sees this class definition, mypy will point at incompatible definitions of __add__ and several other methods.) This is not a particularly compelling problem by itself, as the error would usually be caught the first time the code is imported, but it is mentioned here for completeness.

Reachability We already mentioned the reachability of code using isinstance() . Similar issues arise with other type narrowing constructs such as match statements: correct inference of reachability requires an understanding of disjoint bases. class A : pass class B : pass def f ( x : A ): match x : case B (): # reachable print ( "It's both!" ) def g ( x : int ): match x : case str (): # unreachable print ( "It's both!" ) Overloads Functions decorated with @overload may be unsafe if the parameter types of some overloads overlap, but the return types do not.

For example, the following set of overloads could be exploited to achieve unsound behavior : from typing import overload class A : pass class B : pass @overload def f ( x : A ) -> str : ... @overload def f ( x : B ) -> int : ... If a class exists that inherits from both A and B , then type checkers could pick the wrong overload on a call to f() . Type checkers could detect this source of unsafety and warn about it, but a correct implementation requires an understanding of disjoint bases, because it relies on knowing whether values that are instances of both A and B can exist.

Although many type checkers already perform a version of this check for overlapping overloads, the typing specification does not currently prescribe how this check should work. This PEP does not propose to change that, but it helps provide a building block for a sound check for overlapping overloads. Intersection types Explicit intersection types, denoting a type that contains values that are instances of all of the given types, are not currently part of the type system.

They do, however, arise naturally in a set-theoretic type system like Python’s as a result of type narrowing, and future extensions to the type system may add support for explicit intersection types. With intersection types, it is often important to know whether a particular intersection is inhabited, that is, whether there are values that can be members of that intersection. This allows type checkers to understand reachability and provide more precise type information to users.

As a concrete example, a possible implementation of assignability with intersection types could be that given an intersection type A & B , a type C is assignable to it if C is assignable to at least one of A and B , and overlaps with all of A and B . (“Overlaps” here means that at least one runtime value could exist that would be a member of both types. That is, A and B overlap if A & B is inhabited.) The second part of the rule ensures that str is not assignable to a type like int & Any : while str is assignable to Any , it does not overlap with int .

But of course, we can only know that str and int do not overlap if we know that both classes are disjoint bases. Overview Disjoint bases can be helpful in many corners of the

The rest of this page is on the live site.