Add import aliases to ancestor imports
This commit is contained in:
parent
8567892352
commit
76db2f153e
@ -127,8 +127,15 @@ def import_ancestor(current_package, imports, py_package, py_type):
|
||||
name = Bar
|
||||
"""
|
||||
distance_up = len(current_package) - len(py_package)
|
||||
imports.add(f"from .{'.' * distance_up} import {py_type}")
|
||||
return py_type
|
||||
if py_package:
|
||||
string_import = py_package[-1]
|
||||
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
|
||||
|
||||
|
||||
def import_cousin(current_package, imports, py_package, py_type):
|
||||
|
@ -174,8 +174,8 @@ def test_import_parent_package_from_child():
|
||||
package="package.child", imports=imports, source_type="package.Message"
|
||||
)
|
||||
|
||||
assert imports == {"from .. import Message"}
|
||||
assert name == "Message"
|
||||
assert imports == {"from ... import package as ___package"}
|
||||
assert name == "___package.Message"
|
||||
|
||||
|
||||
def test_import_parent_package_from_deeply_nested_child():
|
||||
@ -186,8 +186,20 @@ def test_import_parent_package_from_deeply_nested_child():
|
||||
source_type="package.deeply.nested.Message",
|
||||
)
|
||||
|
||||
assert imports == {"from .. import Message"}
|
||||
assert name == "Message"
|
||||
assert imports == {"from ... import nested as ___nested"}
|
||||
assert name == "___nested.Message"
|
||||
|
||||
|
||||
def test_import_ancestor_package_from_nested_child():
|
||||
imports = set()
|
||||
name = get_ref_type(
|
||||
package="package.ancestor.nested.child",
|
||||
imports=imports,
|
||||
source_type="package.ancestor.Message",
|
||||
)
|
||||
|
||||
assert imports == {"from .... import ancestor as ____ancestor"}
|
||||
assert name == "____ancestor.Message"
|
||||
|
||||
|
||||
def test_import_root_package_from_child():
|
||||
|
Loading…
x
Reference in New Issue
Block a user