From f95ef025163a17f365fb5cb0d85d8cbd56a35dfd Mon Sep 17 00:00:00 2001 From: Zsolt Dollenstein Date: Mon, 14 Dec 2020 17:23:37 +0000 Subject: [PATCH] Don't allow out of order accesses in the global scope --- libcst/metadata/scope_provider.py | 2 +- libcst/metadata/tests/test_scope_provider.py | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/libcst/metadata/scope_provider.py b/libcst/metadata/scope_provider.py index f4d35836e..b5c6ba68a 100644 --- a/libcst/metadata/scope_provider.py +++ b/libcst/metadata/scope_provider.py @@ -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 diff --git a/libcst/metadata/tests/test_scope_provider.py b/libcst/metadata/tests/test_scope_provider.py index a84d0532b..d1566aac9 100644 --- a/libcst/metadata/tests/test_scope_provider.py +++ b/libcst/metadata/tests/test_scope_provider.py @@ -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(