Skip to content

Commit

Permalink
FIX: import existing Ruff extend-ignores from nbQA (#225)
Browse files Browse the repository at this point in the history
* DX: add E703 and TCH00 to Ruff notebook ignores
* ENH: improve update message of commitlint sub-hook
* MAINT: explain default ignore codes
  • Loading branch information
redeboer authored Nov 28, 2023
1 parent 9bb7021 commit f6fb9b3
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
6 changes: 5 additions & 1 deletion src/repoma/check_dev_files/commitlint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"""

import os
from textwrap import dedent

from repoma.errors import PrecommitError

Expand All @@ -13,5 +14,8 @@ def main() -> None:
if not os.path.exists(path):
return
os.remove(path)
msg = f"Remove outdated {path}"
msg = dedent(f"""
Remove outdated {path}. Commitlint is now configured through
https://github.com/ComPWA/commitlint-config.
""").strip().replace("\n", " ")
raise PrecommitError(msg)
55 changes: 41 additions & 14 deletions src/repoma/check_dev_files/ruff.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from ruamel.yaml import YAML
from ruamel.yaml.comments import CommentedMap
from tomlkit.items import Array, Table
from tomlkit.toml_document import TOMLDocument

from repoma.check_dev_files.setup_cfg import (
has_pyproject_build_system,
Expand Down Expand Up @@ -50,11 +51,9 @@ def main(has_notebooks: bool) -> None:
executor(_check_setup_cfg)
executor(_remove_flake8)
executor(_remove_isort)
executor(_remove_nbqa)
executor(_remove_pydocstyle)
executor(_remove_pylint)
executor(_update_ruff_settings, has_notebooks)
executor(_update_ruff_per_file_ignores, has_notebooks)
executor(_update_ruff_pydocstyle_settings)
executor(_update_precommit_hook, has_notebooks)
executor(_update_pyproject)
Expand Down Expand Up @@ -289,6 +288,14 @@ def __remove_nbqa_settings() -> None:


def _update_ruff_settings(has_notebooks: bool) -> None:
executor = Executor()
executor(__update_ruff_settings, has_notebooks)
executor(_update_ruff_per_file_ignores, has_notebooks)
executor(_remove_nbqa)
executor.finalize()


def __update_ruff_settings(has_notebooks: bool) -> None:
pyproject = load_pyproject()
settings = get_sub_table(pyproject, "tool.ruff", create=True)
extend_ignore = [
Expand Down Expand Up @@ -335,6 +342,37 @@ def _update_ruff_settings(has_notebooks: bool) -> None:
raise PrecommitError(msg)


def __get_ipynb_ignores(pyproject: TOMLDocument, per_file_settings: Table) -> Array:
notebook_ignores = {
"B018", # useless-expression
"C90", # complex-structure
"D", # pydocstyle
"E703", # useless-semicolon
"N806", # non-lowercase-variable-in-function
"N816", # mixed-case-variable-in-global-scope
"PLR09", # complicated logic
"PLR2004", # magic-value-comparison
"PLW0602", # global-variable-not-assigned
"PLW0603", # global-statement
"TCH00", # type-checking block
}
notebook_ignores.update(__get_existing_nbqa_ignores(pyproject))
notebook_ignores.update(per_file_settings.get("*.ipynb", []))
return to_toml_array(sorted(notebook_ignores))


def __get_existing_nbqa_ignores(pyproject: TOMLDocument) -> Set[str]:
nbqa_table = get_sub_table(pyproject, "tool.nbqa.addopts", create=True)
if not nbqa_table:
return set()
ruff_rules: List[str] = nbqa_table.get("ruff", [])
return {
r.replace("--extend-ignore=", "")
for r in ruff_rules
if r.startswith("--extend-ignore=")
}


def __get_selected_ruff_rules() -> Array:
rules = {
"A",
Expand Down Expand Up @@ -412,18 +450,7 @@ def _update_ruff_per_file_ignores(has_notebooks: bool) -> None:
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)
minimal_settings["*.ipynb"] = __get_ipynb_ignores(pyproject, settings)
docs_dir = "docs"
if os.path.exists(docs_dir) and os.path.isdir(docs_dir):
key = f"{docs_dir}/*"
Expand Down

0 comments on commit f6fb9b3

Please sign in to comment.