Typing and Generics

typing.overload

Registers alternate static call signatures for one implementation. Define two or more overload declarations followed by the runtime implementation. Dispatch is the implementation's responsibility; an overload stub is not a callable implementation.

Formal Syntax & Structure

python syntaxpython
@typing.overload

Parameters & Attributes

NameTypeDescription
targetcallableObject to decorate.

Return Value

function declaration

Typing construct or runtime result as described; static typing rules are not runtime validation.

Working Example

Example Snippetpython
from typing import clear_overloads, get_overloads, overload
clear_overloads()@overloaddef double(x:int)->int: ...@overloaddef double(x:str)->str: ...def double(x:int|str)->int|str: return x+xprint((double(3),double('x'),len(get_overloads(double))))
Python Reference