mypy gives this error:
error: Overloaded function signature 2 will never be matched: signature 1's parameter type(s) are the same or broader
with this code:
from typing import overload, Literal
from random import randint
@overload
def func() -> dict[str, float]:
...
@overload
def func() -> Literal['']: # this is the error line
...
def func() -> dict[str, float] | Literal['']:
x = randint(1, 10) % 2
if x:
return ''
else:
return {'foo':12.34}
print(func())
I tried switching the order of the two @overload defs, but that didn't help. I've seen this error message when trying to overload int and float or int and bool, which I can understand given the similarities, but I don't understand the problem here. I also tried str instead of Literal[''] to no avail; similar problems with returning 0 or False. I'm using Python 3.10.4 and mypy 0.960.
I'm returning '' in the real code to signal an error.
How can I fix this?