From b1eb1f7dba1b9f6aa9a59dd8e375f7ac02af565b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Thu, 12 Nov 2026 17:11:36 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix(api):=20keep=20internals=20o?= =?UTF-8?q?ut=20of=20exports?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pyproject_api/__main__.py | 10 ++------ src/pyproject_api/_backend.py | 19 ++++---------- src/pyproject_api/_frontend.py | 3 +-- src/pyproject_api/_via_fresh_subprocess.py | 2 +- .../test_via_fresh_subprocess.py | 25 +++++++++++-------- 5 files changed, 23 insertions(+), 36 deletions(-) diff --git a/src/pyproject_api/__main__.py b/src/pyproject_api/__main__.py index 7d26cb5..8b9ee46 100644 --- a/src/pyproject_api/__main__.py +++ b/src/pyproject_api/__main__.py @@ -12,7 +12,7 @@ from ._frontend import EditableResult, SdistResult, WheelResult -def main_parser() -> argparse.ArgumentParser: +def main_parser() -> argparse.ArgumentParser: # simplint: ignore[unexported-public-name] # CLI parser is internal parser = argparse.ArgumentParser( description=( "A pyproject.toml-based build frontend. " @@ -61,7 +61,7 @@ def main_parser() -> argparse.ArgumentParser: return parser -def main(argv: list[str]) -> None: +def main(argv: list[str]) -> None: # simplint: ignore[unexported-public-name] # CLI entry point is not library API args = main_parser().parse_args(argv) outdir = (args.outdir or args.srcdir / "dist").absolute() @@ -90,11 +90,5 @@ def main(argv: list[str]) -> None: print(res.err, file=sys.stderr) # ruff:ignore[print] # CLI output for the user -__all__ = [ - "main", - "main_parser", # simplint: ignore[unused-export] # the CLI parser is public API, importable from this module -] - - if __name__ == "__main__": main(sys.argv[1:]) diff --git a/src/pyproject_api/_backend.py b/src/pyproject_api/_backend.py index c1ec671..e6dd909 100644 --- a/src/pyproject_api/_backend.py +++ b/src/pyproject_api/_backend.py @@ -13,11 +13,11 @@ import traceback -class MissingCommand(TypeError): # ruff:ignore[error-suffix-on-exception-name] # the frontend reports it by this name +class MissingCommand(TypeError): # ruff:ignore[error-suffix-on-exception-name] # the frontend reports it by this name # simplint: ignore[missing-from-all] # backend-only exception ... -class BackendProxy: +class BackendProxy: # simplint: ignore[missing-from-all] # backend-only proxy def __init__(self, backend_module, backend_obj): self.backend_module = backend_module self.backend_object = backend_obj @@ -54,12 +54,12 @@ def _optional_hooks(self): } -def flush(): +def flush(): # simplint: ignore[missing-from-all] # backend-only output helper sys.stderr.flush() sys.stdout.flush() -def run(argv): +def run(argv): # simplint: ignore[missing-from-all] # backend script entry point reuse_process = argv[0].lower() == "true" try: backend_proxy = BackendProxy(argv[1], None if len(argv) == 2 else argv[2]) # ruff:ignore[magic-value-comparison] # argv may omit the backend object @@ -141,7 +141,7 @@ def _write_response(result_file, result, response): flush() # pragma: no branch # the finally runs on every path -def read_line(fd=0): +def read_line(fd=0): # simplint: ignore[missing-from-all] # backend-only input helper # for some reason input() seems to break (hangs forever) so instead we read byte by byte the unbuffered stream content = bytearray() while True: @@ -158,15 +158,6 @@ def read_line(fd=0): return content -__all__ = [ - "BackendProxy", - "MissingCommand", - "flush", - "read_line", - "run", -] - - if __name__ == "__main__": # printed paths may hold characters the stdout encoding cannot represent sys.stdout.reconfigure(errors="backslashreplace") # type: ignore[unresolved-attribute] # stub says TextIO diff --git a/src/pyproject_api/_frontend.py b/src/pyproject_api/_frontend.py index 46e3d43..0f4ca6a 100644 --- a/src/pyproject_api/_frontend.py +++ b/src/pyproject_api/_frontend.py @@ -22,7 +22,7 @@ _HERE: Final[Path] = Path(__file__).parent -ConfigSettings = dict[str, Any] | None +ConfigSettings = dict[str, Any] | None # simplint: ignore[missing-from-all] # used only in this module's annotations # the backend answers over a JSON protocol, so every value it returns is one of these JsonValue: TypeAlias = str | int | float | bool | list["JsonValue"] | dict[str, "JsonValue"] | None # tomllib yields these types for every value in a parsed document @@ -643,7 +643,6 @@ def _in_tree_path(folder: Path, entry: str) -> Path: __all__ = [ "BackendFailed", "CmdStatus", - "ConfigSettings", "EditableResult", "Frontend", "MetadataForBuildEditableResult", diff --git a/src/pyproject_api/_via_fresh_subprocess.py b/src/pyproject_api/_via_fresh_subprocess.py index 73426b4..5b1220b 100644 --- a/src/pyproject_api/_via_fresh_subprocess.py +++ b/src/pyproject_api/_via_fresh_subprocess.py @@ -16,7 +16,7 @@ from packaging.requirements import Requirement -class SubprocessCmdStatus(CmdStatus, Thread): +class SubprocessCmdStatus(CmdStatus, Thread): # simplint: ignore[unexported-public-name] # internal worker status def __init__(self, process: Popen[str], msg: str | None = None) -> None: super().__init__() self.process = process diff --git a/tests/pyproject_api/test_via_fresh_subprocess.py b/tests/pyproject_api/test_via_fresh_subprocess.py index 3a1c285..264c0b2 100644 --- a/tests/pyproject_api/test_via_fresh_subprocess.py +++ b/tests/pyproject_api/test_via_fresh_subprocess.py @@ -1,12 +1,14 @@ from __future__ import annotations +import sys +import threading +from subprocess import Popen # ruff:ignore[suspicious-subprocess-import] # inspect the spawned backend from textwrap import dedent from typing import TYPE_CHECKING, Final import pytest from pyproject_api import SubprocessFrontend -from pyproject_api._via_fresh_subprocess import SubprocessCmdStatus if TYPE_CHECKING: from pathlib import Path @@ -38,18 +40,19 @@ def build_wheel(wheel_directory, config_settings=None, metadata_directory=None): """), encoding="utf-8", ) - status_constructor: Final = mocker.spy(SubprocessCmdStatus, "__init__") + process_constructor: Final = mocker.spy(sys.modules[SubprocessFrontend.__module__], "Popen") mocker.patch("pyproject_api._frontend.sleep", autospec=True, side_effect=error_type("build interrupted")) + threads_before: Final = set(threading.enumerate()) - try: - with pytest.raises(error_type): - SubprocessFrontend(*SubprocessFrontend.create_args_from_folder(tmp_path)[:-1]).build_wheel( - tmp_path / "dist" - ) + with pytest.raises(error_type): + SubprocessFrontend(*SubprocessFrontend.create_args_from_folder(tmp_path)[:-1]).build_wheel(tmp_path / "dist") - assert not status_constructor.call_args.args[0].is_alive() + process: Final = process_constructor.spy_return + assert isinstance(process, Popen) + try: + assert process.poll() is not None + assert all(thread.daemon for thread in set(threading.enumerate()) - threads_before) finally: # A failed cleanup test must not leave a live child process. - status: Final = status_constructor.call_args.args[0] - status.process.kill() - status.join(timeout=10) + process.kill() + process.wait(timeout=10)