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

Don't allow out of order accesses in the global scope #431

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libcst/metadata/scope_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def record_assignments(self, name: str) -> None:
for assignment in assignments
if assignment.scope != self.scope or assignment._index < self.__index
}
if not previous_assignments and assignments:
if not previous_assignments and assignments and self.scope.parent != self.scope:
previous_assignments = self.scope.parent[name]
self.__assignments |= previous_assignments

Expand Down
18 changes: 18 additions & 0 deletions libcst/metadata/tests/test_scope_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -1591,6 +1591,24 @@ def f():
),
)

def test_no_out_of_order_references_in_global_scope(self) -> None:
m, scopes = get_scope_metadata_provider(
"""
x = y
y = 1
"""
)
for scope in scopes.values():
for acc in scope.accesses:
self.assertEqual(
len(acc.referents),
0,
msg=(
"Access for node has incorrect number of referents: "
+ f"{acc.node}"
),
)

def test_cast(self) -> None:
with self.assertRaises(cst.ParserSyntaxError):
m, scopes = get_scope_metadata_provider(
Expand Down