Skip to content

Commit

Permalink
feat: Handle relative imports
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Jan 2, 2022
1 parent e6df277 commit 62b0927
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/griffe/agents/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,14 +386,21 @@ def visit_importfrom(self, node: ast.ImportFrom) -> None:
Parameters:
node: The node to visit.
"""
# TODO: does this handle relative imports?
for name in node.names:
alias_name = name.asname or name.name
level = node.level
module_path = node.module
if level > 0:
parent: Module = self.current.module
while level > 0:
parent = parent.parent # type: ignore[assignment]
level -= 1
module_path = f"{parent.path}.{module_path}"
if alias_name == "*":
alias_name = node.module.replace(".", "/") + "/*" # type: ignore[union-attr]
alias_path = node.module
alias_name = module_path.replace(".", "/") + "/*" # type: ignore[union-attr]
alias_path = module_path
else:
alias_path = f"{node.module}.{name.name}"
alias_path = f"{module_path}.{name.name}"
self.current.imports[alias_name] = alias_path
self.current[alias_name] = Alias(
alias_name,
Expand Down

0 comments on commit 62b0927

Please # to comment.