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

BREAK: type pre-commit config with TypedDict #272

Merged
merged 4 commits into from
Jan 12, 2024
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 docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"Array": "tomlkit.items.Array",
"ConfigParser": "configparser.ConfigParser",
"K": "typing.TypeVar",
"NotRequired": ("obj", "typing.NotRequired"),
"P": "typing.ParamSpec",
"P.args": ("attr", "typing.ParamSpec.args"),
"P.kwargs": ("attr", "typing.ParamSpec.kwargs"),
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ dependencies = [
"pre-commit",
"ruamel.yaml", # better YAML dumping
"tomlkit",
'typing-extensions; python_version <"3.10.0"',
'typing-extensions; python_version <"3.11.0"',
]
description = "Pre-commit hooks that ensure that ComPWA repositories have a similar developer set-up"
dynamic = ["version"]
Expand Down
14 changes: 8 additions & 6 deletions src/compwa_policy/check_dev_files/black.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""Update :file:`pyproject.toml` black configuration."""

from ruamel.yaml import YAML
from ruamel.yaml.comments import CommentedMap

from compwa_policy.errors import PrecommitError
from compwa_policy.utilities import CONFIG_PATH, vscode
from compwa_policy.utilities.executor import Executor
from compwa_policy.utilities.precommit import (
Hook,
Repo,
remove_precommit_hook,
update_single_hook_precommit_repo,
)
Expand Down Expand Up @@ -89,15 +90,16 @@ def _update_black_settings() -> None:


def _update_precommit_repo(has_notebooks: bool) -> None:
expected_hook = CommentedMap(
expected_repo = Repo(
repo="https://github.com/psf/black-pre-commit-mirror",
hooks=[CommentedMap(id="black")],
rev="",
hooks=[Hook(id="black")],
)
if has_notebooks:
black_jupyter = CommentedMap(
black_jupyter = Hook(
id="black-jupyter",
args=YAML(typ="rt").load("[--line-length=85]"),
types_or=YAML(typ="rt").load("[jupyter]"),
)
expected_hook["hooks"].append(black_jupyter)
update_single_hook_precommit_repo(expected_hook)
expected_repo["hooks"].append(black_jupyter)
update_single_hook_precommit_repo(expected_repo)
26 changes: 15 additions & 11 deletions src/compwa_policy/check_dev_files/citation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import json
import os
from textwrap import dedent
from typing import cast

from html2text import HTML2Text
from ruamel.yaml import YAML
Expand All @@ -18,8 +19,10 @@
from compwa_policy.utilities import CONFIG_PATH, vscode
from compwa_policy.utilities.executor import Executor
from compwa_policy.utilities.precommit import (
find_repo,
load_round_trip_precommit_config,
Hook,
Repo,
find_repo_with_index,
load_roundtrip_precommit_config,
update_single_hook_precommit_repo,
)

Expand Down Expand Up @@ -171,7 +174,7 @@ def add_json_schema_precommit() -> None:
if not CONFIG_PATH.citation.exists():
return
# cspell:ignore jsonschema schemafile
expected_hook = CommentedMap(
expected_hook = Hook(
id="check-jsonschema",
name="Check CITATION.cff",
args=[
Expand All @@ -181,21 +184,21 @@ def add_json_schema_precommit() -> None:
"https://citation-file-format.github.io/1.2.0/schema.json",
"CITATION.cff",
],
pass_filenames=False,
)
config, yaml = load_round_trip_precommit_config()
config, yaml = load_roundtrip_precommit_config()
repo_url = "https://github.com/python-jsonschema/check-jsonschema"
idx_and_repo = find_repo(config, repo_url)
existing_repos: CommentedSeq = config["repos"]
idx_and_repo = find_repo_with_index(config, repo_url)
existing_repos = config["repos"]
if idx_and_repo is None:
repo = CommentedMap(
repo = Repo(
repo=repo_url,
rev="",
hooks=[expected_hook],
)
update_single_hook_precommit_repo(repo)
else:
repo_idx, repo = idx_and_repo
existing_hooks: CommentedSeq = repo["hooks"]
existing_hooks = repo["hooks"]
hook_idx = None
for i, hook in enumerate(existing_hooks):
if hook == expected_hook:
Expand All @@ -206,9 +209,10 @@ def add_json_schema_precommit() -> None:
existing_hooks.append(expected_hook)
else:
existing_hooks[hook_idx] = expected_hook
existing_repos.yaml_set_comment_before_after_key(repo_idx + 1, before="\n")
repos_yaml = cast(CommentedSeq, existing_repos)
repos_yaml.yaml_set_comment_before_after_key(repo_idx + 1, before="\n")
yaml.dump(config, CONFIG_PATH.precommit)
msg = f"Updated check-jsonschema hook in {CONFIG_PATH.citation}"
msg = f"Updated check-jsonschema hook in {CONFIG_PATH.precommit}"
raise PrecommitError(msg)


Expand Down
33 changes: 18 additions & 15 deletions src/compwa_policy/check_dev_files/cspell.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
from glob import glob
from typing import TYPE_CHECKING, Any, Iterable, Sequence

from ruamel.yaml.comments import CommentedMap

from compwa_policy.errors import PrecommitError
from compwa_policy.utilities import COMPWA_POLICY_DIR, CONFIG_PATH, rename_file, vscode
from compwa_policy.utilities.executor import Executor
from compwa_policy.utilities.precommit import (
PrecommitConfig,
load_round_trip_precommit_config,
Hook,
Repo,
find_repo,
load_precommit_config,
load_roundtrip_precommit_config,
update_single_hook_precommit_repo,
)
from compwa_policy.utilities.readme import add_badge, remove_badge
Expand Down Expand Up @@ -47,9 +48,11 @@ def main(no_cspell_update: bool) -> None:
rename_file("cspell.json", str(CONFIG_PATH.cspell))
executor = Executor()
executor(_update_cspell_repo_url)
config = PrecommitConfig.load()
repo = config.find_repo(__REPO_URL)
if repo is None:
has_cspell_hook = False
if CONFIG_PATH.cspell.exists():
config = load_precommit_config()
has_cspell_hook = find_repo(config, __REPO_URL) is not None
if not has_cspell_hook:
executor(_remove_configuration)
else:
executor(_update_precommit_repo)
Expand All @@ -65,14 +68,13 @@ def _update_cspell_repo_url(path: Path = CONFIG_PATH.precommit) -> None:
old_url_patters = [
r".*/mirrors-cspell(.git)?$",
]
config = PrecommitConfig.load(path)
config, yaml = load_roundtrip_precommit_config(path)
for pattern in old_url_patters:
repo_index = config.get_repo_index(pattern)
if repo_index is None:
repo = find_repo(config, pattern)
if repo is None:
continue
config_dict, yaml_parser = load_round_trip_precommit_config(path)
config_dict["repos"][repo_index]["repo"] = __REPO_URL
yaml_parser.dump(config_dict, path)
repo["repo"] = __REPO_URL
yaml.dump(config, path)
msg = f"Updated cSpell pre-commit repo URL to {__REPO_URL} in {path}"
raise PrecommitError(msg)

Expand Down Expand Up @@ -102,9 +104,10 @@ def _remove_configuration() -> None:


def _update_precommit_repo() -> None:
expected_hook = CommentedMap(
expected_hook = Repo(
repo=__REPO_URL,
hooks=[CommentedMap(id="cspell")],
rev="",
hooks=[Hook(id="cspell")],
)
update_single_hook_precommit_repo(expected_hook)

Expand Down
12 changes: 8 additions & 4 deletions src/compwa_policy/check_dev_files/editorconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

from textwrap import dedent

from ruamel.yaml.comments import CommentedMap
from ruamel.yaml.scalarstring import FoldedScalarString

from compwa_policy.utilities import CONFIG_PATH
from compwa_policy.utilities.precommit import update_single_hook_precommit_repo
from compwa_policy.utilities.precommit import (
Hook,
Repo,
update_single_hook_precommit_repo,
)


def main(no_python: bool) -> None:
Expand All @@ -20,7 +23,7 @@ def main(no_python: bool) -> None:


def _update_precommit_config(no_python: bool) -> None:
hook = CommentedMap(
hook = Hook(
id="editorconfig-checker",
name="editorconfig",
alias="ec",
Expand All @@ -33,8 +36,9 @@ def _update_precommit_config(no_python: bool) -> None:
""").strip()
hook["exclude"] = FoldedScalarString(excludes)

