Skip to content

Commit

Permalink
feat: Support meson-python editable modules
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Jan 15, 2024
1 parent 7978f88 commit 9123897
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/griffe/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
_editable_editables_patterns = [re.compile(pat) for pat in (r"^__editables_\w+\.py$", "^_editable_impl_\\w+\\.py$")]
_editable_setuptools_patterns = [re.compile(pat) for pat in ("^__editable__\\w+\\.py$",)]
_editable_scikit_build_core_patterns = [re.compile(pat) for pat in (r"^_\w+_editable.py$",)]
_editable_meson_python_patterns = [re.compile(pat) for pat in (r"^_\w+_editable_loader.py$",)]


def _match_pattern(string: str, patterns: Sequence[Pattern]) -> bool:
Expand Down Expand Up @@ -421,6 +422,14 @@ def _handle_editable_module(path: Path) -> list[Path]:
and node.targets[0].id == "MAPPING"
) and isinstance(node.value, ast.Dict):
return [Path(cst.value).parent for cst in node.value.values if isinstance(cst, ast.Constant)]
if _match_pattern(path.name, _editable_meson_python_patterns):
# Support for how 'meson-python' writes these files:
# example line: `install({'package', 'module1'}, '/media/data/dev/griffe/build/cp311', ["path"], False)`.
# Compiled modules then found in the cp311 folder, under src/package.
parsed_module = ast.parse(path.read_text())
for node in parsed_module.body:
if isinstance(node, ast.Expr) and isinstance(node.value, ast.Call) and node.value.func.id == "install":
return [Path(node.value.args[1].value, "src")]
raise UnhandledEditableModuleError(path)


Expand Down
13 changes: 13 additions & 0 deletions tests/test_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,19 @@ def test_scikit_build_core_file_handling(tmp_path: Path) -> None:
assert _handle_editable_module(pth_file) == [Path("/path/to/whatever")]


def test_meson_python_file_handling(tmp_path: Path) -> None:
"""Assert editable modules by `meson-python` are handled.
Parameters:
tmp_path: Pytest fixture.
"""
pth_file = tmp_path / "_whatever_editable_loader.py"
pth_file.write_text(
"hello=1\ninstall({'whatever', 'hello'}, '/path/to/build', ['/tmp/ninja'], False)",
)
assert _handle_editable_module(pth_file) == [Path("/path/to/build/src")]


@pytest.mark.parametrize(
("first", "second", "find_stubs", "expect"),
[
Expand Down

0 comments on commit 9123897

Please sign in to comment.