Skip to content

Commit c52dd22

Browse files
bswckpawamoy
andauthored
fix: Defer creating module finder until first load
Issue-410: #410 PR-411: #411 Co-authored-by: Timothée Mazzucotelli <[email protected]>
1 parent 06c2f2f commit c52dd22

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/griffe/_internal/loader.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import tempfile
1010
from contextlib import suppress
1111
from datetime import datetime, timezone
12+
from functools import cached_property
1213
from importlib.util import find_spec
1314
from pathlib import Path
1415
from typing import TYPE_CHECKING, ClassVar, cast
@@ -90,13 +91,17 @@ def __init__(
9091
"""Whether to force inspecting (importing) modules, even when sources were found."""
9192
self.store_source: bool = store_source
9293
"""Whether to store source code in the lines collection."""
93-
self.finder: ModuleFinder = ModuleFinder(search_paths)
94-
"""The module source finder."""
94+
self._search_paths: Sequence[str | Path] | None = search_paths
9595
self._time_stats: dict = {
9696
"time_spent_visiting": 0,
9797
"time_spent_inspecting": 0,
9898
}
9999

100+
@cached_property
101+
def finder(self) -> ModuleFinder:
102+
"""The module source finder."""
103+
return ModuleFinder(search_paths=self._search_paths)
104+
100105
def load(
101106
self,
102107
objspec: str | Path | None = None,

tests/test_loader.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,3 +519,14 @@ def test_loading_utf8_with_bom_files(tmp_path: Path) -> None:
519519
loader = GriffeLoader(search_paths=[tmp_path])
520520
package = loader.load("pkg")
521521
assert "func" in package.members # Just checking all went well, no SyntaxError exceptions raised.
522+
523+
524+
def test_deferred_finder(tmp_path: Path) -> None:
525+
"""Check that the deferred finder works as expected."""
526+
ns = tmp_path / "ns"
527+
l1 = GriffeLoader(search_paths=[tmp_path])
528+
ns.mkdir(exist_ok=False)
529+
l2 = GriffeLoader(search_paths=[tmp_path])
530+
l1_result = l1.load("ns")
531+
l2_result = l2.load("ns")
532+
assert l1_result.as_dict() == l2_result.as_dict()

0 commit comments

Comments
 (0)