Skip to content

Commit

Permalink
fix: Don't cut through wildcard-expanded aliases chains
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Feb 24, 2022
1 parent e080549 commit 65dafa4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
19 changes: 1 addition & 18 deletions src/griffe/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,24 +210,7 @@ def expand_wildcards(self, obj: Object, seen: set | None = None) -> None: # noq
del obj[name] # noqa: WPS420

for new_member, alias_lineno, alias_endlineno in expanded:
if new_member.is_alias:
try:
# TODO: maybe don't shortcut aliases:
# we want to keep public paths
alias = Alias(
new_member.name,
new_member.target, # type: ignore[union-attr]
lineno=alias_lineno,
endlineno=alias_endlineno,
)
except AliasResolutionError:
alias = new_member # type: ignore[assignment] # noqa: WPS437
except CyclicAliasError as error: # noqa: WPS440
logger.debug(str(error))
continue
else:
alias = Alias(new_member.name, new_member, lineno=alias_lineno, endlineno=alias_endlineno)
obj[new_member.name] = alias
obj[new_member.name] = Alias(new_member.name, new_member, lineno=alias_lineno, endlineno=alias_endlineno)

def resolve_module_aliases( # noqa: WPS231
self,
Expand Down
23 changes: 23 additions & 0 deletions tests/test_loader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Tests for the `loader` module."""

from griffe.expressions import Name
from griffe.loader import GriffeLoader
from tests.helpers import temporary_pyfile, temporary_pypackage

Expand Down Expand Up @@ -41,3 +42,25 @@ def test_recursive_wildcard_expansion():
assert "CONST_Y" in package["mod_a"].members
assert "CONST_X" in package["mod_a.mod_b"].members
assert "CONST_Y" in package["mod_a.mod_b"].members


def test_dont_shortcut_alias_chain_after_expanding_wildcards():
"""Assert public aliases paths are not resolved to canonical paths when expanding wildcards."""
with temporary_pypackage("package", ["mod_a.py", "mod_b.py", "mod_c.py"]) as tmp_package:
mod_a = tmp_package.path / "mod_a.py"
mod_b = tmp_package.path / "mod_b.py"
mod_c = tmp_package.path / "mod_c.py"

mod_a.write_text("from package.mod_b import *\nclass Child(Base): ...\n")
mod_b.write_text("from package.mod_c import Base\n__all__ = ['Base']\n")
mod_c.write_text("class Base: ...\n")

loader = GriffeLoader(search_paths=[tmp_package.tmpdir])
package = loader.load_module(tmp_package.name)
loader.resolve_aliases(package)
child = package["mod_a.Child"]
assert child.bases
base = child.bases[0]
assert isinstance(base, Name)
assert base.source == "Base"
assert base.full == "package.mod_b.Base"

0 comments on commit 65dafa4

Please # to comment.