-
Notifications
You must be signed in to change notification settings - Fork 38.8k
refactor: test: use throwaway _ variable for unused loop counters #19674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: test: use throwaway _ variable for unused loop counters #19674
Conversation
substitutes "for x in range(N):" by "for _ in range(N):" indicates to the reader that a block is just repeated N times, and that the loop counter is not used in the body
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
|
Concept ACK: the idiomatic/Pythonic While you're at it consider using Note that |
|
Concept ACK. I'm really hesitant to review this without us trying to prevent it in the future. Playing with vulture for a couple minutes seemed to give far too many false positives. Pylint, on the other hand, surely had many false negatives, but seemed to do a good job otherwise.
Possible extension that I don't want to forget: replace |
I think The reason we disabled (False negatives are not a big deal, but false positives are :)) |
Just manually checked on this PR. Surprisingly, yes! All instances can be replaced, by either |
|
-0 on this, I agree this looks somewhat neater, but also, I'm not sure it's worth making this change and being pedantic about it in the linter. |
|
Thanks for the conceptual reviews and linting tipps so far!
Yes, I was thinking the same. My hope is that with the pattern being more widespread in the code, awareness for this raises and hence it is on the long term easier catched both by PR creators and by reviewers. Not sure if that's enough -- adding it as rule to the linter seems to be too strong though, agree with laanwj here. For anyone wanting to try out the linters: note that (at least on my system) |
|
ACK dac7a11 -- the updated code is easier to reason about since the throwaway nature of a variable is expressed explicitly (using the Pythonic |
Yes, but also some people contributing to the test framework have been complaining about the Python linter framework that it does 'silly' checks that don't really help catch errors, but just feel overly pedantic. I think this is one such one. Edit: To be clear: |
darosior
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK dac7a11
|
manual inspection ACK dac7a11 though I think this kind of thing is a very low risk of testing bugs. |
…sed loop counters dac7a11 refactor: test: use _ variable for unused loop counters (Sebastian Falbesoner) Pull request description: This tiny PR substitutes Python loops in the form of `for x in range(N): ...` by `for _ in range(N): ...` where applicable. The idea is indicating to the reader that a block (or statement, in list comprehensions) is just repeated N times, and that the loop counter is not used in the body, hence using the throwaway variable. This is already done quite often in the current tests (see e.g. `$ git grep "for _ in range("`). Another alternative would be using `itertools.repeat` (according to Python core developer Raymond Hettinger it's [even faster](https://twitter.com/raymondh/status/1144527183341375488)), but that doesn't seem to be widespread in use and I'm not sure about a readability increase. The only drawback I see is that whenever one wants to debug loop iterations, one would need to introduce a loop variable again. Reviewing this is basically a no-brainer, since tests would fail immediately if a a substitution has taken place on a loop where the variable is used. Instances to replace were found by `$ git grep "for.*in range("` and manually checked. ACKs for top commit: darosior: ACK dac7a11 instagibbs: manual inspection ACK bitcoin@dac7a11 practicalswift: ACK dac7a11 -- the updated code is easier to reason about since the throwaway nature of a variable is expressed explicitly (using the Pythonic `_` idiom) instead of implicitly. Explicit is better than implicit was we all know by now :) Tree-SHA512: 5f43ded9ce14e5e00b3876ec445b90acda1842f813149ae7bafa93f3ac3d510bb778e2c701187fd2c73585e6b87797bb2d2987139bd1a9ba7d58775a59392406
Summary: substitutes "for x in range(N):" by "for _ in range(N):" indicates to the reader that a block is just repeated N times, and that the loop counter is not used in the body Backport notes: - this PR also replaces a few occurences of `range(0, x)` with `range(x)` - test/functional/p2p_tx_download.py has been included in test/functional/p2p_inv_download.py, and was already up to date This is a backport of [[bitcoin/bitcoin#19674 | core#19674]] Test Plan: `ninja check-functional-extended` Reviewers: #bitcoin_abc, majcosta Reviewed By: #bitcoin_abc, majcosta Differential Revision: https://reviews.bitcoinabc.org/D10068
This tiny PR substitutes Python loops in the form of
for x in range(N): ...byfor _ in range(N): ...where applicable. The idea is indicating to the reader that a block (or statement, in list comprehensions) is just repeated N times, and that the loop counter is not used in the body, hence using the throwaway variable. This is already done quite often in the current tests (see e.g.$ git grep "for _ in range("). Another alternative would be usingitertools.repeat(according to Python core developer Raymond Hettinger it's even faster), but that doesn't seem to be widespread in use and I'm not sure about a readability increase.The only drawback I see is that whenever one wants to debug loop iterations, one would need to introduce a loop variable again. Reviewing this is basically a no-brainer, since tests would fail immediately if a a substitution has taken place on a loop where the variable is used.
Instances to replace were found by
$ git grep "for.*in range("and manually checked.