Avoid naming conflicts when importing multiple types with the same name from an ancestor package
This commit is contained in:
		| @@ -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, |     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. |     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) |     distance_up = len(current_package) - len(py_package) | ||||||
|     if py_package: |     if py_package: | ||||||
|         string_import = py_package[-1] |         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_alias = f"_{'_' * distance_up}{string_import}__" | ||||||
|         string_from = f"..{'.' * distance_up}" |         string_from = f"..{'.' * distance_up}" | ||||||
|         imports.add(f"from {string_from} import {string_import} as {string_alias}") |         imports.add(f"from {string_from} import {string_import} as {string_alias}") | ||||||
|         return f"{string_alias}.{py_type}" |         return f"{string_alias}.{py_type}" | ||||||
|     else: |     else: | ||||||
|         imports.add(f"from .{'.' * distance_up} import {py_type}") |         string_alias = f"{'_' * distance_up}{py_type}__" | ||||||
|         return py_type |         imports.add(f"from .{'.' * distance_up} import {py_type} as {string_alias}") | ||||||
|  |         return string_alias | ||||||
|  |  | ||||||
|  |  | ||||||
| def reference_cousin( | def reference_cousin( | ||||||
|   | |||||||
| @@ -214,8 +214,8 @@ def test_reference_root_package_from_child(): | |||||||
|         package="package.child", imports=imports, source_type="Message" |         package="package.child", imports=imports, source_type="Message" | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
|     assert imports == {"from ... import Message"} |     assert imports == {"from ... import Message as __Message__"} | ||||||
|     assert name == "Message" |     assert name == "__Message__" | ||||||
|  |  | ||||||
|  |  | ||||||
| def test_reference_root_package_from_deeply_nested_child(): | 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" |         package="package.deeply.nested.child", imports=imports, source_type="Message" | ||||||
|     ) |     ) | ||||||
|  |  | ||||||
|     assert imports == {"from ..... import Message"} |     assert imports == {"from ..... import Message as ____Message__"} | ||||||
|     assert name == "Message" |     assert name == "____Message__" | ||||||
|  |  | ||||||
|  |  | ||||||
| def test_reference_unrelated_package(): | def test_reference_unrelated_package(): | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user