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..8cefe245 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,16 @@ 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"], + types_or=["jupyter"], + ) + 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"], - ), - ) 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") 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