typing.overload Types Multiple Signatures
Declare typing.overload signatures for int and str inputs, keep one runtime implementation, and inspect the registered overloads.
Code
from typing import get_overloads, overload
@overload
def parse(value: int) -> int: ...
@overload
def parse(value: str) -> list[str]: ...
def parse(value: int | str) -> int | list[str]:
if isinstance(value, int):
return value * 2
return [part.strip() for part in value.split(',')]
def main() -> None:
print(parse(21))
print(parse('alpha, beta, gamma'))
print('registered overloads:', len(get_overloads(parse)))
print('runtime callable:', type(parse).__name__)
if __name__ == '__main__':
main()Output
Captured from a sandboxed run of this snippet in the build pipeline.
42
['alpha', 'beta', 'gamma']
registered overloads: 2
runtime callable: functionHow it works
The working surface is the functions parse and main — everything else is wiring.
The return value is newly constructed — inputs are never handed back.
Putting it to use
Entry signature: def parse(value: int) -> int
Running the file prints labeled results such as registered overloads:, runtime callable: — each operation’s effect is visible in the output.
The entry point wires parse together — run it once and the whole demo flows.
Core lines
def main() -> None:
print(parse(21))
print(parse('alpha, beta, gamma'))
print('registered overloads:', len(get_overloads(parse)))
print('runtime callable:', type(parse).__name__)
def parse(value: int | str) -> int | list[str]:
if isinstance(value, int):
return value * 2
return [part.strip() for part in value.split(',')]
Interactive Tools for This Topic
FAQ
- Does `parse` modify its input?
- The input survives unchanged; what you get back is a newly constructed value.
- What does typing.overload Types Multiple Signatures print when run?
- Execute the whole file; each printed line is prefixed with a label like `registered overloads:`, `runtime callable:`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.