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

DX: switch to black-jupyter pre-commit hook #171

Merged
merged 3 commits into from
Sep 27, 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
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ repos:
pass_filenames: false
args:
- --allow-labels
- --no-notebooks
- --no-pypi
- --repo-name=repo-maintenance
- --repo-title=ComPWA Repository Maintenance
Expand Down
8 changes: 7 additions & 1 deletion src/repoma/check_dev_files/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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(
Expand Down
29 changes: 12 additions & 17 deletions src/repoma/check_dev_files/black.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
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 (
complies_with_subset,
get_sub_table,
load_pyproject,
to_toml_array,
update_nbqa_settings,
write_pyproject,
)
from repoma.utilities.setup_cfg import get_supported_python_versions
Expand All @@ -25,22 +24,21 @@
)


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(
set_sub_setting,
"[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()


Expand Down Expand Up @@ -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"],
),
)
1 change: 1 addition & 0 deletions src/repoma/check_dev_files/deprecated.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions src/repoma/utilities/precommit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down