Skip to content

Commit

Permalink
fix: Always call on_package_loaded hook on a package, and not any o…
Browse files Browse the repository at this point in the history
…ther object

Issue-283: #283
  • Loading branch information
pawamoy committed Jun 7, 2024
1 parent e438336 commit 40db38d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
10 changes: 5 additions & 5 deletions src/griffe/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def load_module(
DeprecationWarning,
stacklevel=2,
)
return self.load(
return self.load( # type: ignore[return-value]
module,
submodules=submodules,
try_relative_path=try_relative_path,
Expand All @@ -129,7 +129,7 @@ def load(
find_stubs_package: bool = False,
# TODO: Remove at some point.
module: str | Path | None = None,
) -> Object:
) -> Object | Alias:
"""Load an object as a Griffe object, given its Python or file path.
Note that this will load the whole object's package,
Expand Down Expand Up @@ -230,7 +230,7 @@ def load(

# Package is loaded, we now retrieve the initially requested object and return it.
obj = self.modules_collection.get_member(obj_path)
self.extensions.call("on_package_loaded", pkg=obj)
self.extensions.call("on_package_loaded", pkg=top_module)
return obj

def resolve_aliases(
Expand Down Expand Up @@ -746,7 +746,7 @@ def load(
resolve_aliases: bool = False,
resolve_external: bool | None = None,
resolve_implicit: bool = False,
) -> Object:
) -> Object | Alias:
"""Load and return a module.
Example:
Expand Down Expand Up @@ -839,7 +839,7 @@ def load_git(
resolve_aliases: bool = False,
resolve_external: bool | None = None,
resolve_implicit: bool = False,
) -> Object:
) -> Object | Alias:
"""Load and return a module from a specific Git reference.
This function will create a temporary
Expand Down
13 changes: 12 additions & 1 deletion tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
if TYPE_CHECKING:
from pathlib import Path

from griffe.dataclasses import Alias


def test_has_docstrings_does_not_try_to_resolve_alias() -> None:
"""Assert that checkins presence of docstrings does not trigger alias resolution."""
Expand Down Expand Up @@ -48,7 +50,7 @@ def test_recursive_wildcard_expansion() -> None:
assert "CONST_X" not in package.members
assert "CONST_Y" not in package.members

loader.expand_wildcards(package)
loader.expand_wildcards(package) # type: ignore[arg-type]

assert "CONST_X" in package["mod_a"].members
assert "CONST_Y" in package["mod_a"].members
Expand Down Expand Up @@ -475,3 +477,12 @@ def raise_module_not_found_error(*args, **kwargs) -> None: # noqa: ARG001,ANN00
loader = GriffeLoader()
monkeypatch.setattr(loader.finder, "find_spec", raise_module_not_found_error)
assert loader.load("griffe")


def test_not_calling_package_loaded_hook_on_something_else_than_package() -> None:
"""Always call the `on_package_loaded` hook on a package, not any other object."""
with temporary_pypackage("pkg", {"__init__.py": "from typing import List as L"}) as pkg:
loader = GriffeLoader(search_paths=[pkg.tmpdir])
alias: Alias = loader.load("pkg.L") # type: ignore[assignment]
assert alias.is_alias
assert not alias.resolved

0 comments on commit 40db38d

Please sign in to comment.