Types

Overloads

overload describes APIs whose return type depends on argument types.

The overload stubs give static tools precise call shapes: integer in, integer out; string in, string out.

Source

from typing import overload

@overload
def double(value: int) -> int: ...

@overload
def double(value: str) -> str: ...

print("static signatures only")

Output

static signatures only
STATIC SIGNATURESdouble(int) -> intdouble(str) -> strRUNTIMEone double() body
@overload declares static call signatures for double(int) and double(str); one runtime implementation handles both.

There is still one runtime implementation. It must accept every shape promised by the overloads.

Source

def double(value: int | str) -> int | str:
    return value * 2

print(double(4))
print(double("ha"))

Output

8
haha

Only the implementation's annotations are visible on the runtime function. The overload declarations were for the type checker.

Source

print(double.__annotations__)

Output

{'value': int | str, 'return': int | str}

Notes

See also

Run the complete example

Example code

Expected output

8
haha
{'value': int | str, 'return': int | str}

Execution time appears here after you run the example.