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

Suggested lint rule: disallow implicit-cast-to-bool #10949

Open
patrick-kidger opened this issue Apr 15, 2024 · 2 comments
Open

Suggested lint rule: disallow implicit-cast-to-bool #10949

patrick-kidger opened this issue Apr 15, 2024 · 2 comments
Labels
rule Implementing or modifying a lint rule

Comments

@patrick-kidger
Copy link

patrick-kidger commented Apr 15, 2024

I'm thinking of writing a new lint rule for Ruff, to disallow implicit-cast-to-bool.

That is, to disallow statements like foo = None; if foo:. Our team finds that these are a common source of bugs. For example if foo: Optional[int] then was the intention for if foo: to not trigger on just foo = None, or did the author want it to not trigger on foo = 0 as well?

As per the contributing guidelines I'm opening an issue to see if this is a rule that is in-scope for Ruff?
In terms of technical feasibility of implementing this -- to what extent do the ruff internals track the types of each variable?

@MichaReiser MichaReiser added the rule Implementing or modifying a lint rule label Apr 15, 2024
@MichaReiser
Copy link
Member

Hy. I haven't checked if Ruff already has a similar rule and I'm not familiar enough with Python to know if that's a rule that Ruff should support (@AlexWaygood knows more) but I think this isn't a rule that we can add until #1774 is done because it's not a rule that we want to enable by default and many users are using --select ALL

@kkom
Copy link

kkom commented Apr 16, 2024

Big ➕1️⃣ to a rule like this! FWIW, I created a discussion about it here: #8757 (comment)

Alternative option: Typechecker

As a sidenote, if you want to, you could also support this discussion where I pitched this rule in Pyright: microsoft/pyright#6488 (comment)

I think implementing this in the typechecker would be much more reliable.

Pyright is known to implement opinionated rules like this (e.g. unnecessary comparison), but the maintainer is currently not convinced about this one:

I'm not convinced that it represents a sufficiently common source of bugs to merit adding such a rule, but I could perhaps be persuaded with some additional evidence.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
rule Implementing or modifying a lint rule
Projects
None yet
Development

No branches or pull requests

3 participants