Skip to content

Commit

Permalink
fix: Better handle relative imports
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Jan 2, 2022
1 parent 338760e commit 91b42de
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/griffe/agents/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ def visit_import(self, node: ast.Import) -> None:
)
self.generic_visit(node)

def visit_importfrom(self, node: ast.ImportFrom) -> None:
def visit_importfrom(self, node: ast.ImportFrom) -> None: # noqa: WPS231
"""Visit an "import from" node.
Parameters:
Expand All @@ -392,11 +392,18 @@ def visit_importfrom(self, node: ast.ImportFrom) -> None:
level = node.level
module_path = node.module
if level > 0:
if module_path is None:
level -= 1
parent: Module = self.current.module
if parent.is_package or parent.is_subpackage:
level -= 1
while level > 0:
parent = parent.parent # type: ignore[assignment]
level -= 1
module_path = f"{parent.path}.{module_path}"
if module_path:
module_path = f"{parent.path}.{module_path}"
else:
module_path = parent.path
if alias_name == "*":
alias_name = module_path.replace(".", "/") + "/*" # type: ignore[union-attr]
alias_path = module_path
Expand Down

0 comments on commit 91b42de

Please sign in to comment.