Python Glossary Русский

Python 3.15+typingterm

@typing.disjoint_base

decorators and markers

A decorator for type checkers: the class is a disjoint base, and inheriting from it together with another such base is not allowed.

Syntax

@typing.disjoint_base
class C: ...

At runtime the decorator forbids nothing; it only marks the class. The point is static checking: if int and str are disjoint bases, the checker knows a value cannot be an instance of both and can flag an unreachable branch such as isinstance(x, int) and isinstance(x, str). In CPython, built-in classes with their own memory layout have this property: class C(int, str) fails with TypeError: multiple bases have instance lay-out conflict, and stub files (.pyi) mark such classes with this decorator.

Examples

from typing import disjoint_base
@disjoint_base
class Shape:
    pass
print(Shape.__name__)  # → Shape — во время выполнения класс обычный
try:
    class Both(int, str):
        pass
except TypeError as e:
    print(e)  # → multiple bases have instance lay-out conflict

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy