I have the following function:
import pandas as pd
def eq(left: pd.Timestamp, right: pd.Timestamp) -> bool:
return left == right
I get the following error when I run it through Mypy:
error: Returning Any from function declared to return "bool"
I believe this is because Mypy doesn't know about pd.Timestamp so treats it as Any. (Using the Mypy reveal_type function shows that Mypy treats left and right as Any.)
What is the correct way to deal with this to stop Mypy complaining?
you can cast it as a bool.
import pandas as pd
def eq(left: pd.Timestamp, right: pd.Timestamp) -> bool:
return bool(left == right)
if mypy doesn't like that you can import cast from typing and use that to cast it to a bool.
import pandas as pd
from typing import cast
def eq(left: pd.Timestamp, right: pd.Timestamp) -> bool:
result = bool(left == right)
return cast(bool, result)
I have tested it also without using the cast command. The Mypy check is passed without any error with the following code:
import pandas as pd
def eq(left: pd.Timestamp, right: pd.Timestamp) -> bool:
return bool(left == right)
(Tested with Mypy 0910.)