diff --git a/src/griffe/_internal/loader.py b/src/griffe/_internal/loader.py index 15ea774db..cfe2be80c 100644 --- a/src/griffe/_internal/loader.py +++ b/src/griffe/_internal/loader.py @@ -9,6 +9,7 @@ import tempfile from contextlib import suppress from datetime import datetime, timezone +from functools import cached_property from importlib.util import find_spec from pathlib import Path from typing import TYPE_CHECKING, ClassVar, cast @@ -90,13 +91,17 @@ def __init__( """Whether to force inspecting (importing) modules, even when sources were found.""" self.store_source: bool = store_source """Whether to store source code in the lines collection.""" - self.finder: ModuleFinder = ModuleFinder(search_paths) - """The module source finder.""" + self._search_paths: Sequence[str | Path] | None = search_paths self._time_stats: dict = { "time_spent_visiting": 0, "time_spent_inspecting": 0, } + @cached_property + def finder(self) -> ModuleFinder: + """The module source finder.""" + return ModuleFinder(search_paths=self._search_paths) + def load( self, objspec: str | Path | None = None, diff --git a/tests/test_loader.py b/tests/test_loader.py index f1adb329f..6708f15da 100644 --- a/tests/test_loader.py +++ b/tests/test_loader.py @@ -519,3 +519,14 @@ def test_loading_utf8_with_bom_files(tmp_path: Path) -> None: loader = GriffeLoader(search_paths=[tmp_path]) package = loader.load("pkg") assert "func" in package.members # Just checking all went well, no SyntaxError exceptions raised. + + +def test_deferred_finder(tmp_path: Path) -> None: + """Check that the deferred finder works as expected.""" + ns = tmp_path / "ns" + l1 = GriffeLoader(search_paths=[tmp_path]) + ns.mkdir(exist_ok=False) + l2 = GriffeLoader(search_paths=[tmp_path]) + l1_result = l1.load("ns") + l2_result = l2.load("ns") + assert l1_result.as_dict() == l2_result.as_dict()