Comments (6)
b if a else a
is not equivalent to b or a
.
from flake8-simplify.
This would be pretty easy to check and implement:
IfExp(
test=Name(id='a', ctx=Load()),
body=Name(id='a', ctx=Load()),
orelse=Name(id='b', ctx=Load()),
),
However, I'm uncertain of I would prefer the left or the right expression. Give me a couple of days to think about it. The right expression is shorter, but the left expression seems clearer to me.
from flake8-simplify.
The second one I would rather convert to
b if a else a -> b or a
. It might need less evaluation.
from flake8-simplify.
I've just created the following expression tables with
abs = [("a", ""), ("", "b"), ("a", "b"), ("", 0)]
for a, b in abs:
print(b if a else a)
Table for a if a else b
and a or b
:
a | b | Result |
---|---|---|
"a" | "" | "a" |
"" | "b" | "b" |
"a" | "b" | "a" |
"" | 0 | 0 |
Table for b if a else a
and a and b
:
a | b | Result |
---|---|---|
"a" | "" | "" |
"" | "b" | "" |
"a" | "b" | "b" |
"" | 0 | "" |
Table for b or a
:
a | b | Result |
---|---|---|
"a" | "" | "a" |
"" | "b" | "b" |
"a" | "b" | "b" |
"" | 0 | "" |
Table for b and a
:
a | b | Result |
---|---|---|
"a" | "" | "" |
"" | "b" | "" |
"a" | "b" | "a" |
"" | 0 | 0 |
from flake8-simplify.
@SpecLad You're completely right. They don't even have a single element in common 😅
This example helped me to make my mind up about this rule. Although it easy to reason which input gives which output, I do need to think about it. I find the boolean expressions a or b
/ a and b
/ b or a
/ b and a
harder to reason about than their ternary expression equivalents.
from flake8-simplify.
As the ternary expressions (a if a else b) are easier for me to understand than the logical expressions (a or b), I will not implement this rule.
from flake8-simplify.
Related Issues (20)
- SIM401 should not apply to non-constant default values
- [Adjust Rule] SIM901 case where bool could be useful
- [New Rule] Merge duplicate startswith / endswith calls HOT 1
- [Adjust Rule] Only emit SIM208 in boolean contexts
- [New Rule]: rewrite dict.get(key, None) as dict.get(key) HOT 2
- [New Rule] Suggest `exist_ok` for `Path.mkdir` and `os.makedirs`
- [Adjust Rule] loosen SIM115 when just opening a file without reference
- SIM118 false positive when using awsglue.dynamicframe.DynamicFrameCollection HOT 1
- [Adjust Rule] missing case with SIM108
- [Adjust Rule] SIM401 False positive, rule too greedy
- Add new release 0.20 with changes at GitHub
- [New Rule] Replace `hasattr` if else with `getattr` when appropiate HOT 1
- [New Rule] AnySequence(dict.keys()) to AnySequence(dict)
- [New Rule] Use set_default in Python dictionaries
- [Adjust Rule] SIM118 - possible issue with objects having a method "keys()"
- [New Rule] Preferring in-place operators
- Usage of deprecated ast nodes
- [Error] Not working with flake8==7.0.0 and Python 3.11.7
- [New Rule] Duplicate blocks in if/else
- [Adjust Rule] SIM 108 and SIM 118 don't cover enough cases as Ruff
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flake8-simplify.