Skip to content

Commit

Permalink
refactor: nox.toml.load
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <[email protected]>
  • Loading branch information
henryiii committed Apr 7, 2024
1 parent c5ec073 commit b64bbc3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 10 deletions.
4 changes: 2 additions & 2 deletions docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://peps.python.org/pep-0723>`_ 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``:

Expand Down Expand Up @@ -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")
Expand Down
3 changes: 1 addition & 2 deletions nox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -30,5 +29,5 @@
"session",
"options",
"Session",
"load_toml",
"toml",
]
10 changes: 9 additions & 1 deletion nox/_toml.py → nox/toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions tests/test_toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]


Expand Down Expand Up @@ -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"]


Expand All @@ -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:
Expand Down Expand Up @@ -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")

0 comments on commit b64bbc3

Please sign in to comment.