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

FIX: import existing Ruff extend-ignores from nbQA #225

Merged
merged 4 commits into from
Nov 28, 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
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
Loading