Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

typing.overload

Declare alternative argument and result relationships for a static type checker.

Version: Python 3.14; repository image, production runtime not attested

Syntax

@overload
def parse(value: str) -> int: ...

Declare alternative argument and result relationships for a static type checker.

In an ordinary module, the decorated signatures must be followed by one implementation that handles actual calls. Python does not dispatch to a stub based on its annotations.

The example inspects both registered signatures and uses one implementation with an explicit branch.

Examples

from typing import get_overloads, overload

@overload
def flip(value: str) -> int: ...
@overload
def flip(value: int) -> str: ...
def flip(value):
    return len(value) if isinstance(value, str) else str(value)

print(len(get_overloads(flip)))
print(repr(flip("abc")), repr(flip(12)))

Result

2
3 '12'

The example inspects both registered signatures and uses one implementation with an explicit branch.

Sources