Skip to content

Commit

Permalink
fix: Fix relative imports to absolute with wildcards
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Feb 16, 2022
1 parent 87ff1df commit 69500dd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/griffe/agents/visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ def visit_import(self, node: ast.Import) -> None:
node: The node to visit.
"""
for name in node.names:
alias_path = name.name.split(".", 1)[0]
alias_name = name.asname or alias_path
alias_path = name.name
alias_name = name.asname or alias_path.split(".", 1)[0]
self.current.imports[alias_name] = alias_path
self.current[alias_name] = Alias(
alias_name,
Expand All @@ -399,12 +399,12 @@ def visit_importfrom(self, node: ast.ImportFrom) -> None: # noqa: WPS231
# have the same name and can be accessed the same way
continue

alias_path = relative_to_absolute(node, name, self.current.module)
if name.name == "*":
alias_name = node.module.replace(".", "/") + "/*" # type: ignore[union-attr]
alias_path = node.module
alias_name = alias_path.replace(".", "/") # type: ignore[union-attr]
alias_path = alias_path.replace(".*", "")
else:
alias_name = name.asname or name.name
alias_path = relative_to_absolute(node, name, self.current.module)
self.current.imports[alias_name] = alias_path
self.current[alias_name] = Alias(
alias_name,
Expand Down
8 changes: 8 additions & 0 deletions tests/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
("from ... import f", "a.c.d.i", False, "a.f"),
("from ...b import f", "a.c.d.i", False, "a.b.f"),
("from ...c.d import e", "a.c.d.i", False, "a.c.d.e"),
("from .c import *", "a", False, "a.c.*"),
("from .c import *", "a.b", False, "a.c.*"),
("from .b import *", "a.b", True, "a.b.b.*"),
("from .. import *", "a.c.d.i", False, "a.c.*"),
("from ..d import *", "a.c.d.i", False, "a.c.d.*"),
("from ... import *", "a.c.d.i", False, "a.*"),
("from ...b import *", "a.c.d.i", False, "a.b.*"),
("from ...c.d import *", "a.c.d.i", False, "a.c.d.*"),
],
)
def test_relative_to_absolute_imports(code, path, is_package, expected):
Expand Down

0 comments on commit 69500dd

Please sign in to comment.