From ab7a3be3902af5f4af1d1e762b2b6e532826569f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Mazzucotelli?= Date: Sun, 2 Apr 2023 21:31:13 +0200 Subject: [PATCH] feat: Support newer versions of `editables` --- src/griffe/finder.py | 11 +++++++---- tests/test_finder.py | 5 +++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/griffe/finder.py b/src/griffe/finder.py index 0e59abb9..b578fb47 100644 --- a/src/griffe/finder.py +++ b/src/griffe/finder.py @@ -20,6 +20,9 @@ NamePartsType = Tuple[str, ...] NamePartsAndPathType = Tuple[NamePartsType, Path] logger = get_logger(__name__) +editable_editables_prefixes = ("__editables_", "_editable_impl_") +editable_setuptools_prefixes = ("__editable__",) +editable_prefixes = (*editable_editables_prefixes, *editable_setuptools_prefixes) class Package: @@ -271,7 +274,7 @@ def _extend_from_pth_files(self) -> None: def _extend_from_editable_modules(self) -> None: for path in self.search_paths: for item in self._contents(path): - if item.stem.startswith(("__editables_", "__editable__")) and item.suffix == ".py": + if item.stem.startswith(editable_prefixes) and item.suffix == ".py": with suppress(UnhandledEditableModuleError): for editable_path in _handle_editable_module(item): self._append_search_path(editable_path) @@ -341,15 +344,15 @@ def _handle_editable_module(path: Path) -> list[Path]: editable_lines = path.read_text(encoding="utf8").splitlines(keepends=False) except FileNotFoundError as error: raise UnhandledEditableModuleError(path) from error - if path.name.startswith("__editables_"): - # support for how 'editables' writes these files: + if path.name.startswith(editable_editables_prefixes): + # support for how 'editables' write these files: # example line: F.map_module('griffe', '/media/data/dev/griffe/src/griffe/__init__.py') new_path = Path(editable_lines[-1].split("'")[3]) if new_path.exists(): # TODO: could remove existence check if new_path.name.startswith("__init__"): return [new_path.parent.parent] return [new_path] - elif path.name.startswith("__editable__"): + elif path.name.startswith(editable_setuptools_prefixes): # support for how 'setuptools' writes these files: # example line: MAPPING = {'griffe': '/media/data/dev/griffe/src/griffe', 'briffe': '/media/data/dev/griffe/src/briffe'} parsed_module = ast.parse(path.read_text()) diff --git a/tests/test_finder.py b/tests/test_finder.py index b44f7eed..ab5d6b30 100644 --- a/tests/test_finder.py +++ b/tests/test_finder.py @@ -94,13 +94,14 @@ def test_pth_file_handling(tmp_path: Path) -> None: assert directories == [Path("tests")] -def test_editables_file_handling(tmp_path: Path) -> None: +@pytest.mark.parametrize("editable_file_name", ["__editables_whatever.py", "_editable_impl_whatever.py"]) +def test_editables_file_handling(tmp_path: Path, editable_file_name: str) -> None: """Assert editable modules by `editables` are handled. Parameters: tmp_path: Pytest fixture. """ - pth_file = tmp_path / "__editables_whatever.py" + pth_file = tmp_path / editable_file_name pth_file.write_text("hello\nF.map_module('griffe', 'src/griffe/__init__.py')") assert _handle_editable_module(pth_file) == [Path("src")]