Fixes issue where importing cousin where path has a package with the same name broke import

This commit is contained in:
boukeversteegh
2020-06-09 21:07:20 +02:00
parent 7c8d47de6d
commit 3105e952ea
2 changed files with 25 additions and 6 deletions

View File

@@ -1,3 +1,4 @@
import os
import re
from typing import Dict, List, Type
@@ -138,16 +139,14 @@ def import_cousin(current_package, imports, py_package, py_type):
package = foo.bar.baz
name = foo.example.Bar
"""
shared_ancestory = [
pair[0] for pair in zip(current_package, py_package) if pair[0] == pair[1]
]
distance_up = len(current_package) - len(shared_ancestory)
shared_ancestry = os.path.commonprefix([current_package, py_package])
distance_up = len(current_package) - len(shared_ancestry)
string_from = f".{'.' * distance_up}" + ".".join(
py_package[len(shared_ancestory) : -1]
py_package[len(shared_ancestry) : -1]
)
string_import = py_package[-1]
alias = f"{'_' * distance_up}" + safe_snake_case(
".".join(py_package[len(shared_ancestory) :])
".".join(py_package[len(shared_ancestry) :])
)
imports.add(f"from {string_from} import {string_import} as {alias}")
return f"{alias}.{py_type}"