Skip to content
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

Fix _prettyprint after 'for i in range' changed to 'for i, val in enumerate'. #1384

Merged
merged 2 commits into from
Mar 24, 2022

Conversation

jpivarski
Copy link
Member

Just FYI @henryiii: changing

for i in range(len(out)):
    if i > 0:
        out[i] = " " + out[i]
    else:
        out[i] = "[" + out[i]
    if i < len(out) - 1:
        out[i] = out[i] + ","
    else:
        out[i] = out[i] + "]"

into a for loop with

for i, val in enumerate(out):

and all out[i] on the right-hand side of assignments converted into val broke this functionality. The original for loop relied on a side-effect in which out[i] was changed twice. Using an assigned-once name, val, prevented the first change from being seen in the final result.

I repeatedly run into cases in which side-effect turn out to be a bad idea (there's really something to this pure functional paradigm), and this could be counted as one of them. But side-effect based programming is a part of ordinary Python use and I wanted to bring to your attention that there are corner-cases like this in which for i in range(len(XYZ))for i, val in enumerate(XYZ) can be dangerous. Not to avoid these refactorings, but to keep an eye out for them!

(Also, I haven't yet checked the history to be sure that that's how this bug crept in, I'm just guessing that because it looks like it.)

@jpivarski jpivarski enabled auto-merge (squash) March 24, 2022 14:01
@jpivarski jpivarski disabled auto-merge March 24, 2022 21:14
@jpivarski jpivarski enabled auto-merge (squash) March 24, 2022 21:23
@codecov
Copy link

codecov bot commented Mar 24, 2022

Codecov Report

Merging #1384 (1b02d5b) into main (b2fd2be) will decrease coverage by 0.85%.
The diff coverage is 54.30%.

Impacted Files Coverage Δ
src/awkward/_v2/_connect/cling.py 0.00% <0.00%> (ø)
src/awkward/_v2/_connect/cuda/__init__.py 0.00% <0.00%> (ø)
src/awkward/_v2/_lookup.py 97.50% <0.00%> (ø)
src/awkward/_v2/_prettyprint.py 66.09% <0.00%> (+2.29%) ⬆️
src/awkward/_v2/identifier.py 55.69% <0.00%> (ø)
src/awkward/_v2/operations/convert/ak_from_jax.py 75.00% <0.00%> (ø)
src/awkward/_v2/operations/convert/ak_to_jax.py 75.00% <0.00%> (ø)
src/awkward/_v2/operations/io/ak_from_parquet.py 75.00% <0.00%> (ø)
src/awkward/_v2/operations/io/ak_to_parquet.py 75.00% <0.00%> (ø)
src/awkward/_v2/operations/structure/ak_firsts.py 75.00% <0.00%> (ø)
... and 152 more

@henryiii
Copy link
Member

I tried to watch for this, I know I avoided converting at least 1-2 because this side effect was being used. It's quite subtle - the new version is much better, IMO, since the reader can immediately see it's being intentionally reassigned.

@jpivarski jpivarski merged commit d946ae4 into main Mar 24, 2022
@jpivarski jpivarski deleted the jpivarski/fix-prettyprint-after-for-loop-change branch March 24, 2022 21:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants