Sep-16-2018, 09:38 PM
I'm attempting to type hint on a temperature conversion function.
The function has a deliberate mistake in that it returns a Number instead of a dictionary, but the
return statement is for a Dictionary.
Thanks
The function has a deliberate mistake in that it returns a Number instead of a dictionary, but the
return statement is for a Dictionary.
#!/home/bluefrog/anaconda3/bin/python3 from decimal import Decimal from typing import Optional, Union, Dict Number = Union[int, float, Decimal] def convert_temperature(*, f_temp: Optional[Number]=None, c_temp: Optional[Number]=None) -> Number: if c_temp is None: c_temp = 5*(f_temp-32)/9 elif f_temp is None: f_temp = 32+9*c_temp/5 else: raise Exception( "Logic Design Problem" ) result: Dict[str, Number] = {'c_temp': c_temp, 'f_temp': f_temp} return resultWhen I run mypy I get the expected message on the incorrect return, but I also get 2 other messages that I do not understand:
$ mypy celcius_to_fahrenheit.py celcius_to_fahrenheit.py:8: error: Unsupported operand types for - ("None" and "int") celcius_to_fahrenheit.py:8: note: Left operand is of type "Union[int, float, Decimal, None]" celcius_to_fahrenheit.py:15: error: Incompatible return value type (got "Dict[str, Union[int, float, Decimal]]", expected "Union[int, float, Decimal]")Could somebody explain why the 2 messages for line 8 appear. I was only expecting a message for line 15, which does appear. I am using 3.7 along with mypy
Thanks