Skip to content

Commit

Permalink
FEAT: convert dev dependencies to dependency groups (#459)
Browse files Browse the repository at this point in the history
* BEHAVIOR: always install dependencies through `uv export`
* BEHAVIOR: switch to `--group` instead of `--extra`
* DX: rename `sty` dependency group to `style`
* DX: run `pyright` in `tox -e sty` job
* ENH: define `dependency-groups` in `PyprojectTOML`
* ENH: install `ruff` into `dev`, not `style`
* ENH: remove output `requirements.txt` file
* MAINT: autoupdate pre-commit hooks
* MAINT: clean up dependencies
* MAINT: split `add_dependency()` function
  • Loading branch information
redeboer authored Oct 28, 2024
1 parent d3f0649 commit 4e7f635
Show file tree
Hide file tree
Showing 15 changed files with 290 additions and 130 deletions.
1 change: 1 addition & 0 deletions .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"PyPA",
"pytest",
"PYTHONHASHSEED",
"QRules",
"rtoml",
"sympy",
"toctree",
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ on:

jobs:
doc:
uses: ComPWA/actions/.github/workflows/ci-docs.yml@v2
uses: ComPWA/actions/.github/workflows/ci-docs.yml@v2.1
permissions:
pages: write
id-token: write
with:
gh-pages: true
specific-pip-packages: ${{ inputs.specific-pip-packages }}
pytest:
uses: ComPWA/actions/.github/workflows/pytest.yml@v2
uses: ComPWA/actions/.github/workflows/pytest.yml@v2.1
with:
coverage-target: compwa_policy
macos-python-version: "3.9"
Expand All @@ -45,4 +45,4 @@ jobs:
if: inputs.specific-pip-packages == ''
secrets:
token: ${{ secrets.PAT }}
uses: ComPWA/actions/.github/workflows/pre-commit.yml@v2
uses: ComPWA/actions/.github/workflows/pre-commit.yml@v2.1
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repos:
- id: check-useless-excludes

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.6.9
rev: v0.7.1
hooks:
- id: ruff
args: [--fix]
Expand Down Expand Up @@ -78,7 +78,7 @@ repos:
exclude: (?x)^(labels/.*\.toml)$

- repo: https://github.com/streetsidesoftware/cspell-cli
rev: v8.15.1
rev: v8.15.2
hooks:
- id: cspell

Expand All @@ -104,6 +104,6 @@ repos:
- python

- repo: https://github.com/ComPWA/pyright-pre-commit
rev: v1.1.384
rev: v1.1.386
hooks:
- id: pyright
57 changes: 28 additions & 29 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,43 @@ maintainers = [{email = "[email protected]"}]
name = "compwa-policy"
requires-python = ">=3.9"

[project.optional-dependencies]
[project.readme]
content-type = "text/markdown"
file = "README.md"

[project.scripts]
check-dev-files = "compwa_policy.check_dev_files:main"
colab-toc-visible = "compwa_policy.colab_toc_visible:main"
fix-nbformat-version = "compwa_policy.fix_nbformat_version:main"
remove-empty-tags = "compwa_policy.remove_empty_tags:main"
self-check = "compwa_policy.self_check:main"
set-nb-cells = "compwa_policy.set_nb_cells:main"

[project.urls]
Source = "https://github.com/ComPWA/policy"
Tracker = "https://github.com/ComPWA/policy/issues"

[dependency-groups]
dev = [
"compwa-policy[doc]",
"compwa-policy[sty]",
"compwa-policy[test]",
"labels",
"ruff",
"sphinx-autobuild",
{include-group = "doc"},
{include-group = "style"},
{include-group = "test"},
]
doc = [
"Sphinx",
"myst-parser",
"sphinx-api-relink >=0.0.4",
"sphinx",
"sphinx-api-relink",
"sphinx-argparse",
"sphinx-book-theme",
"sphinx-codeautolink",
"sphinx-copybutton",
]
sty = [
"compwa-policy[types]",
style = [
"mypy",
"ruff",
{include-group = "types"},
]
test = [
"pytest",
Expand All @@ -59,27 +74,11 @@ test = [
]
types = [
"pytest",
"sphinx-api-relink >=0.0.3",
"sphinx-api-relink",
"types-PyYAML",
"types-toml",
]

[project.readme]
content-type = "text/markdown"
file = "README.md"

[project.scripts]
check-dev-files = "compwa_policy.check_dev_files:main"
colab-toc-visible = "compwa_policy.colab_toc_visible:main"
fix-nbformat-version = "compwa_policy.fix_nbformat_version:main"
remove-empty-tags = "compwa_policy.remove_empty_tags:main"
self-check = "compwa_policy.self_check:main"
set-nb-cells = "compwa_policy.set_nb_cells:main"

[project.urls]
Source = "https://github.com/ComPWA/policy"
Tracker = "https://github.com/ComPWA/policy/issues"

[tool.setuptools]
include-package-data = false
license-files = ["LICENSE"]
Expand Down Expand Up @@ -169,6 +168,8 @@ reportUnusedFunction = true
reportUnusedImport = true
reportUnusedVariable = true
typeCheckingMode = "strict"
venv = ".venv"
venvPath = "."

[tool.pytest.ini_options]
addopts = [
Expand Down Expand Up @@ -358,8 +359,6 @@ setenv =
allowlist_externals =
pre-commit
commands =
pre-commit run {posargs} --all-files
pre-commit run --all-files {posargs}
description = Perform all linting, formatting, and spelling checks
setenv =
SKIP = pyright
"""
6 changes: 3 additions & 3 deletions src/compwa_policy/.github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ on:

jobs:
doc:
uses: ComPWA/actions/.github/workflows/ci-docs.yml@v2
uses: ComPWA/actions/.github/workflows/ci-docs.yml@v2.1
permissions:
pages: write
id-token: write
with:
specific-pip-packages: ${{ inputs.specific-pip-packages }}
pytest:
uses: ComPWA/actions/.github/workflows/pytest.yml@v2
uses: ComPWA/actions/.github/workflows/pytest.yml@v2.1
with:
specific-pip-packages: ${{ inputs.specific-pip-packages }}
style:
if: inputs.specific-pip-packages == ''
secrets:
token: ${{ secrets.PAT }}
uses: ComPWA/actions/.github/workflows/pre-commit.yml@v2
uses: ComPWA/actions/.github/workflows/pre-commit.yml@v2.1
88 changes: 34 additions & 54 deletions src/compwa_policy/check_dev_files/binder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
import os
from dataclasses import dataclass
from textwrap import dedent
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

from compwa_policy.errors import PrecommitError
from compwa_policy.utilities import CONFIG_PATH
from compwa_policy.utilities.executor import Executor
from compwa_policy.utilities.match import git_ls_files
from compwa_policy.utilities.pyproject import Pyproject

if TYPE_CHECKING:
from collections.abc import Mapping
from pathlib import Path

from compwa_policy.check_dev_files.conda import PackageManagerChoice
Expand Down Expand Up @@ -84,28 +84,16 @@ def __get_post_builder_for_pixi_with_uv() -> str:
for script in activation.scripts:
expected_content += "\nbash " + script
expected_content += "\npixi clean cache --yes\n"
notebook_extras = __get_notebook_extras()
if "uv.lock" in set(git_ls_files(untracked=True)):
expected_content += "\nuv export \\"
for extra in notebook_extras:
expected_content += f"\n --extra {extra} \\"
expected_content += dedent(R"""
> requirements.txt
uv pip install \
--requirement requirements.txt \
--system
uv cache clean
""")
else:
package = "."
if notebook_extras:
package = f"'.[{','.join(notebook_extras)}]'"
expected_content += dedent(Rf"""
uv pip install \
--editable {package} \
--no-cache \
--system
""")
expected_content += "\nuv export \\"
for groups in __get_notebook_groups():
expected_content += f"\n --group {groups} \\"
expected_content += dedent(R"""
> requirements.txt
uv pip install \
--requirement requirements.txt \
--system
uv cache clean
""")
return expected_content


Expand Down Expand Up @@ -135,41 +123,33 @@ def __get_post_builder_for_uv() -> str:
curl -LsSf https://astral.sh/uv/install.sh | sh
source $HOME/.cargo/env
""").strip()
notebook_extras = __get_notebook_extras()
if "uv.lock" in set(git_ls_files(untracked=True)):
expected_content += "\nuv export \\"
for extra in notebook_extras:
expected_content += f"\n --extra {extra} \\"
expected_content += dedent(R"""
> requirements.txt
uv pip install \
--requirement requirements.txt \
--system
uv cache clean
""")
else:
package = "."
if notebook_extras:
package = f"'.[{','.join(notebook_extras)}]'"
expected_content += dedent(Rf"""
uv pip install \
--editable {package} \
--no-cache \
--system
""")
expected_content += "\nuv export \\"
for group in __get_notebook_groups():
expected_content += f"\n --group {group} \\"
expected_content += dedent(R"""
> requirements.txt
uv pip install \
--requirement requirements.txt \
--system
rm requirements.txt
uv cache clean
""")
return expected_content


def __get_notebook_extras() -> list[str]:
def __get_notebook_groups() -> list[str]:
dependency_groups = ___safe_get_table("dependency-groups")
allowed_groups = {"jupyter", "notebooks"}
return sorted(allowed_groups & set(dependency_groups))


def ___safe_get_table(dotted_header: str) -> Mapping[str, Any]:
if not CONFIG_PATH.pyproject.exists():
return []
return {}
pyproject = Pyproject.load()
table_key = "project.optional-dependencies"
if not pyproject.has_table(table_key):
return []
optional_dependencies = pyproject.get_table(table_key)
allowed_sections = {"jupyter", "notebooks"}
return sorted(allowed_sections & set(optional_dependencies))
if not pyproject.has_table(dotted_header):
return {}
return pyproject.get_table(dotted_header)


def _make_executable(path: Path) -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/compwa_policy/check_dev_files/jupyter.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ def _update_dev_requirements(no_ruff: bool) -> None:
}
packages.update(ruff_packages)
for package in sorted(packages):
pyproject.add_dependency(package, optional_key=["jupyter", "dev"])
pyproject.add_dependency(package, dependency_group=["jupyter", "dev"])
Loading

0 comments on commit 4e7f635

Please sign in to comment.