question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Forbid to use `&` and `|` with booleans

See original GitHub issue

Rule request

Thesis

Yesterday I saw this code:

my_result = (first > 0 & second != 0) | other is None

And I was 😮

So, we need to check all operations around & and | and detect any booleans there:

  • Explicit True and False
  • Compares with is or == or !=
  • Compares with >, >=, etc
  • Logical operations not, or, and

If we can detect at least one of these operations, then we should raise a violation. Let’s call it BinaryAndBooleanMixupViolation.

Note: other binary operations are still fine! So, these lines should not raise:

  • first & second
  • first | 10
  • etc

Reasoning

This case indicates that a person confused & with and and | with or. This can be the case if a person is comming from another language.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
thammajicommented, Nov 11, 2020

Hi, I would like to try working on this issue. This would be my first issue, so I’m not sure of a solution yet as I’m still going through the contribution tutorials. I’ll update once I have an idea for a solution, but any suggestion is welcome too!

Also would calling & and | as bitwise operator be more appropriate? so the error would be BitwiseAndBooleanMixupViolation

0reactions
thammajicommented, Dec 10, 2020

Oh I see. I will move my visitor to operators.py then (it fits there better anyway).

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - How do "and" and "or" act with non-boolean values?
If all values are true in a boolean context, and returns the last value. If any value is false in a boolean context...
Read more >
strict-boolean-expressions | typescript-eslint
This rule requires type information to run. Forbids usage of non-boolean types in expressions where a boolean is expected. boolean and never types...
Read more >
4.6. Booleans - Red Hat Enterprise Linux
For a list of Booleans, an explanation of what each one is, and whether they are on or off, run the semanage boolean...
Read more >
Understanding Booleans in Java: A Quick Guide - HubSpot Blog
Learn how to make use of booleans in Java programming with this quick, beginner's guide.
Read more >
Python Booleans: Optimize Your Code With Truth Values
You'll see how to use Booleans to compare values, check for identity and ... Short-circuit evaluation of comparison chains can prevent other exceptions:....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found