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

MAINT: extract function for YAML(typ="rt").load #428

Merged
merged 2 commits into from
Oct 15, 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
7 changes: 3 additions & 4 deletions src/compwa_policy/check_dev_files/black.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

from typing import Any

from ruamel.yaml import YAML

from compwa_policy.utilities import CONFIG_PATH, vscode
from compwa_policy.utilities.executor import Executor
from compwa_policy.utilities.precommit import ModifiablePrecommit
from compwa_policy.utilities.precommit.struct import Hook, Repo
from compwa_policy.utilities.pyproject import ModifiablePyproject, complies_with_subset
from compwa_policy.utilities.toml import to_toml_array
from compwa_policy.utilities.yaml import read_preserved_yaml


def main(precommit: ModifiablePrecommit, has_notebooks: bool) -> None:
Expand Down Expand Up @@ -89,8 +88,8 @@ def _update_precommit_repo(precommit: ModifiablePrecommit, has_notebooks: bool)
if has_notebooks:
black_jupyter = Hook(
id="black-jupyter",
args=YAML(typ="rt").load("[--line-length=85]"),
types_or=YAML(typ="rt").load("[jupyter]"),
args=read_preserved_yaml("[--line-length=85]"),
types_or=read_preserved_yaml("[jupyter]"),
)
expected_repo["hooks"].append(black_jupyter)
precommit.update_single_hook_repo(expected_repo)
5 changes: 2 additions & 3 deletions src/compwa_policy/check_dev_files/pyupgrade.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
"""Install `pyupgrade <https://github.com/asottile/pyupgrade>`_ as a hook."""

from ruamel.yaml import YAML
from ruamel.yaml.comments import CommentedSeq

from compwa_policy.utilities.executor import Executor
from compwa_policy.utilities.precommit import ModifiablePrecommit
from compwa_policy.utilities.precommit.struct import Hook, Repo
from compwa_policy.utilities.pyproject import Pyproject
from compwa_policy.utilities.yaml import read_preserved_yaml


def main(precommit: ModifiablePrecommit, no_ruff: bool) -> None:
Expand Down Expand Up @@ -51,8 +51,7 @@ def __get_pyupgrade_version_argument() -> CommentedSeq:
supported_python_versions = Pyproject.load().get_supported_python_versions()
lowest_version = supported_python_versions[0]
version_repr = lowest_version.replace(".", "")
yaml = YAML(typ="rt")
return yaml.load(f"[--py{version_repr}-plus]")
return read_preserved_yaml(f"[--py{version_repr}-plus]")


def _remove_pyupgrade(precommit: ModifiablePrecommit) -> None:
Expand Down
3 changes: 2 additions & 1 deletion src/compwa_policy/check_dev_files/ruff.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
)
from compwa_policy.utilities.readme import add_badge, remove_badge
from compwa_policy.utilities.toml import to_toml_array
from compwa_policy.utilities.yaml import read_preserved_yaml

if TYPE_CHECKING:
from collections.abc import Iterable, Mapping
Expand Down Expand Up @@ -615,7 +616,7 @@ def __add_nbqa_isort_pre_commit(precommit: ModifiablePrecommit) -> None:
expected_repo = Repo(
repo="https://github.com/nbQA-dev/nbQA",
rev="1.9.0",
hooks=[Hook(id="nbqa-isort", args=YAML(typ="rt").load("[--float-to-top]"))],
hooks=[Hook(id="nbqa-isort", args=read_preserved_yaml("[--float-to-top]"))],
)
if excludes is not None:
expected_repo["hooks"][0]["exclude"] = excludes
Expand Down
4 changes: 2 additions & 2 deletions src/compwa_policy/check_dev_files/toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from typing import TYPE_CHECKING

import tomlkit
from ruamel.yaml import YAML

from compwa_policy.errors import PrecommitError
from compwa_policy.utilities import COMPWA_POLICY_DIR, CONFIG_PATH, vscode
Expand All @@ -16,6 +15,7 @@
from compwa_policy.utilities.precommit.struct import Hook, Repo
from compwa_policy.utilities.pyproject import ModifiablePyproject
from compwa_policy.utilities.toml import to_toml_array
from compwa_policy.utilities.yaml import read_preserved_yaml

if TYPE_CHECKING:
from compwa_policy.utilities.precommit import ModifiablePrecommit
Expand Down Expand Up @@ -70,7 +70,7 @@ def _update_tomlsort_hook(precommit: ModifiablePrecommit) -> None:
expected_hook = Repo(
repo="https://github.com/pappasam/toml-sort",
rev="",
hooks=[Hook(id="toml-sort", args=YAML(typ="rt").load("[--in-place]"))],
hooks=[Hook(id="toml-sort", args=read_preserved_yaml("[--in-place]"))],
)
excludes = filter_patterns([
"**/Manifest.toml",
Expand Down
14 changes: 13 additions & 1 deletion src/compwa_policy/utilities/yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from __future__ import annotations

from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Any

import yaml
from ruamel.yaml import YAML
Expand Down Expand Up @@ -31,6 +31,18 @@ def create_prettier_round_trip_yaml() -> YAML:
return yaml_parser


def read_preserved_yaml(src: str) -> Any:
"""Get a :code:`ruamel.yaml` object from a YAML string.

>>> formatted_obj = read_preserved_yaml("[--line-length=85]")
>>> formatted_obj
['--line-length=85']
>>> type(formatted_obj)
<class 'ruamel.yaml.comments.CommentedSeq'>
"""
return YAML(typ="rt").load(src)


def write_yaml(definition: dict, output_path: Path | str) -> None:
"""Write a `dict` to disk with standardized YAML formatting."""
with open(output_path, "w") as stream:
Expand Down
Loading