Python Glossary Русский

Python 3.5+typingfunction

typing.overload

decorators and markers

A decorator declaring multiple signatures (overloads) of one function for the type checker.

Syntax

@typing.overload

Overloads are pure annotations for the type checker: at runtime they have no body, and calling such a stub raises NotImplementedError. That's why all the @overload stubs must be followed by one plain, undecorated implementation that inspects the arguments itself — Python does no type-based dispatch the way C++ or Java do.

Examples

# Python 3.11+
from typing import overload, get_overloads
print(callable(overload))   # → True
# Python 3.11+
from typing import overload, get_overloads
@overload
def double(x: int) -> int: ...
@overload
def double(x: str) -> str: ...
def double(x): return x * 2   # реализация идёт последней, без @overload
print(double(5))   # → 10
print(double('ab'))   # → abab
print(len(get_overloads(double)))   # → 2
# Python 3.11+
from typing import overload, get_overloads
@overload
def solo(x: int) -> int: ...
print(solo(1))   # → NotImplementedError (перегрузка без реализации)

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy