Skip to content

Commit

Permalink
Disable renaming of type parameters
Browse files Browse the repository at this point in the history
It's not practical to rename type parameters safely until we can properly model annotation namespaces.
  • Loading branch information
dflook committed Sep 5, 2024
1 parent d5cc78d commit e128644
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/python_minifier/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,14 @@ def minify(
rename_globals = False
rename_locals = False

if preserve_locals is None:
preserve_locals = []
if preserve_globals is None:
preserve_globals = []

preserve_locals.extend(module.preserved)
preserve_globals.extend(module.preserved)

allow_rename_locals(module, rename_locals, preserve_locals)
allow_rename_globals(module, rename_globals, preserve_globals)

Expand Down
7 changes: 7 additions & 0 deletions src/python_minifier/rename/bind_names.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class NameBinder(NodeVisitor):
def __call__(self, module):
assert isinstance(module, ast.Module)
module.tainted = False
module.preserved = set()
return self.visit(module)

def get_binding(self, name, namespace):
Expand Down Expand Up @@ -165,14 +166,20 @@ def visit_TypeVar(self, node):
if node.name not in node.namespace.nonlocal_names:
self.get_binding(node.name, node.namespace).add_reference(node)

get_global_namespace(node.namespace).preserved.add(node.name)

def visit_TypeVarTuple(self, node):
if node.name not in node.namespace.nonlocal_names:
self.get_binding(node.name, node.namespace).add_reference(node)

get_global_namespace(node.namespace).preserved.add(node.name)

def visit_ParamSpec(self, node):
if node.name not in node.namespace.nonlocal_names:
self.get_binding(node.name, node.namespace).add_reference(node)

get_global_namespace(node.namespace).preserved.add(node.name)

def bind_names(module):
"""
Bind names to their local namespace
Expand Down

0 comments on commit e128644

Please sign in to comment.