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

DX: replace nbqa-ruff with official Ruff formatter #222

Merged
merged 2 commits into from
Nov 27, 2023
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 .cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"conda",
"fromdict",
"indentless",
"ipynb",
"jsonschema",
"linkcheck",
"maxdepth",
Expand Down
3 changes: 1 addition & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ repos:
rev: v0.1.4
hooks:
- id: ruff
args:
- --fix
args: [--fix]

- repo: https://github.com/ComPWA/mirrors-taplo
rev: v0.8.1
Expand Down
2 changes: 1 addition & 1 deletion src/repoma/check_dev_files/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int:
executor(pytest.main)
executor(pyupgrade.main)
if not args.no_ruff:
executor(ruff.main)
executor(ruff.main, has_notebooks)
executor(setup_cfg.main, args.ignore_author)
if args.pin_requirements != "no":
executor(
Expand Down
5 changes: 3 additions & 2 deletions src/repoma/check_dev_files/black.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Update :file:`pyproject.toml` black configuration."""

from ruamel.yaml import YAML
from ruamel.yaml.comments import CommentedMap

from repoma.errors import PrecommitError
Expand Down Expand Up @@ -97,8 +98,8 @@ def _update_precommit_repo(has_notebooks: bool) -> None:
if has_notebooks:
black_jupyter = CommentedMap(
id="black-jupyter",
args=["--line-length=85"],
types_or=["jupyter"],
args=YAML(typ="rt").load("[--line-length=85]"),
types_or=YAML(typ="rt").load("[jupyter]"),
)
expected_hook["hooks"].append(black_jupyter)
update_single_hook_precommit_repo(expected_hook)
14 changes: 8 additions & 6 deletions src/repoma/check_dev_files/pyupgrade.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Install `pyupgrade <https://github.com/asottile/pyupgrade>`_ as a hook."""

from ruamel.yaml.comments import CommentedMap
from ruamel.yaml import YAML
from ruamel.yaml.comments import CommentedMap, CommentedSeq

from repoma.utilities import natural_sorting
from repoma.utilities.executor import Executor
Expand All @@ -24,7 +25,7 @@ def _update_precommit_repo() -> None:
hooks=[
CommentedMap(
id="pyupgrade",
args=[__get_pyupgrade_version_argument()],
args=__get_pyupgrade_version_argument(),
)
],
)
Expand All @@ -36,20 +37,21 @@ def _update_precommit_nbqa_hook() -> None:
repo_url="https://github.com/nbQA-dev/nbQA",
expected_hook=CommentedMap(
id="nbqa-pyupgrade",
args=[__get_pyupgrade_version_argument()],
args=__get_pyupgrade_version_argument(),
),
)


def __get_pyupgrade_version_argument() -> str:
def __get_pyupgrade_version_argument() -> CommentedSeq:
"""Get the --py3x-plus argument for pyupgrade.

>>> __get_pyupgrade_version_argument()
'--py36-plus'
['--py36-plus']
"""
supported_python_versions = sorted(
(v.replace(".", "") for v in get_supported_python_versions()),
key=natural_sorting,
)
lowest_version = supported_python_versions[0]
return f"--py{lowest_version}-plus"
yaml = YAML(typ="rt")
return yaml.load(f"[--py{lowest_version}-plus]")
95 changes: 52 additions & 43 deletions src/repoma/check_dev_files/ruff.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from textwrap import dedent
from typing import List, Set

from ruamel.yaml import YAML
from ruamel.yaml.comments import CommentedMap
from tomlkit.items import Array, Table

Expand All @@ -17,7 +18,6 @@
from repoma.utilities.executor import Executor
from repoma.utilities.precommit import (
remove_precommit_hook,
update_precommit_hook,
update_single_hook_precommit_repo,
)
from repoma.utilities.project_info import (
Expand All @@ -30,7 +30,6 @@
get_sub_table,
load_pyproject,
to_toml_array,
update_nbqa_settings,
write_pyproject,
)
from repoma.utilities.readme import add_badge, remove_badge
Expand All @@ -42,7 +41,7 @@
)


def main() -> None:
def main(has_notebooks: bool) -> None:
executor = Executor()
executor(
add_badge,
Expand All @@ -51,14 +50,13 @@ def main() -> None:
executor(_check_setup_cfg)
executor(_remove_flake8)
executor(_remove_isort)
executor(_remove_nbqa)
executor(_remove_pydocstyle)
executor(_remove_pylint)
executor(_update_nbqa_settings)
executor(_update_ruff_settings)
executor(_update_ruff_per_file_ignores)
executor(_update_ruff_settings, has_notebooks)
executor(_update_ruff_per_file_ignores, has_notebooks)
executor(_update_ruff_pydocstyle_settings)
executor(_update_precommit_hook)
executor(_update_precommit_nbqa_hook)
executor(_update_precommit_hook, has_notebooks)
executor(_update_pyproject)
executor(_update_vscode_settings)
executor.finalize()
Expand Down Expand Up @@ -268,28 +266,29 @@ def __add_package(optional_dependencies: Table, key: str, package: str) -> None:
)


