-
Notifications
You must be signed in to change notification settings - Fork 193
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
[scope] keep track of assignment/access ordering #413
Conversation
libcst/metadata/scope_provider.py
Outdated
if earlier_accesses != set() and self.scope.parent != self.scope: | ||
for shadowed_assignment in self.scope.parent[self.name]: | ||
shadowed_assignment.record_accesses(earlier_accesses) |
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.
Why do we need this?
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.
NVM, I got it. It's to handle the accesses not part of later access. Add them to parent assignments make sense.
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.
Exactly!
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 used to have a comment here, it might've got lost during rebases. I'll add it back
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.
Other than the comments, LGTM.
Summary
This PR makes sure references are never attached to assignments that are declared later than the access (but still in the same scope). It does so by keeping track of the relative ordering of accesses to assignments, through the inclusion of an
__index
member in both. The idea is to increment the index every time an assignment "happens" in a scope. This is a bit tricky because fora = b
, the right hand side cannot refer to variables on the left side even if the left side is visited first.Fixes #326
Test Plan
Added extensive test case for a variable shadowing in a class/global scope scenario.