From 62b0927516ca345de61aa3cc03e977d4d37220de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Sun, 2 Jan 2022 12:20:33 +0100 Subject: [PATCH] feat: Handle relative imports --- src/griffe/agents/visitor.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/griffe/agents/visitor.py b/src/griffe/agents/visitor.py index c6e743b2..78345716 100644 --- a/src/griffe/agents/visitor.py +++ b/src/griffe/agents/visitor.py @@ -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,