I have a function that returns a number but it's not obvious what the user enters. It could accept a str, int or float. So I've type-hinted it as follows:
from numbers import Number
def to_number(value: Union[str, Number]) -> Number:
if isinstance(value, Number) and math.isnan(value):
raise ValueError(f"No NaNsense")
try:
return int(value)
except ValueError:
try:
return float(value)
except ValueError as exc:
raise ValueError(f"'{value}' is not a number") from exc
Upon running pytype on the file I get the following error:
File "whatever.py", line 8, in evaluate: bad return type [bad-return-type]
Expected: numbers.Number
Actually returned: int
File "whatever.py", line 11, in evaluate: bad return type [bad-return-type]
Expected: numbers.Number
Actually returned: float
But in the REPL
>>> isinstance(int(5), numbers.Number)
True
>>> isinstance(float(5), numbers.Number)
True
What's the resolution here? I'm on CPython 3.7.13, PyType 2022.12.15