[Adjust Rule] SIM113 False Positive in Nested Loops
See original GitHub issueDesired change
SIM113 False Positive in nested loops
In this case, we could using enumerate is rather tricky. How do you suggest we best do it in nested loops like so?
- Rule(s): SIM113
- Adjustment: In which way should it/they be adjusted? The result should not be triggered in nested loops
Explanation
Why is the adjustment necessary / better?
Because the enumerate logic actually becomes MORE complicated in this nested loop context. You need to have some complex multiplication to get the proper number and that may not even be possible if bars is a generator that has variable length. In which case, counting manually here is actually for the best.
Example
This is an example where the mentioned rule(s) would currently be suboptimal:
count = 0
for foo in foos:
for bar in bars:
count +=1
In this second one, I could se the argument for epochs (assuming there is not complex increment logic, but it complains about number of iterations)
epoch, i = np.load(resume_file)
while epochs < TOTAL_EPOCHS:
for b in batches:
i+=1
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Commits · MartinThoma/flake8-simplify - GitHub
A flake8 plugin that helps you to simplify code. Contribute to MartinThoma/flake8-simplify development by creating an account on GitHub.
Read more >flake8-simplify - PyPI
SIM101 : Multiple isinstance-calls which can be merged into a single call by using a tuple as a ... This rule was removed...
Read more >Chapter 5: Nested loops, Which loop to use?
We expect the iteration of a loop to be controlled by the true/false statements in the while and do-while and the for loop...
Read more >Allow goto in switch statements nested in while loops without ...
Template for a good false-positive report, formatted with Markdown: versions used (SonarQube, Scanner, language analyzer) I use ...
Read more >Python Loops - For, While, Nested Loops With Examples
It only breaks out of the loop or stops executing the code block if the condition is FALSE, and in this case, the...
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 Free
Top 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

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.
@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.