From b64bbc3732da7da9343b406d476d4dc46ac42eaf Mon Sep 17 00:00:00 2001 From: Henry Schreiner Date: Sun, 7 Apr 2024 13:16:19 -0400 Subject: [PATCH] refactor: nox.toml.load Signed-off-by: Henry Schreiner --- docs/tutorial.rst | 4 ++-- nox/__init__.py | 3 +-- nox/{_toml.py => toml.py} | 10 +++++++++- tests/test_toml.py | 10 +++++----- 4 files changed, 17 insertions(+), 10 deletions(-) rename nox/{_toml.py => toml.py} (92%) diff --git a/docs/tutorial.rst b/docs/tutorial.rst index 1aea1f42..6e6fd151 100644 --- a/docs/tutorial.rst +++ b/docs/tutorial.rst @@ -176,7 +176,7 @@ Loading dependencies from pyproject.toml or scripts One common need is loading a dependency list from a ``pyproject.toml`` file (say to prepare an environment without installing the package) or supporting `PEP 723 `_ scripts. Nox provides a helper to -load these with ``nox.load_toml``. It can be passed a filepath to a toml file +load these with ``nox.toml.load``. It can be passed a filepath to a toml file or to a script file following PEP 723. For example, if you have the following ``peps.py``: @@ -204,7 +204,7 @@ You can make a session for it like this: @nox.session def peps(session): - requirements = nox.load_toml("peps.py")["dependencies"] + requirements = nox.toml.load("peps.py")["dependencies"] session.install(*requirements) session.run("peps.py") diff --git a/nox/__init__.py b/nox/__init__.py index bb311db9..ef870684 100644 --- a/nox/__init__.py +++ b/nox/__init__.py @@ -17,7 +17,6 @@ from nox._options import noxfile_options as options from nox._parametrize import Param as param from nox._parametrize import parametrize_decorator as parametrize -from nox._toml import load_toml from nox.registry import session_decorator as session from nox.sessions import Session @@ -30,5 +29,5 @@ "session", "options", "Session", - "load_toml", + "toml", ] diff --git a/nox/_toml.py b/nox/toml.py similarity index 92% rename from nox/_toml.py rename to nox/toml.py index d3df54d6..13a1f216 100644 --- a/nox/_toml.py +++ b/nox/toml.py @@ -14,6 +14,14 @@ else: import tomllib + +__all__ = ["load"] + + +def __dir__() -> list[str]: + return __all__ + + # Note: the implementation (including this regex) taken from PEP 723 # https://peps.python.org/pep-0723 @@ -22,7 +30,7 @@ ) -def load_toml(filename: os.PathLike[str] | str) -> dict[str, Any]: +def load(filename: os.PathLike[str] | str) -> dict[str, Any]: """ Load a toml file or a script with a PEP 723 script block. diff --git a/tests/test_toml.py b/tests/test_toml.py index 7bda84c5..a461f5fe 100644 --- a/tests/test_toml.py +++ b/tests/test_toml.py @@ -17,7 +17,7 @@ def test_load_pyproject(tmp_path: Path) -> None: """ ) - toml = nox.load_toml(filepath) + toml = nox.toml.load(filepath) assert toml["project"]["dependencies"] == ["numpy", "requests"] @@ -46,7 +46,7 @@ def test_load_script_block(tmp_path: Path, example: str) -> None: ) ) - toml = nox.load_toml(filepath) + toml = nox.toml.load(filepath) assert toml["dependencies"] == ["requests<3", "rich"] @@ -68,7 +68,7 @@ def test_load_no_script_block(tmp_path: Path) -> None: ) with pytest.raises(ValueError, match="No script block found"): - nox.load_toml(filepath) + nox.toml.load(filepath) def test_load_multiple_script_block(tmp_path: Path) -> None: @@ -98,10 +98,10 @@ def test_load_multiple_script_block(tmp_path: Path) -> None: ) with pytest.raises(ValueError, match="Multiple script blocks found"): - nox.load_toml(filepath) + nox.toml.load(filepath) def test_load_non_recongnised_extension(): msg = "Extension must be .py or .toml, got .txt" with pytest.raises(ValueError, match=msg): - nox.load_toml("some.txt") + nox.toml.load("some.txt")