Forbid to use `&` and `|` with booleans
See original GitHub issueRule 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
andFalse
- 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:
- Created 3 years ago
- Comments:9 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
Oh I see. I will move my visitor to operators.py then (it fits there better anyway).