def _update_nbqa_settings() -> None:
def _remove_nbqa() -> None:
executor = Executor()
executor(__remove_nbqa_settings)
executor(remove_precommit_hook, "nbqa-ruff")
executor.finalize()


def __remove_nbqa_settings() -> None:
# cspell:ignore addopts
ruff_rules = [
"--extend-ignore=B018",
"--extend-ignore=C90",
"--extend-ignore=D",
"--extend-ignore=N806",
"--extend-ignore=N816",
"--extend-ignore=PLR09",
"--extend-ignore=PLR2004",
"--extend-ignore=PLW0602",
"--extend-ignore=PLW0603",
"--line-length=85",
]
pyproject = load_pyproject()
nbqa_table = get_sub_table(pyproject, "tool.nbqa.addopts", create=True)
ruff_rules.extend(nbqa_table.get("ruff", []))
ruff_rules = sorted(set(ruff_rules))
update_nbqa_settings("ruff", to_toml_array(ruff_rules))
nbqa_addopts = get_sub_table(pyproject, "tool.nbqa.addopts", create=True)
if "ruff" in nbqa_addopts:
del nbqa_addopts["ruff"]
if not nbqa_addopts:
tool_table = get_sub_table(pyproject, "tool", create=True)
del tool_table["nbqa"]
write_pyproject(pyproject)
if nbqa_addopts:
msg = f"Removed Ruff configuration for nbQA from {CONFIG_PATH.pyproject}"
raise PrecommitError(msg)


def _update_ruff_settings() -> None:
def _update_ruff_settings(has_notebooks: bool) -> None:
pyproject = load_pyproject()
settings = get_sub_table(pyproject, "tool.ruff", create=True)
extend_ignore = [
Expand All @@ -315,6 +314,10 @@ def _update_ruff_settings() -> None:
"target-version": __get_target_version(),
"task-tags": __get_task_tags(settings),
}
if has_notebooks:
extend_include = ["*.ipynb"]
extend_include = sorted({*settings.get("extend-include", []), *extend_include})
minimal_settings["extend-include"] = to_toml_array(extend_include)
src_directories = __get_src_directories()
if src_directories:
minimal_settings["src"] = src_directories
Expand Down Expand Up @@ -404,10 +407,23 @@ def __get_target_version() -> str:
return lowest_version


def _update_ruff_per_file_ignores() -> None:
def _update_ruff_per_file_ignores(has_notebooks: bool) -> None:
pyproject = load_pyproject()
settings = get_sub_table(pyproject, "tool.ruff.per-file-ignores", create=True)
minimal_settings = {}
if has_notebooks:
notebook_ignores = [
"B018",
"C90",
"D",
"N806",
"N816",
"PLR09",
"PLR2004",
"PLW0602",
"PLW0603",
]
minimal_settings["*.ipynb"] = to_toml_array(notebook_ignores)
docs_dir = "docs"
if os.path.exists(docs_dir) and os.path.isdir(docs_dir):
key = f"{docs_dir}/*"
Expand Down Expand Up @@ -455,26 +471,19 @@ def _update_ruff_pydocstyle_settings() -> None:
raise PrecommitError(msg)


def _update_precommit_hook() -> None:
def _update_precommit_hook(has_notebooks: bool) -> None:
if not CONFIG_PATH.precommit.exists():
return
expected_hook = CommentedMap(
yaml = YAML(typ="rt")
ruff_hook = CommentedMap(id="ruff", args=yaml.load("[--fix]"))
if has_notebooks:
types = yaml.load("[python, pyi, jupyter]")
ruff_hook["types_or"] = types
expected_repo = CommentedMap(
repo="https://github.com/astral-sh/ruff-pre-commit",
hooks=[CommentedMap(id="ruff", args=["--fix"])],
)
update_single_hook_precommit_repo(expected_hook)


def _update_precommit_nbqa_hook() -> None:
if not CONFIG_PATH.precommit.exists():
return
update_precommit_hook(
repo_url="https://github.com/nbQA-dev/nbQA",
expected_hook=CommentedMap(
id="nbqa-ruff",
args=["--fix"],
),
hooks=[ruff_hook],
)
update_single_hook_precommit_repo(expected_repo)


def _update_vscode_settings() -> None:
Expand Down
3 changes: 2 additions & 1 deletion src/repoma/check_dev_files/toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import List, Union

import tomlkit
from ruamel.yaml import YAML
from ruamel.yaml.comments import CommentedMap

from repoma.errors import PrecommitError
Expand Down Expand Up @@ -73,7 +74,7 @@ def _update_tomlsort_config() -> None:
def _update_tomlsort_hook() -> None:
expected_hook = CommentedMap(
repo="https://github.com/pappasam/toml-sort",
hooks=[CommentedMap(id="toml-sort", args=["--in-place"])],
hooks=[CommentedMap(id="toml-sort", args=YAML(typ="rt").load("[--in-place]"))],
)
excludes = []
if glob("labels/*.toml"):
Expand Down