-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make sure combined imports maintain the same namespace
- Loading branch information
Showing
6 changed files
with
180 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import ast | ||
|
||
from python_minifier.rename import add_namespace, resolve_names | ||
from python_minifier.rename.bind_names import bind_names | ||
from python_minifier.rename.util import iter_child_namespaces | ||
from python_minifier.util import is_ast_node | ||
|
||
|
||
def assert_namespace_tree(source, expected_tree): | ||
tree = ast.parse(source) | ||
|
||
add_namespace(tree) | ||
bind_names(tree) | ||
resolve_names(tree) | ||
|
||
actual = print_namespace(tree) | ||
|
||
print(actual) | ||
assert actual.strip() == expected_tree.strip() | ||
|
||
|
||
def print_namespace(namespace, indent=''): | ||
s = '' | ||
|
||
if not indent: | ||
s += '\n' | ||
|
||
def namespace_name(node): | ||
if is_ast_node(node, (ast.FunctionDef, 'AsyncFunctionDef')): | ||
return 'Function ' + node.name | ||
elif isinstance(node, ast.ClassDef): | ||
return 'Class ' + node.name | ||
else: | ||
return namespace.__class__.__name__ | ||
|
||
s += indent + '+ ' + namespace_name(namespace) + '\n' | ||
|
||
for name in sorted(namespace.global_names): | ||
s += indent + ' - global ' + name + '\n' | ||
|
||
for name in sorted(namespace.nonlocal_names): | ||
s += indent + ' - nonlocal ' + name + '\n' | ||
|
||
for binding in sorted(namespace.bindings, key=lambda b: b.name): | ||
s += indent + ' - ' + repr(binding) + '\n' | ||
|
||
for child in iter_child_namespaces(namespace): | ||
s += print_namespace(child, indent=indent + ' ') | ||
|
||
return s |
Oops, something went wrong.