Skip to content

Make it possible for type checking to detect "The truth value of a DataFrame is ambiguous." #663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
karolzlot opened this issue Apr 27, 2023 · 5 comments · Fixed by #1087
Labels
Numeric Operations Arithmetic, Comparison, and Logical operations

Comments

@karolzlot
Copy link

I described the problem here:
microsoft/pyright#5024
and also here:
microsoft/pylance-release#4289

@karolzlot karolzlot changed the title Improve pandas typestubs so type checking can detect "The truth value of a DataFrame is ambiguous. Make it possible for type checking to detect "The truth value of a DataFrame is ambiguous. Apr 27, 2023
@gandhis1
Copy link
Contributor

gandhis1 commented Apr 27, 2023

Maybe annotate __bool__ on pd.DataFrame and pd.Series to return typing.Never ?

@karolzlot karolzlot changed the title Make it possible for type checking to detect "The truth value of a DataFrame is ambiguous. Make it possible for type checking to detect "The truth value of a DataFrame is ambiguous." Apr 27, 2023
@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Apr 28, 2023

I think the solution here is that DataFrame.__eq__() needs to have a stub in core/frame.pyi returning a DataFrame

@Dr-Irv Dr-Irv added the Numeric Operations Arithmetic, Comparison, and Logical operations label Apr 28, 2023
@gandhis1
Copy link
Contributor

I think the solution here is that DataFrame.__eq__() needs to have a stub in core/frame.pyi returning a DataFrame

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__

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented Apr 30, 2023

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?

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 microsoft/pyright#5039

@Dr-Irv
Copy link
Collaborator

Dr-Irv commented May 4, 2023

pyright fixed the issue, in 1.1.306 so we should define __bool__() for Series and DataFrame as NoReturn, and add some tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Numeric Operations Arithmetic, Comparison, and Logical operations
Projects
None yet
3 participants