From d218c9721201ec7ef5ad16688a49ca68031f712e Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Mon, 3 Jul 2023 10:59:11 +0200 Subject: [PATCH] FEAT: update `tool.nbqa.addopts` settings (#147) * BEHAVIOR: enforce single line options for single values * ENH: run `pyright` before `pytest`conda activate repoma * FEAT: add `tool.nbqa.addopts` `ruff` setting * FEAT: remove `tool.nbqa.addopts` `flake8` option * FEAT: remove `tool.nbqa.addopts` `isort` option --- src/repoma/check_dev_files/__init__.py | 2 +- src/repoma/check_dev_files/black.py | 29 ++--------------- src/repoma/check_dev_files/deprecated.py | 41 +++++++++++++++++------- src/repoma/check_dev_files/ruff.py | 2 ++ src/repoma/utilities/pyproject.py | 27 ++++++++++++++++ 5 files changed, 62 insertions(+), 39 deletions(-) diff --git a/src/repoma/check_dev_files/__init__.py b/src/repoma/check_dev_files/__init__.py index e560bdf7..529949ee 100644 --- a/src/repoma/check_dev_files/__init__.py +++ b/src/repoma/check_dev_files/__init__.py @@ -174,8 +174,8 @@ def main(argv: Optional[Sequence[str]] = None) -> int: cron_frequency=args.pin_requirements, ) executor(mypy.main) - executor(pytest.main) executor(pyright.main) + executor(pytest.main) executor(pyupgrade.main) executor(ruff.main) executor(setup_cfg.main, args.ignore_author) diff --git a/src/repoma/check_dev_files/black.py b/src/repoma/check_dev_files/black.py index bd05ccb9..fec49de3 100644 --- a/src/repoma/check_dev_files/black.py +++ b/src/repoma/check_dev_files/black.py @@ -6,8 +6,6 @@ from repoma.utilities import CONFIG_PATH from repoma.utilities.executor import Executor from repoma.utilities.precommit import ( - find_repo, - load_round_trip_precommit_config, update_precommit_hook, update_single_hook_precommit_repo, ) @@ -16,6 +14,7 @@ get_sub_table, load_pyproject, to_toml_array, + update_nbqa_settings, write_pyproject, ) from repoma.utilities.setup_cfg import get_supported_python_versions @@ -27,9 +26,9 @@ def main() -> None: executor = Executor() executor(_remove_outdated_settings) executor(_update_black_settings) - executor(_update_nbqa_settings) executor(_update_precommit_repo) executor(_update_precommit_nbqa_hook) + executor(update_nbqa_settings, "black", to_toml_array(["--line-length=85"])) executor.finalize() @@ -83,27 +82,3 @@ def _update_precommit_nbqa_hook() -> None: additional_dependencies=["black>=22.1.0"], ), ) - - -def _update_nbqa_settings() -> None: - # cspell:ignore addopts - if not CONFIG_PATH.precommit.exists(): - return - if not __has_nbqa_precommit_repo(): - return - pyproject = load_pyproject() - nbqa_table = get_sub_table(pyproject, "tool.nbqa.addopts", create=True) - expected = ["--line-length=85"] - if nbqa_table.get("black") != expected: - nbqa_table["black"] = expected - write_pyproject(pyproject) - msg = "Added nbQA configuration for black" - raise PrecommitError(msg) - - -def __has_nbqa_precommit_repo() -> bool: - config, _ = load_round_trip_precommit_config() - nbqa_repo = find_repo(config, "https://github.com/nbQA-dev/nbQA") - if nbqa_repo is None: - return False - return True diff --git a/src/repoma/check_dev_files/deprecated.py b/src/repoma/check_dev_files/deprecated.py index fb9a4736..3146e406 100644 --- a/src/repoma/check_dev_files/deprecated.py +++ b/src/repoma/check_dev_files/deprecated.py @@ -1,6 +1,6 @@ """Remove deprecated linters and formatters.""" import os -from typing import List +from typing import TYPE_CHECKING, List from repoma.errors import PrecommitError from repoma.utilities import CONFIG_PATH @@ -15,6 +15,9 @@ set_setting, ) +if TYPE_CHECKING: + from tomlkit.items import Table + def remove_deprecated_tools() -> None: executor = Executor() @@ -28,6 +31,7 @@ def remove_deprecated_tools() -> None: def _remove_flake8() -> None: executor = Executor() executor(__remove_configs, [".flake8"]) + executor(__remove_nbqa_option, "flake8") executor(__uninstall, "flake8", check_options=["lint", "sty"]) executor(__uninstall, "pep8-naming", check_options=["lint", "sty"]) executor(remove_extension_recommendation, "ms-python.flake8", unwanted=True) @@ -42,6 +46,7 @@ def _remove_flake8() -> None: def _remove_isort() -> None: executor = Executor() executor(__remove_isort_settings) + executor(__remove_nbqa_option, "isort") executor(remove_extension_recommendation, "ms-python.isort", unwanted=True) executor(remove_precommit_hook, "isort") executor(remove_precommit_hook, "nbqa-isort") @@ -50,6 +55,30 @@ def _remove_isort() -> None: executor.finalize() +def __remove_isort_settings() -> None: + pyproject = load_pyproject() + if pyproject.get("tool", {}).get("isort") is None: + return + pyproject["tool"].remove("isort") # type: ignore[union-attr] + write_pyproject(pyproject) + msg = f"Removed [tool.isort] section from {CONFIG_PATH.pyproject}" + raise PrecommitError(msg) + + +def __remove_nbqa_option(option: str) -> None: + pyproject = load_pyproject() + # cspell:ignore addopts + nbqa_table: Table = pyproject.get("tool", {}).get("nbqa", {}).get("addopts") + if nbqa_table is None: + return + if nbqa_table.get(option) is None: + return + nbqa_table.remove(option) + write_pyproject(pyproject) + msg = f"Removed {option!r} nbQA options from {CONFIG_PATH.pyproject}" + raise PrecommitError(msg) + + def _remove_pydocstyle() -> None: executor = Executor() executor( @@ -77,16 +106,6 @@ def _remove_pylint() -> None: executor.finalize() -def __remove_isort_settings() -> None: - pyproject = load_pyproject() - if pyproject.get("tool", {}).get("isort") is None: - return - pyproject["tool"].remove("isort") # type: ignore[union-attr] - write_pyproject(pyproject) - msg = f"Removed [tool.isort] section from {CONFIG_PATH.pyproject}" - raise PrecommitError(msg) - - def __remove_configs(paths: List[str]) -> None: executor = Executor() for path in paths: diff --git a/src/repoma/check_dev_files/ruff.py b/src/repoma/check_dev_files/ruff.py index 4758fd89..af3c200a 100644 --- a/src/repoma/check_dev_files/ruff.py +++ b/src/repoma/check_dev_files/ruff.py @@ -20,6 +20,7 @@ get_sub_table, load_pyproject, to_toml_array, + update_nbqa_settings, write_pyproject, ) from repoma.utilities.readme import add_badge @@ -40,6 +41,7 @@ def main() -> None: executor(_update_precommit_hook) executor(_update_precommit_nbqa_hook) executor(_update_vscode_settings) + executor(update_nbqa_settings, "ruff", to_toml_array(["--line-length=85"])) executor.finalize() diff --git a/src/repoma/utilities/pyproject.py b/src/repoma/utilities/pyproject.py index 99db8510..2f02e5c9 100644 --- a/src/repoma/utilities/pyproject.py +++ b/src/repoma/utilities/pyproject.py @@ -7,7 +7,9 @@ from tomlkit.items import Array, Table from tomlkit.toml_document import TOMLDocument +from repoma.errors import PrecommitError from repoma.utilities import CONFIG_PATH +from repoma.utilities.precommit import find_repo, load_round_trip_precommit_config def complies_with_subset(settings: dict, minimal_settings: dict) -> bool: @@ -48,4 +50,29 @@ def to_toml_array(items: Iterable[Any]) -> Array: array.extend(items) if len(array) > 1: array.multiline(True) + else: + array.multiline(False) return array + + +def update_nbqa_settings(key: str, expected: Any) -> None: + # cspell:ignore addopts + if not CONFIG_PATH.precommit.exists(): + return + if not __has_nbqa_precommit_repo(): + return + pyproject = load_pyproject() + nbqa_table = get_sub_table(pyproject, "tool.nbqa.addopts", create=True) + if nbqa_table.get(key) != expected: + nbqa_table[key] = expected + write_pyproject(pyproject) + msg = f"Added nbQA configuration for {key!r}" + raise PrecommitError(msg) + + +def __has_nbqa_precommit_repo() -> bool: + config, _ = load_round_trip_precommit_config() + nbqa_repo = find_repo(config, "https://github.com/nbQA-dev/nbQA") + if nbqa_repo is None: + return False + return True