Skip to content
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

E712 (Comparison to True should be cond is True) should be ignored when comparison is against pandas.Series #1852

Closed
rsokolewicz opened this issue Jan 13, 2023 · 9 comments
Labels
bug Something isn't working

Comments

@rsokolewicz
Copy link

using ruff==0.0.215

The following snippet raises an E712 when running ruff:

import pandas as pd

df = pd.DataFrame({"x" : [False, True]})
df["x"] != False

and the autofix replaces df["x"] != False with df["x"] is not False. The two are unfortunately not equivalent and the latter leads to errors from pandas.

@charliermarsh charliermarsh added the bug Something isn't working label Jan 14, 2023
@charliermarsh
Copy link
Member

Pretty hard to avoid until we can infer that df is a DataFrame here.

@akanz1
Copy link

akanz1 commented Jan 14, 2023

Can we prevent the autofix from applying this specific rule?

@nils-borrmann-y42
Copy link

nils-borrmann-y42 commented Jan 14, 2023

The same problem happens in sqlalchemy queries (.where(foo != bar) is not equivalent to .where(foo is not bar))

@charliermarsh
Copy link
Member

Before I disable autofix for this rule entirely... you can disable it for your project via:

[tool.ruff]
unfixable = ["E712"]

Is that sufficient here?

@akanz1
Copy link

akanz1 commented Jan 14, 2023

Works for me, thanks! :)

@rsokolewicz
Copy link
Author

In my data analysis and data science projects I end up ignoring this rule more often than having it autofixed, so I ended up using

[tool.ruff]
ignore = ["E712"]

instead. There are a few other cases where autofixing == True to is True will break code, namely when comparing against booleans that have a package-specific implementation such as numpy (numpy.bool_) or pytorch (toch.bool).

@charliermarsh
Copy link
Member

Yeah, that makes sense. I think I'm going to close this for now given that we have some workarounds (users should either disable the rule entirely, or, if they want the rule to be enabled but want to avoid false fixes, should use unfixable).

@Faolain
Copy link

Faolain commented Jul 17, 2023

This just broke our code using SQLAlchemy converting

signup_code = (
    db.session.query(SignupCode)
    .filter(SignupCode.code == code, SignupCode.used == False)
    .first()
)

to

 signup_code = (
        db.session.query(SignupCode)
        .filter(SignupCode.code == code, SignupCode.used is False)
        .first()
    )

which it definitely shouldn't have autofixed. Can this instead be a suggestion provided but something that is not autofixed? Can prevent a lot of headaches.

@charliermarsh
Copy link
Member

This is marked in the codebase as a "Suggested" autofix, which in a future release will require running with additional command-line flags to explicitly opt-in to fixing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants