Comments (7)
Ok, true, this might be a nicer way for the simple case 😃
You might want to add this example to the main Readme so people know in one second how to fix this linting issue.
from flake8-simplify.
@GitRon Done :-) I've also linked the issues in which the rules were introduced and renamed the titles of the issues so that they are easier to find. Additionally, I've added a short description of the rule + a link to the example later in the document. I might move the complete set of rules and examples to another page in the future as I'm not a big fan of huge READMEs.
from flake8-simplify.
Good point! Thank you for raising it.
At the moment, I'm not sure how to fix it. Or if there is any way to fix it. I'm thinking about removing SIM113 completely again.
For now, you can set --ignore=SIM113
or within the setup.cfg
[flake8]
ignore=SIM113
from flake8-simplify.
Just gave your package a shot and its really nice - but I didn't get the SIM113 (which is not documented on the main page btw). Whats the reason not to use a regular var as a loop counter?
Thanks!
from flake8-simplify.
@GitRon Thank you :-)
SIM113 should bring awareness to people who are not aware of enumerate
. The advantage of using enumerate (when possible) is that you don't have to increment that variable in the loop body. It is a tiny bit easier to read as the reader does not have to figure out where / when the loop variable is incremented (or if it is incremented in multiple places).
Of course, Skylion007 gave a good example with
count = 0
foos = [1, 2, 3, 4]
bars = ["A", "B", "C"]
for foo in foos:
for bar in bars:
count +=1
where this rule should not be triggered.
By the way, it is possible to use enumerate here as well:
count = 0
foos = [1, 2, 3, 4]
bars = ["A", "B", "C"]
from itertools import product
for count, (foo, bar) in enumerate(product(foos, bars)):
print((count, foo, bar))
But that is harder to read, especially when more happens in the function body.
I'm currently wondering if checking for nested loops via the introduced parent
attribute solves the issue.
from flake8-simplify.
Thanks for the explanation! Actually I didn't know about this as well. Looks fancy but I might argue for the simple solution to be valid as well. Linting is quite strict so IMHO I think it should only point out things, you should never do. And if you want to keep a complicated case simple, it might be valid to to for approach one, won't it?
from flake8-simplify.
It depends on the exact case. For the example given in this issue by Skylion007 , I agree that NOT using enumerate is the simpler solution.
In the following case, I want to encourage people to use enumerate
:
# Bad, SIM113 should be triggered:
i = 0
for element in some_iterable:
... # do something with i
i += 1
# Good
for i, element in enumerate(some_iterable):
... # do something with i
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()" HOT 1
- [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.