The below code fails mypy
with error: Overloaded function signatures 1 and 2 overlap with incompatible return types
.
@overload
def test_overload(x: str) -> str: ...
@overload
def test_overload(x: object) -> int: ...
def test_overload(x) -> Union[str, int]:
if isinstance(x, str):
return x
else:
return 1
What I'm trying to express is: "This function takes an arbitrary Python object. If that object is a string, it returns a string. If it is any other type, it returns an integer. Note this particular example is contrived to represent the general case.
Is it possible to express this with overloads?