-
Notifications
You must be signed in to change notification settings - Fork 65
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
Create a LocalScope
for CollectionComprehensions
and generate a Declaration
#2019
Open
KuechA
wants to merge
12
commits into
main
Choose a base branch
from
ak/python-comprehension-scope
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3387229
Create scope for collection comprehensions
KuechA 209c218
generate local variables in python add declarations pass for comprehe…
KuechA 1ff35b8
Extend test to check fixes
KuechA 501d597
Remove useless flag
KuechA cb0b660
Add test case
KuechA 8b8fccd
Adapt test
KuechA c7893a6
Test resolution of param
KuechA 110c288
Reduce c&p code
KuechA 8011422
add documentation
KuechA 9489ecf
Move applyWithScope to Node
KuechA 091041a
python style
KuechA d75f3f3
Merge branch 'main' into ak/python-comprehension-scope
KuechA File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 11 additions & 4 deletions
15
cpg-language-python/src/test/resources/python/comprehension.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,31 @@ | ||
def foo(arg): | ||
return 7 | ||
|
||
def listComp(x, y): | ||
def list_comp(x, y): | ||
a = [foo(i) for i in x if i == 10] | ||
b = [foo(i) for i in x] | ||
c = {foo(i) for i in x if i == 10 if i < 20} | ||
d = [foo(i) for z in y if z in x for i in z if i == 10 ] | ||
foo(i) | ||
|
||
def setComp(x, y): | ||
def set_comp(x, y): | ||
a = {foo(i) for i in x if i == 10} | ||
b = {foo(i) for i in x} | ||
c = {foo(i) for i in x if i == 10 if i < 20} | ||
d = {foo(i) for z in y if z in x for i in z if i == 10 } | ||
|
||
def dictComp(x, y): | ||
def dict_comp(x, y): | ||
a = {i: foo(i) for i in x if i == 10} | ||
b = {i: foo(i) for i in x} | ||
c = {i: foo(i) for i in x if i == 10 if i < 20} | ||
d = {i: foo(i) for z in y if z in x for i in z if i == 10 } | ||
|
||
def generator(x, y): | ||
a = (i**2 for i in range(10) if i == 10) | ||
b = (i**2 for i in range(10)) | ||
b = (i**2 for i in range(10)) | ||
|
||
def bar(k, v): | ||
return k+v | ||
|
||
def tuple_comp(x): | ||
a = [bar(k, v) for (k, v) in x] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I am not sure, if this is really relevant in our case, but it seems that the exact definition is that only the control variable is not leaked into the outer scope, but I guess the only sane way to model this is exactly like you did by opening up a local scope for the comprehension.
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.
Interesting, from https://peps.python.org/pep-0709/ -> "Comprehensions are currently compiled as nested functions, which provides isolation of the comprehension’s iteration variable, but is inefficient at runtime."
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.
Hm, this sounds as if the current behavior would be appropriately modeled with the local scope but the intended/future behavior might be different.
Actually, we're only generating a
VariableDeclaration
inside this local scope for the variable of the comprehension expression and this is done inside thePythonAddDeclarationsPass
. Leaving the scope earlier (i.e., right after generating theComprehensionExpression
) here won't impact on resolving variables, right?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.
Yes, although I am not sure if this "new" bevahiour will leak the control variable or not. Maybe our PEP master @maximiliankaul understands this better than us.
I would assume so
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.
From my understanding, the variable should be available only in the
LocalScope
tied to theComprehensionExpression
. Obviously, we can always enter this scope to incorrectly resolve a variable but this shouldn't be the case. I also added a test which simulates the test case he provided in the original comment.