From aac14c27a316f09064ff120c965da617eb31d8a7 Mon Sep 17 00:00:00 2001 From: Daniel Flook Date: Tue, 20 Aug 2024 09:03:08 +0100 Subject: [PATCH] Split namespace tests out for python3.3 --- test/test_bind_names.py | 115 +++--- test/test_bind_names_python33.py | 669 +++++++++++++++++++++++++++++++ 2 files changed, 718 insertions(+), 66 deletions(-) create mode 100644 test/test_bind_names_python33.py diff --git a/test/test_bind_names.py b/test/test_bind_names.py index 2fdef2ef..c37e842c 100644 --- a/test/test_bind_names.py +++ b/test/test_bind_names.py @@ -53,7 +53,7 @@ def namespace_name(node): return s def test_module_namespace(): - if sys.version_info >= (3, 4): + if sys.version_info < (3, 4): pytest.skip('Test requires python 3.4 or later') source = ''' @@ -68,26 +68,9 @@ def test_module_namespace(): assert_namespace_tree(source, expected_namespaces) -def test_module_namespace_python33(): - if sys.version_info < (3, 4) and sys.version_info >= (3, 0): - pytest.skip('Test requires python 3.3') - - source = ''' -name_in_module = True -name_in_module = True -''' - - expected_namespaces = ''' -+ Module - - NameBinding(name='name_in_module', allow_rename=True) - - BuiltinBinding(name='True', allow_rename=True) -''' - - assert_namespace_tree(source, expected_namespaces) - def test_lambda_namespace(): - if sys.version_info < (3, 0): - pytest.skip('Test requires python 3.0 or later') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' name_in_module = True @@ -108,8 +91,8 @@ def test_lambda_namespace(): assert_namespace_tree(source, expected_namespaces) def test_function_namespace(): - if sys.version_info < (3, 0): - pytest.skip('Test requires python 3.0 or later') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' name_in_module = True @@ -141,8 +124,8 @@ def inner_func(): assert_namespace_tree(source, expected_namespaces) def test_async_function_namespace(): - if sys.version_info < (3, 5): - pytest.skip('No async functions in python < 3.5') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' name_in_module = True @@ -176,8 +159,8 @@ async def inner_func(): # region generator namespace def test_generator_namespace(): - if sys.version_info < (3, 0): - pytest.skip('Test requires python 3.0 or later') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' a = (x for x in range(10)) @@ -194,8 +177,8 @@ def test_generator_namespace(): assert_namespace_tree(source, expected_namespaces) def test_multi_generator_namespace(): - if sys.version_info < (3, 0): - pytest.skip('Test requires python 3.0 or later') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' x = [] @@ -215,8 +198,8 @@ def test_multi_generator_namespace(): assert_namespace_tree(source, expected_namespaces) def test_multi_generator_namespace_2(): - if sys.version_info < (3, 0): - pytest.skip('Test requires python 3.0 or later') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' c = '' @@ -239,8 +222,8 @@ def test_multi_generator_namespace_2(): assert_namespace_tree(source, expected_namespaces) def test_nested_generator(): - if sys.version_info < (3, 0): - pytest.skip('Test requires python 3.0 or later') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' c = '' @@ -264,8 +247,8 @@ def test_nested_generator(): assert_namespace_tree(source, expected_namespaces) def test_nested_generator_2(): - if sys.version_info < (3, 0): - pytest.skip('Test requires python 3.0 or later') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' x = '' @@ -290,8 +273,8 @@ def test_nested_generator_2(): # region setcomp def test_setcomp_namespace(): - if sys.version_info < (3, 0): - pytest.skip('Test requires python 3.0 or later') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' a = {x for x in range(10)} @@ -308,8 +291,8 @@ def test_setcomp_namespace(): assert_namespace_tree(source, expected_namespaces) def test_multi_setcomp_namespace(): - if sys.version_info < (3, 0): - pytest.skip('Test requires python 3.0 or later') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' x = [] @@ -329,8 +312,8 @@ def test_multi_setcomp_namespace(): assert_namespace_tree(source, expected_namespaces) def test_multi_setcomp_namespace_2(): - if sys.version_info < (3, 0): - pytest.skip('Test requires python 3.0 or later') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' c = '' @@ -353,8 +336,8 @@ def test_multi_setcomp_namespace_2(): assert_namespace_tree(source, expected_namespaces) def test_nested_setcomp(): - if sys.version_info < (3, 0): - pytest.skip('Test requires python 3.0 or later') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' c = '' @@ -378,8 +361,8 @@ def test_nested_setcomp(): assert_namespace_tree(source, expected_namespaces) def test_nested_setcomp_2(): - if sys.version_info < (3, 0): - pytest.skip('Test requires python 3.0 or later') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' x = '' @@ -403,8 +386,8 @@ def test_nested_setcomp_2(): # region listcomp def test_listcomp_namespace(): - if sys.version_info < (3, 0): - pytest.skip('listcomp target bindings are different in python < 3.0') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' a = [x for x in range(10)] @@ -421,8 +404,8 @@ def test_listcomp_namespace(): assert_namespace_tree(source, expected_namespaces) def test_multi_listcomp_namespace(): - if sys.version_info < (3, 0): - pytest.skip('listcomp target bindings are different in python < 3.0') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' x = [] @@ -442,8 +425,8 @@ def test_multi_listcomp_namespace(): assert_namespace_tree(source, expected_namespaces) def test_multi_listcomp_namespace_2(): - if sys.version_info < (3, 0): - pytest.skip('listcomp target bindings are different in python < 3.0') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' c = '' @@ -466,8 +449,8 @@ def test_multi_listcomp_namespace_2(): assert_namespace_tree(source, expected_namespaces) def test_nested_listcomp(): - if sys.version_info < (3, 0): - pytest.skip('listcomp target bindings are different in python < 3.0') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' c = '' @@ -491,8 +474,8 @@ def test_nested_listcomp(): assert_namespace_tree(source, expected_namespaces) def test_nested_listcomp_2(): - if sys.version_info < (3, 0): - pytest.skip('listcomp target bindings are different in python < 3.0') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' x = '' @@ -516,8 +499,8 @@ def test_nested_listcomp_2(): # region dictcomp def test_dictcomp_namespace(): - if sys.version_info < (3, 0): - pytest.skip('Test requires python 3.0 or later') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' a = {x: x for x in range(10)} @@ -534,8 +517,8 @@ def test_dictcomp_namespace(): assert_namespace_tree(source, expected_namespaces) def test_multi_dictcomp_namespace(): - if sys.version_info < (3, 0): - pytest.skip('Test requires python 3.0 or later') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' x = [] @@ -555,8 +538,8 @@ def test_multi_dictcomp_namespace(): assert_namespace_tree(source, expected_namespaces) def test_multi_dictcomp_namespace_2(): - if sys.version_info < (3, 0): - pytest.skip('Test requires python 3.0 or later') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' c = '' @@ -579,8 +562,8 @@ def test_multi_dictcomp_namespace_2(): assert_namespace_tree(source, expected_namespaces) def test_nested_dictcomp(): - if sys.version_info < (3, 0): - pytest.skip('Test requires python 3.0 or later') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' c = '' @@ -604,8 +587,8 @@ def test_nested_dictcomp(): assert_namespace_tree(source, expected_namespaces) def test_nested_dictcomp_2(): - if sys.version_info < (3, 0): - pytest.skip('Test requires python 3.0 or later') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' x = {} @@ -663,8 +646,8 @@ class C: def test_class_name_rebinding(): - if sys.version_info < (3, 0): - pytest.skip('Test requires python 3.0 or later') + if sys.version_info < (3, 4): + pytest.skip('Test requires python 3.4 or later') source = ''' OhALongName = 'Hello' diff --git a/test/test_bind_names_python33.py b/test/test_bind_names_python33.py new file mode 100644 index 00000000..d527d922 --- /dev/null +++ b/test/test_bind_names_python33.py @@ -0,0 +1,669 @@ +import ast +import sys + +import pytest + +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 + + +def test_module_namespace(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +name_in_module = True +name_in_module = True +''' + + expected_namespaces = ''' ++ Module + - BuiltinBinding(name='True', allow_rename=True) + - NameBinding(name='name_in_module', allow_rename=True) +''' + + assert_namespace_tree(source, expected_namespaces) + + +def test_lambda_namespace(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +name_in_module = 0 + +b = lambda arg, *args, **kwargs: arg + 1 +''' + + expected_namespaces = ''' ++ Module + - NameBinding(name='b', allow_rename=True) + - NameBinding(name='name_in_module', allow_rename=True) + + Lambda + - NameBinding(name='arg', allow_rename=False) + - NameBinding(name='args', allow_rename=True) + - NameBinding(name='kwargs', allow_rename=True) +''' + + assert_namespace_tree(source, expected_namespaces) + + +def test_function_namespace(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +name_in_module = True + +def func(arg, *args, **kwargs): + name_in_func = True + print(name_in_module) + + def inner_func(): + print(name_in_module) + name_in_inner_func = True +''' + + expected_namespaces = ''' ++ Module + - BuiltinBinding(name='True', allow_rename=True) + - NameBinding(name='func', allow_rename=True) + - NameBinding(name='name_in_module', allow_rename=True) + - BuiltinBinding(name='print', allow_rename=True) + + Function func + - NameBinding(name='arg', allow_rename=True) + - NameBinding(name='args', allow_rename=True) + - NameBinding(name='inner_func', allow_rename=True) + - NameBinding(name='kwargs', allow_rename=True) + - NameBinding(name='name_in_func', allow_rename=True) + + Function inner_func + - NameBinding(name='name_in_inner_func', allow_rename=True) +''' + + assert_namespace_tree(source, expected_namespaces) + + +# region generator namespace + +def test_generator_namespace(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +a = (x for x in range(10)) +''' + + expected_namespaces = ''' ++ Module + - NameBinding(name='a', allow_rename=True) + - BuiltinBinding(name='range', allow_rename=True) + + GeneratorExp + - NameBinding(name='x', allow_rename=True) +''' + + assert_namespace_tree(source, expected_namespaces) + + +def test_multi_generator_namespace(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +x = [] +f = [] +a = (x for x in f for x in x) +''' + + expected_namespaces = ''' ++ Module + - NameBinding(name='a', allow_rename=True) + - NameBinding(name='f', allow_rename=True) + - NameBinding(name='x', allow_rename=True) + + GeneratorExp + - NameBinding(name='x', allow_rename=True) +''' + + assert_namespace_tree(source, expected_namespaces) + + +def test_multi_generator_namespace_2(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +c = '' +line = '' +file = [] +a = (c for line in file for c in line) +''' + + expected_namespaces = ''' ++ Module + - NameBinding(name='a', allow_rename=True) + - NameBinding(name='c', allow_rename=True) + - NameBinding(name='file', allow_rename=True) + - NameBinding(name='line', allow_rename=True) + + GeneratorExp + - NameBinding(name='c', allow_rename=True) + - NameBinding(name='line', allow_rename=True) +''' + + assert_namespace_tree(source, expected_namespaces) + + +def test_nested_generator(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +c = '' +line = '' +file = [] +a = (c for c in (line for line in file)) +''' + + expected_namespaces = ''' ++ Module + - NameBinding(name='a', allow_rename=True) + - NameBinding(name='c', allow_rename=True) + - NameBinding(name='file', allow_rename=True) + - NameBinding(name='line', allow_rename=True) + + GeneratorExp + - NameBinding(name='c', allow_rename=True) + + GeneratorExp + - NameBinding(name='line', allow_rename=True) +''' + + assert_namespace_tree(source, expected_namespaces) + + +def test_nested_generator_2(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +x = '' +a = (x for x in (x for x in x)) +''' + + expected_namespaces = ''' ++ Module + - NameBinding(name='a', allow_rename=True) + - NameBinding(name='x', allow_rename=True) + + GeneratorExp + - NameBinding(name='x', allow_rename=True) + + GeneratorExp + - NameBinding(name='x', allow_rename=True) +''' + + assert_namespace_tree(source, expected_namespaces) + + +# endregion + + +# region setcomp + +def test_setcomp_namespace(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +a = {x for x in range(10)} +''' + + expected_namespaces = ''' ++ Module + - NameBinding(name='a', allow_rename=True) + - BuiltinBinding(name='range', allow_rename=True) + + SetComp + - NameBinding(name='x', allow_rename=True) +''' + + assert_namespace_tree(source, expected_namespaces) + + +def test_multi_setcomp_namespace(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +x = [] +f = [] +a = {x for x in f for x in x} +''' + + expected_namespaces = ''' ++ Module + - NameBinding(name='a', allow_rename=True) + - NameBinding(name='f', allow_rename=True) + - NameBinding(name='x', allow_rename=True) + + SetComp + - NameBinding(name='x', allow_rename=True) +''' + + assert_namespace_tree(source, expected_namespaces) + + +def test_multi_setcomp_namespace_2(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +c = '' +line = '' +file = [] +a = {c for line in file for c in line} +''' + + expected_namespaces = ''' ++ Module + - NameBinding(name='a', allow_rename=True) + - NameBinding(name='c', allow_rename=True) + - NameBinding(name='file', allow_rename=True) + - NameBinding(name='line', allow_rename=True) + + SetComp + - NameBinding(name='c', allow_rename=True) + - NameBinding(name='line', allow_rename=True) +''' + + assert_namespace_tree(source, expected_namespaces) + + +def test_nested_setcomp(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +c = '' +line = '' +file = [] +a = {c for c in {line for line in file}} +''' + + expected_namespaces = ''' ++ Module + - NameBinding(name='a', allow_rename=True) + - NameBinding(name='c', allow_rename=True) + - NameBinding(name='file', allow_rename=True) + - NameBinding(name='line', allow_rename=True) + + SetComp + - NameBinding(name='c', allow_rename=True) + + SetComp + - NameBinding(name='line', allow_rename=True) +''' + + assert_namespace_tree(source, expected_namespaces) + + +def test_nested_setcomp_2(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +x = '' +a = {x for x in {x for x in x}} +''' + + expected_namespaces = ''' ++ Module + - NameBinding(name='a', allow_rename=True) + - NameBinding(name='x', allow_rename=True) + + SetComp + - NameBinding(name='x', allow_rename=True) + + SetComp + - NameBinding(name='x', allow_rename=True) +''' + + assert_namespace_tree(source, expected_namespaces) + + +# endregion + +# region listcomp + +def test_listcomp_namespace(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +a = [x for x in range(10)] +''' + + expected_namespaces = ''' ++ Module + - NameBinding(name='a', allow_rename=True) + - BuiltinBinding(name='range', allow_rename=True) + + ListComp + - NameBinding(name='x', allow_rename=True) +''' + + assert_namespace_tree(source, expected_namespaces) + + +def test_multi_listcomp_namespace(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +x = [] +f = [] +a = [x for x in f for x in x] +''' + + expected_namespaces = ''' ++ Module + - NameBinding(name='a', allow_rename=True) + - NameBinding(name='f', allow_rename=True) + - NameBinding(name='x', allow_rename=True) + + ListComp + - NameBinding(name='x', allow_rename=True) +''' + + assert_namespace_tree(source, expected_namespaces) + + +def test_multi_listcomp_namespace_2(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +c = '' +line = '' +file = [] +a = [c for line in file for c in line] +''' + + expected_namespaces = ''' ++ Module + - NameBinding(name='a', allow_rename=True) + - NameBinding(name='c', allow_rename=True) + - NameBinding(name='file', allow_rename=True) + - NameBinding(name='line', allow_rename=True) + + ListComp + - NameBinding(name='c', allow_rename=True) + - NameBinding(name='line', allow_rename=True) +''' + + assert_namespace_tree(source, expected_namespaces) + + +def test_nested_listcomp(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +c = '' +line = '' +file = [] +a =[c for c in [line for line in file]] +''' + + expected_namespaces = ''' ++ Module + - NameBinding(name='a', allow_rename=True) + - NameBinding(name='c', allow_rename=True) + - NameBinding(name='file', allow_rename=True) + - NameBinding(name='line', allow_rename=True) + + ListComp + - NameBinding(name='c', allow_rename=True) + + ListComp + - NameBinding(name='line', allow_rename=True) +''' + + assert_namespace_tree(source, expected_namespaces) + + +def test_nested_listcomp_2(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +x = '' +a =[x for x in [x for x in x]] +''' + + expected_namespaces = ''' ++ Module + - NameBinding(name='a', allow_rename=True) + - NameBinding(name='x', allow_rename=True) + + ListComp + - NameBinding(name='x', allow_rename=True) + + ListComp + - NameBinding(name='x', allow_rename=True) +''' + + assert_namespace_tree(source, expected_namespaces) + + +# endregion + +# region dictcomp + +def test_dictcomp_namespace(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +a = {x: x for x in range(10)} +''' + + expected_namespaces = ''' ++ Module + - NameBinding(name='a', allow_rename=True) + - BuiltinBinding(name='range', allow_rename=True) + + DictComp + - NameBinding(name='x', allow_rename=True) +''' + + assert_namespace_tree(source, expected_namespaces) + + +def test_multi_dictcomp_namespace(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +x = [] +f = [] +a = {x: x for x, x in f for x, x in x} +''' + + expected_namespaces = ''' ++ Module + - NameBinding(name='a', allow_rename=True) + - NameBinding(name='f', allow_rename=True) + - NameBinding(name='x', allow_rename=True) + + DictComp + - NameBinding(name='x', allow_rename=True) +''' + + assert_namespace_tree(source, expected_namespaces) + + +def test_multi_dictcomp_namespace_2(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +c = '' +line = '' +file = [] +a = {c: c for line, line in file for c, c in line} +''' + + expected_namespaces = ''' ++ Module + - NameBinding(name='a', allow_rename=True) + - NameBinding(name='c', allow_rename=True) + - NameBinding(name='file', allow_rename=True) + - NameBinding(name='line', allow_rename=True) + + DictComp + - NameBinding(name='c', allow_rename=True) + - NameBinding(name='line', allow_rename=True) +''' + + assert_namespace_tree(source, expected_namespaces) + + +def test_nested_dictcomp(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +c = '' +line = '' +file = [] +a = {c: c for c, c in {line: line for line in file}} +''' + + expected_namespaces = ''' ++ Module + - NameBinding(name='a', allow_rename=True) + - NameBinding(name='c', allow_rename=True) + - NameBinding(name='file', allow_rename=True) + - NameBinding(name='line', allow_rename=True) + + DictComp + - NameBinding(name='c', allow_rename=True) + + DictComp + - NameBinding(name='line', allow_rename=True) +''' + + assert_namespace_tree(source, expected_namespaces) + + +def test_nested_dictcomp_2(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +x = {} +a = {x:x for x, x in {x: x for x in x}} +''' + + expected_namespaces = ''' ++ Module + - NameBinding(name='a', allow_rename=True) + - NameBinding(name='x', allow_rename=True) + + DictComp + - NameBinding(name='x', allow_rename=True) + + DictComp + - NameBinding(name='x', allow_rename=True) +''' + + assert_namespace_tree(source, expected_namespaces) + + +# endregion + +def test_class_namespace(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +OhALongName = 'Hello' +OhALongName = 'Hello' +MyOtherName = 'World' + +def func(): + class C: + OhALongName = ' World' + MyOtherName = OhALongName + ClassName = 0 + +func() +''' + + expected_namespaces = ''' ++ Module + - NameBinding(name='MyOtherName', allow_rename=True) + - NameBinding(name='OhALongName', allow_rename=False) + - NameBinding(name='func', allow_rename=True) + + Function func + - NameBinding(name='C', allow_rename=True) + + Class C + - nonlocal OhALongName + - NameBinding(name='ClassName', allow_rename=False) + - NameBinding(name='MyOtherName', allow_rename=False) +''' + + assert_namespace_tree(source, expected_namespaces) + + +def test_class_name_rebinding(): + if sys.version_info < (3, 3) or sys.version_info > (3, 4): + pytest.skip('Test is for python3.3 only') + + source = ''' +OhALongName = 'Hello' +OhALongName = 'Hello' +MyOtherName = 'World' + +def func(): + class C: + OhALongName = OhALongName + ' World' + MyOtherName = OhALongName + + +func() +''' + + expected_namespaces = ''' ++ Module + - NameBinding(name='MyOtherName', allow_rename=True) + - NameBinding(name='OhALongName', allow_rename=False) + - NameBinding(name='func', allow_rename=True) + + Function func + - NameBinding(name='C', allow_rename=True) + + Class C + - nonlocal OhALongName + - NameBinding(name='MyOtherName', allow_rename=False) +''' + + assert_namespace_tree(source, expected_namespaces)