pandas-stubs
pandas-stubs copied to clipboard
Make it possible for type checking to detect "The truth value of a DataFrame is ambiguous."
I described the problem here: https://github.com/microsoft/pyright/issues/5024 and also here: https://github.com/microsoft/pylance-release/issues/4289
Maybe annotate __bool__ on pd.DataFrame and pd.Series to return typing.Never ?
I think the solution here is that DataFrame.__eq__() needs to have a stub in core/frame.pyi returning a DataFrame
I think the solution here is that
DataFrame.__eq__()needs to have a stub incore/frame.pyireturning aDataFrame
I'm not sure I follow what that will accomplish. This error is triggered when someone does the equivalent of casting a DataFrame to a bool - which calls __bool__, no?
Even pandas itself has it annotated the way I suggested:
@final
def __nonzero__(self) -> NoReturn:
raise ValueError(
f"The truth value of a {type(self).__name__} is ambiguous. "
"Use a.empty, a.bool(), a.item(), a.any() or a.all()."
)
__bool__ = __nonzero__
I'm not sure I follow what that will accomplish. This error is triggered when someone does the equivalent of casting a
DataFrameto abool- which calls__bool__, no?
You're right, and actually DataFrame.__eq__() is return a DataFrame already.
So I tried with a simple class, and it didn't work with pyright. See https://github.com/microsoft/pyright/issues/5039
pyright fixed the issue, in 1.1.306 so we should define __bool__() for Series and DataFrame as NoReturn, and add some tests.