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.

[Adjust Rule] SIM113 False Positive in Nested Loops

See original GitHub issue

Desired 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
GitRoncommented, Jan 7, 2021

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.

1reaction
MartinThomacommented, Jan 7, 2021

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

Read more comments on GitHub >

github_iconTop 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 >

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