I’m not sure that it does make things easier. Currently mypy accepts this:
from typing import overload
class Int: ...
class Float: ...
@overload
def convert(x: int) -> Int: ...
@overload
def convert(x: float) -> Float: ...
# def convert(x: float | int) -> Float: ...
def convert(x: int | float) -> Int | Float:
if isinstance(x, int):
return Int()
elif isinstance(x, float):
return Float()
else:
raise TypeError
If you switch the commented overload line so that float
becomes float | int
then mypy will reject this:
$ mypy t.py
t.py:7: error: Overloaded function signatures 1 and 2 overlap with incompatible return types [overload-overlap]
Found 1 error in 1 file (checked 1 source file)
It is complaining because Float and Int are not related classes but float and int are also unrelated classes.