Skip to content

typing.overload used

Detects use of overloaded functions.

Overloaded functions can often lead to runtime errors if the implementations are not consistent with the overload definitions.

We only emit a warning here as just using typing.overload will not necessarily lead to a runtime type error.

What gets flagged

Here is an example of an overloaded function that is not implemented correctly, but type checkers will not emit diagnostics for this:

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from typing import overload

@overload
def foo(x: int) -> str: ...

@overload
def foo(x: str) -> int: ...

def foo(x: int | str) -> str | int:
    return x
Unsoundness Checker Output
Text Only
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
warning[typing-overload-used]: Using `typing.overload` can lead to runtime errors.
 --> main.py:3:1
  |
1 | from typing import overload
2 |
3 | @overload
  | ^^^^^^^^^
4 | def foo(x: int) -> str: ...
  |
info: rule `typing-overload-used` was selected in the configuration file

warning[typing-overload-used]: Using `typing.overload` can lead to runtime errors.
 --> main.py:6:1
  |
4 | def foo(x: int) -> str: ...
5 |
6 | @overload
  | ^^^^^^^^^
7 | def foo(x: str) -> int: ...
  |
info: rule `typing-overload-used` was selected in the configuration file

Mypy: No Diagnostic Emitted

Pyright: No Diagnostic Emitted

Ty: No Diagnostic Emitted