Skip to content

Commit

Permalink
Place Names bindings in load context within a ClassDef in non-local n…
Browse files Browse the repository at this point in the history
…amespace
  • Loading branch information
dflook committed Aug 9, 2024
1 parent f236db3 commit 194b5fc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/python_minifier/rename/bind_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ def get_binding(self, name, namespace):
# nonlocal names should not create a binding in any context
assert name not in namespace.nonlocal_names

if isinstance(namespace, ast.ClassDef):
binding = self.get_binding(name, get_nonlocal_namespace(namespace))
binding.disallow_rename()
return binding

for binding in namespace.bindings:
if binding.name == name:
break
Expand All @@ -43,6 +38,10 @@ def get_binding(self, name, namespace):
# This is actually a syntax error - but we want the same syntax error after minifying!
binding.disallow_rename()

if isinstance(namespace, ast.ClassDef):
# This name will become an attribute of the class, so it can't be renamed
binding.disallow_rename()

return binding

def visit_Name(self, node):
Expand Down
7 changes: 7 additions & 0 deletions src/python_minifier/rename/mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ def add_parent(node, parent=None, namespace=None):
if is_ast_node(node, 'Nonlocal'):
namespace.nonlocal_names.update(node.names)

if isinstance(node, ast.Name):
if isinstance(namespace, ast.ClassDef):
if isinstance(node.ctx, ast.Load):
namespace.nonlocal_names.add(node.id)
elif isinstance(node.ctx, ast.Store) and isinstance(node.parent, ast.AugAssign):
namespace.nonlocal_names.add(node.id)

for child in ast.iter_child_nodes(node):
add_parent(child, parent=node, namespace=namespace)

Expand Down

0 comments on commit 194b5fc

Please sign in to comment.