Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: helper to get dependency-groups #876

Merged
merged 5 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ repos:
files: ^nox/
args: []
additional_dependencies:
- dependency-groups>=1.2
- jinja2
- packaging
- importlib_metadata
Expand Down
21 changes: 20 additions & 1 deletion nox/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from pathlib import Path
from typing import TYPE_CHECKING

import packaging.requirements
import packaging.specifiers
from dependency_groups import resolve

if TYPE_CHECKING:
from typing import Any
Expand All @@ -17,7 +19,7 @@
import tomllib


__all__ = ["load_toml", "python_versions"]
__all__ = ["load_toml", "python_versions", "dependency_groups"]


def __dir__() -> list[str]:
Expand Down Expand Up @@ -127,3 +129,20 @@ def python_versions(
max_minor_version = int(max_version.split(".")[1])

return [f"3.{v}" for v in range(min_minor_version, max_minor_version + 1)]


def dependency_groups(pyproject: dict[str, Any], *groups: str) -> tuple[str, ...]:
"""
Get a list of dependencies from a ``[dependency-groups]`` section(s).

Example:

.. code-block:: python

@nox.session
def test(session):
pyproject = nox.project.load_toml("pyproject.toml")
session.install(*nox.project.dependency_groups(pyproject, "dev"))
"""
dep_groups = pyproject["dependency-groups"]
return resolve(dep_groups, *groups)
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ classifiers = [
dependencies = [
"argcomplete<4,>=1.9.4",
"colorlog<7,>=2.6.1",
"dependency-groups>=1.1",
"packaging>=20.9",
"tomli>=1; python_version<'3.11'",
"virtualenv>=20.14.1",
Expand Down
33 changes: 32 additions & 1 deletion tests/test_project.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import pytest

from nox.project import python_versions
from nox.project import dependency_groups, python_versions


def test_classifiers():
Expand Down Expand Up @@ -63,3 +63,34 @@ def test_python_range_no_min():

with pytest.raises(ValueError, match="No minimum version found"):
python_versions(pyproject, max_version="3.5")


def test_dependency_groups():
example = {
"dependency-groups": {
"test": ["pytest", "coverage"],
"docs": ["sphinx", "sphinx-rtd-theme"],
"typing": ["mypy", "types-requests"],
"typing-test": [
{"include-group": "typing"},
{"include-group": "test"},
"useful-types",
],
}
}

assert dependency_groups(example, "test") == ("pytest", "coverage")
assert dependency_groups(example, "typing-test") == (
"mypy",
"types-requests",
"pytest",
"coverage",
"useful-types",
)
assert dependency_groups(example, "typing_test") == (
"mypy",
"types-requests",
"pytest",
"coverage",
"useful-types",
)
Loading