I've been running into a bit of weirdness with Unions (and Optionals, of course) in Python - namely it seems that the static type checker tests properties against all member of a union, and not a member of the union (i.e. it seems overly strict?). As an example, consider the following:
import pandas as pd
def test_dummy() -> pd.DataFrame:
df = pd.DataFrame()
df = df.fillna(df)
return df
This creates a type warning, as pd.fillna(..., inplace: Bool = False, ...) -> Optional[pd.DataFrame] (it is a None return if inplace=True). I suspect that in theory the static type checker should realize the return of the function changes depending on the arguments (as that should be known when code is written), but that's a bit beyond the point.
I have the following questions:
What is the best way to resolve this? I can think of two solutions:
i) do nothing -- which creates ugly squiggles in my code
ii)
castthe return offillnato apd.DataFrame; my understanding is this is a informative step to the static type checker so should not cause any concerns or issues?Let us consider that I'm writing a function
fwhich, similarly to this, has its return types vary depending on the function call inputs, and this should be determinable before runtime. In order to avoid such errors in the future; what is the best way to go about writing this function? Would it be better to do something like a@typing.overload?