Avoid naming conflicts when importing multiple types with the same name from an ancestor package

This commit is contained in:
boukeversteegh 2020-07-01 12:07:59 +02:00
parent e3135ce766
commit 81711d2427
2 changed files with 9 additions and 7 deletions

View File

@ -121,18 +121,20 @@ def reference_ancestor(
"""
Returns a reference to a python type in a package which is an ancestor to the current package,
and adds the required import that is aliased (if possible) to avoid name conflicts.
Adds trailing __ to avoid name mangling (python.org/dev/peps/pep-0008/#id34).
"""
distance_up = len(current_package) - len(py_package)
if py_package:
string_import = py_package[-1]
# Add trailing __ to avoid name mangling (python.org/dev/peps/pep-0008/#id34)
string_alias = f"_{'_' * distance_up}{string_import}__"
string_from = f"..{'.' * distance_up}"
imports.add(f"from {string_from} import {string_import} as {string_alias}")
return f"{string_alias}.{py_type}"
else:
imports.add(f"from .{'.' * distance_up} import {py_type}")
return py_type
string_alias = f"{'_' * distance_up}{py_type}__"
imports.add(f"from .{'.' * distance_up} import {py_type} as {string_alias}")
return string_alias
def reference_cousin(

View File

@ -214,8 +214,8 @@ def test_reference_root_package_from_child():
package="package.child", imports=imports, source_type="Message"
)
assert imports == {"from ... import Message"}
assert name == "Message"
assert imports == {"from ... import Message as __Message__"}
assert name == "__Message__"
def test_reference_root_package_from_deeply_nested_child():
@ -224,8 +224,8 @@ def test_reference_root_package_from_deeply_nested_child():
package="package.deeply.nested.child", imports=imports, source_type="Message"
)
assert imports == {"from ..... import Message"}
assert name == "Message"
assert imports == {"from ..... import Message as ____Message__"}
assert name == "____Message__"
def test_reference_unrelated_package():