From f15c0a7fcb4455c4661d5bdfb365c9a1419048ef Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Wed, 27 Sep 2023 13:18:13 +0200 Subject: [PATCH 1/3] DX: switch to `black-jupyter` hook --- .pre-commit-config.yaml | 1 + src/repoma/check_dev_files/__init__.py | 8 +++++++- src/repoma/check_dev_files/black.py | 25 ++++++++----------------- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 73e55760..18ef9e36 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -44,6 +44,7 @@ repos: pass_filenames: false args: - --allow-labels + - --no-notebooks - --no-pypi - --repo-name=repo-maintenance - --repo-title=ComPWA Repository Maintenance diff --git a/src/repoma/check_dev_files/__init__.py b/src/repoma/check_dev_files/__init__.py index 93911f89..da71fe2a 100644 --- a/src/repoma/check_dev_files/__init__.py +++ b/src/repoma/check_dev_files/__init__.py @@ -101,6 +101,12 @@ def main(argv: Optional[Sequence[str]] = None) -> int: default=False, help="Do not run test job on macOS", ) + parser.add_argument( + "--no-notebooks", + action="store_true", + default=False, + help="This repository does not contain Jupyter notebooks", + ) parser.add_argument( "--no-pypi", action="store_true", @@ -175,7 +181,7 @@ def main(argv: Optional[Sequence[str]] = None) -> int: executor(toml.main) # has to run before pre-commit executor(prettier.main, args.no_prettierrc) if is_python_repo: - executor(black.main) + executor(black.main, not args.no_notebooks) executor(release_drafter.main, args.repo_name, args.repo_title) if args.pin_requirements != "no": executor( diff --git a/src/repoma/check_dev_files/black.py b/src/repoma/check_dev_files/black.py index cb593616..f0c535a7 100644 --- a/src/repoma/check_dev_files/black.py +++ b/src/repoma/check_dev_files/black.py @@ -6,7 +6,7 @@ from repoma.utilities import CONFIG_PATH from repoma.utilities.executor import Executor from repoma.utilities.precommit import ( - update_precommit_hook, + remove_precommit_hook, update_single_hook_precommit_repo, ) from repoma.utilities.pyproject import ( @@ -14,7 +14,6 @@ get_sub_table, load_pyproject, to_toml_array, - update_nbqa_settings, write_pyproject, ) from repoma.utilities.setup_cfg import get_supported_python_versions @@ -25,14 +24,13 @@ ) -def main() -> None: +def main(has_notebooks: bool) -> None: if not CONFIG_PATH.pyproject.exists(): return executor = Executor() executor(_remove_outdated_settings) executor(_update_black_settings) - executor(_update_precommit_repo) - executor(_update_precommit_nbqa_hook) + executor(_update_precommit_repo, has_notebooks) executor(add_extension_recommendation, "ms-python.black-formatter") executor(set_setting, {"black-formatter.importStrategy": "fromEnvironment"}) executor( @@ -40,7 +38,7 @@ def main() -> None: "[python]", {"editor.defaultFormatter": "ms-python.black-formatter"}, ) - executor(update_nbqa_settings, "black", to_toml_array(["--line-length=85"])) + executor(remove_precommit_hook, "nbqa-black") executor.finalize() @@ -78,19 +76,12 @@ def _update_black_settings() -> None: raise PrecommitError(msg) -def _update_precommit_repo() -> None: +def _update_precommit_repo(has_notebooks: bool) -> None: expected_hook = CommentedMap( repo="https://github.com/psf/black", hooks=[CommentedMap(id="black")], ) + if has_notebooks: + black_jupyter = CommentedMap(id="black-jupyter", args=["--line-length=85"]) + expected_hook["hooks"].append(black_jupyter) update_single_hook_precommit_repo(expected_hook) - - -def _update_precommit_nbqa_hook() -> None: - update_precommit_hook( - repo_url="https://github.com/nbQA-dev/nbQA", - expected_hook=CommentedMap( - id="nbqa-black", - additional_dependencies=["black>=22.1.0"], - ), - ) From f381e33bdff269ff1a181941cbd202fc80e73728 Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Wed, 27 Sep 2023 14:01:57 +0200 Subject: [PATCH 2/3] FIX: run `black-jupyter` on Jupyter notbeooks only --- src/repoma/check_dev_files/black.py | 6 +++++- src/repoma/utilities/precommit.py | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/repoma/check_dev_files/black.py b/src/repoma/check_dev_files/black.py index f0c535a7..8cefe245 100644 --- a/src/repoma/check_dev_files/black.py +++ b/src/repoma/check_dev_files/black.py @@ -82,6 +82,10 @@ def _update_precommit_repo(has_notebooks: bool) -> None: hooks=[CommentedMap(id="black")], ) if has_notebooks: - black_jupyter = CommentedMap(id="black-jupyter", args=["--line-length=85"]) + black_jupyter = CommentedMap( + id="black-jupyter", + args=["--line-length=85"], + types_or=["jupyter"], + ) expected_hook["hooks"].append(black_jupyter) update_single_hook_precommit_repo(expected_hook) diff --git a/src/repoma/utilities/precommit.py b/src/repoma/utilities/precommit.py index 67012434..3d4cbe3f 100644 --- a/src/repoma/utilities/precommit.py +++ b/src/repoma/utilities/precommit.py @@ -217,6 +217,7 @@ class Hook: language: Optional[str] = None always_run: Optional[bool] = None pass_filenames: Optional[bool] = None + types_or: Optional[List[str]] = None @define From 3745bacb2441304cdcbb1bea5fa29b58bc0f536a Mon Sep 17 00:00:00 2001 From: Remco de Boer <29308176+redeboer@users.noreply.github.com> Date: Wed, 27 Sep 2023 14:10:54 +0200 Subject: [PATCH 3/3] MAINT: remove outdated `black` option for nbQA --- src/repoma/check_dev_files/deprecated.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/repoma/check_dev_files/deprecated.py b/src/repoma/check_dev_files/deprecated.py index d73b60eb..f36dac3c 100644 --- a/src/repoma/check_dev_files/deprecated.py +++ b/src/repoma/check_dev_files/deprecated.py @@ -44,6 +44,7 @@ def _remove_flake8() -> None: def _remove_isort() -> None: executor = Executor() executor(__remove_isort_settings) + executor(__remove_nbqa_option, "black") executor(__remove_nbqa_option, "isort") executor(remove_extension_recommendation, "ms-python.isort", unwanted=True) executor(remove_precommit_hook, "isort")