Skip to content

Commit ee27ad9

Browse files
committed
fix: Fix target path of aliases for multipart imports (import a.b.c as x)
Issue-259: #259
1 parent 01da648 commit ee27ad9

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/griffe/agents/visitor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ def visit_import(self, node: ast.Import) -> None:
435435
node: The node to visit.
436436
"""
437437
for name in node.names:
438-
alias_path = name.name.split(".", 1)[0]
438+
alias_path = name.name if name.asname else name.name.split(".", 1)[0]
439439
alias_name = name.asname or alias_path.split(".", 1)[0]
440440
self.current.imports[alias_name] = alias_path
441441
self.current.set_member(

tests/test_nodes.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,20 @@ def test_relative_to_absolute_imports(code: str, path: str, is_package: bool, ex
124124
assert relative_to_absolute(node, name, module) == expected
125125

126126

127+
def test_multipart_imports() -> None:
128+
"""Assert that a multipart path like `a.b.c` imported as `x` points to the right target."""
129+
with temporary_visited_module(
130+
"""
131+
import pkg.b.c
132+
import pkg.b.c as alias
133+
""",
134+
) as module:
135+
pkg = module["pkg"]
136+
alias = module["alias"]
137+
assert pkg.target_path == "pkg"
138+
assert alias.target_path == "pkg.b.c"
139+
140+
127141
@pytest.mark.parametrize(
128142
"expression",
129143
[

0 commit comments

Comments
 (0)