Skip to content

Commit

Permalink
feat: Support newer versions of editables
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Apr 2, 2023
1 parent c7ec7f7 commit ab7a3be
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
11 changes: 7 additions & 4 deletions src/griffe/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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())
Expand Down
5 changes: 3 additions & 2 deletions tests/test_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")]

Expand Down

0 comments on commit ab7a3be

Please sign in to comment.