expected_hook = CommentedMap(
expected_hook = Repo(
repo="https://github.com/editorconfig-checker/editorconfig-checker.python",
rev="",
hooks=[hook],
)
update_single_hook_precommit_repo(expected_hook)
18 changes: 12 additions & 6 deletions src/compwa_policy/check_dev_files/github_workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
write,
)
from compwa_policy.utilities.executor import Executor
from compwa_policy.utilities.precommit import PrecommitConfig
from compwa_policy.utilities.precommit import load_precommit_config
from compwa_policy.utilities.project_info import PythonVersion, get_pypi_name
from compwa_policy.utilities.yaml import create_prettier_round_trip_yaml

Expand Down Expand Up @@ -174,12 +174,18 @@ def __update_style_section(config: CommentedMap, python_version: PythonVersion)
config["jobs"]["style"]["with"] = {
"python-version": DoubleQuotedScalarString(python_version)
}
if not CONFIG_PATH.precommit.exists():
if __is_remove_style_job():
del config["jobs"]["style"]
else:
cfg = PrecommitConfig.load()
if cfg.ci is not None and cfg.ci.skip is None:
del config["jobs"]["style"]


def __is_remove_style_job() -> bool:
if not CONFIG_PATH.precommit.exists():
return True
config = load_precommit_config()
precommit_ci = config.get("ci")
if precommit_ci is not None and "skip" not in precommit_ci:
return True
return False


def __update_pytest_section(
Expand Down
17 changes: 9 additions & 8 deletions src/compwa_policy/check_dev_files/nbstripout.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""Check the nbstripout hook in the pre-commit config."""

from ruamel.yaml.comments import CommentedMap
from ruamel.yaml.scalarstring import LiteralScalarString

from compwa_policy.utilities import CONFIG_PATH
from compwa_policy.utilities.precommit import (
Hook,
Repo,
find_repo,
load_round_trip_precommit_config,
load_precommit_config,
update_single_hook_precommit_repo,
)

Expand All @@ -15,10 +16,9 @@ def main() -> None:
# cspell:ignore nbconvert showmarkdowntxt
if not CONFIG_PATH.precommit.exists():
return
config, _ = load_round_trip_precommit_config()
config = load_precommit_config()
repo_url = "https://github.com/kynan/nbstripout"
idx_and_repo = find_repo(config, repo_url)
if idx_and_repo is None:
if find_repo(config, repo_url) is None:
return
extra_keys_argument = [
"cell.attachments",
Expand All @@ -39,10 +39,11 @@ def main() -> None:
"metadata.varInspector",
"metadata.vscode",
]
expected_hook = CommentedMap(
expected_repo = Repo(
repo=repo_url,
rev="",
hooks=[
CommentedMap(
Hook(
id="nbstripout",
args=[
"--extra-keys",
Expand All @@ -51,4 +52,4 @@ def main() -> None:
)
],
)
update_single_hook_precommit_repo(expected_hook)
update_single_hook_precommit_repo(expected_repo)
Loading
Loading