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 assignment/access ordering in comprehensions #423

Merged
merged 4 commits into from
Dec 1, 2020

Conversation

zsol
Copy link
Member

@zsol zsol commented Nov 20, 2020

Summary

This change fixes attaching accesses to the right assignment in list comprehensions, so in

a = 1
[a for a in [1,2,3]]

The a at the beginning of the list comprehension is now attached to the a declared after for instead of the declaration on the first line.

Test Plan

Added unit tests

@zsol zsol requested a review from jimmylai November 20, 2020 11:06
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 20, 2020
@codecov-io
Copy link

codecov-io commented Nov 20, 2020

Codecov Report

Merging #423 (57b7bad) into master (16b30fe) will increase coverage by 0.01%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #423      +/-   ##
==========================================
+ Coverage   94.31%   94.32%   +0.01%     
==========================================
  Files         232      232              
  Lines       22531    22574      +43     
==========================================
+ Hits        21251    21294      +43     
  Misses       1280     1280              
Impacted Files Coverage Δ
libcst/metadata/scope_provider.py 96.55% <100.00%> (+<0.01%) ⬆️
libcst/metadata/tests/test_scope_provider.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 16b30fe...57b7bad. Read the comment docs.

a_param = f.params.params[0].name
a_param_assignment = list(scopes[a_param]["a"])[0]
a_param_refs = list(a_param_assignment.references)
self.assertEqual(a_param_refs, [])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't the param a have references in the comprehensions (the last a appear in the comprehension)?
[a for a in [] for b in a]
[b for a in [] for b in a]

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, that last a refers to the binding in the first for, so it's equivalent to:

def f(a_1):
    [a_2 for a_2 in [] for b_1 in a_2]
    [b_2 for a_3 in [] for b_2 in a_3]
    [a_5 for a_4 in [] for a_5 in []]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I didn't aware the nested for loop comprehension.
[a for a in a] is an example that I'm curious about whether it'll work fine.
The 3rd a should be an access of the function parameter a.
The first a is an Access of the Assignment of the 2nd a.
Can you also add this as a test case?

Copy link
Contributor

@jimmylai jimmylai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the comment, LGTM.

a_param = f.params.params[0].name
a_param_assignment = list(scopes[a_param]["a"])[0]
a_param_refs = list(a_param_assignment.references)
self.assertEqual(a_param_refs, [])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I didn't aware the nested for loop comprehension.
[a for a in a] is an example that I'm curious about whether it'll work fine.
The 3rd a should be an access of the function parameter a.
The first a is an Access of the Assignment of the 2nd a.
Can you also add this as a test case?

@zsol zsol merged commit 1326a0e into Instagram:master Dec 1, 2020
@zsol zsol deleted the scope-ordering-comprehension branch December 1, 2020 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants