From a64fa6d478da6a5b9a2f8fcfd69ea77413ff8569 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Fri, 31 Jan 2020 17:18:59 -0800 Subject: [PATCH] Replace aspy.yaml with sort_keys=False --- pre_commit/clientlib.py | 6 +++--- pre_commit/commands/autoupdate.py | 9 ++++----- pre_commit/commands/migrate_config.py | 7 ++++--- pre_commit/commands/try_repo.py | 5 ++--- pre_commit/constants.py | 2 -- pre_commit/util.py | 14 ++++++++++++++ setup.cfg | 3 +-- testing/fixtures.py | 16 ++++++++-------- 8 files changed, 36 insertions(+), 26 deletions(-) diff --git a/pre_commit/clientlib.py b/pre_commit/clientlib.py index 43e2c8ec..56ec0dd1 100644 --- a/pre_commit/clientlib.py +++ b/pre_commit/clientlib.py @@ -9,13 +9,13 @@ from typing import Optional from typing import Sequence import cfgv -from aspy.yaml import ordered_load from identify.identify import ALL_TAGS import pre_commit.constants as C from pre_commit.error_handler import FatalError from pre_commit.languages.all import all_languages from pre_commit.util import parse_version +from pre_commit.util import yaml_load logger = logging.getLogger('pre_commit') @@ -84,7 +84,7 @@ class InvalidManifestError(FatalError): load_manifest = functools.partial( cfgv.load_from_filename, schema=MANIFEST_SCHEMA, - load_strategy=ordered_load, + load_strategy=yaml_load, exc_tp=InvalidManifestError, ) @@ -288,7 +288,7 @@ class InvalidConfigError(FatalError): def ordered_load_normalize_legacy_config(contents: str) -> Dict[str, Any]: - data = ordered_load(contents) + data = yaml_load(contents) if isinstance(data, list): # TODO: Once happy, issue a deprecation warning and instructions return {'repos': data} diff --git a/pre_commit/commands/autoupdate.py b/pre_commit/commands/autoupdate.py index fd98118a..5a9a9880 100644 --- a/pre_commit/commands/autoupdate.py +++ b/pre_commit/commands/autoupdate.py @@ -8,9 +8,6 @@ from typing import Optional from typing import Sequence from typing import Tuple -from aspy.yaml import ordered_dump -from aspy.yaml import ordered_load - import pre_commit.constants as C from pre_commit import git from pre_commit import output @@ -25,6 +22,8 @@ from pre_commit.util import CalledProcessError from pre_commit.util import cmd_output from pre_commit.util import cmd_output_b from pre_commit.util import tmpdir +from pre_commit.util import yaml_dump +from pre_commit.util import yaml_load class RevInfo(NamedTuple): @@ -105,7 +104,7 @@ def _original_lines( raise AssertionError('could not find rev lines') else: with open(path, 'w') as f: - f.write(ordered_dump(ordered_load(original), **C.YAML_DUMP_KWARGS)) + f.write(yaml_dump(yaml_load(original))) return _original_lines(path, rev_infos, retry=True) @@ -117,7 +116,7 @@ def _write_new_config(path: str, rev_infos: List[Optional[RevInfo]]) -> None: continue match = REV_LINE_RE.match(lines[idx]) assert match is not None - new_rev_s = ordered_dump({'rev': rev_info.rev}, **C.YAML_DUMP_KWARGS) + new_rev_s = yaml_dump({'rev': rev_info.rev}) new_rev = new_rev_s.split(':', 1)[1].strip() if rev_info.frozen is not None: comment = f' # frozen: {rev_info.frozen}' diff --git a/pre_commit/commands/migrate_config.py b/pre_commit/commands/migrate_config.py index 5b90b6f6..d83b8e9c 100644 --- a/pre_commit/commands/migrate_config.py +++ b/pre_commit/commands/migrate_config.py @@ -1,7 +1,8 @@ import re import yaml -from aspy.yaml import ordered_load + +from pre_commit.util import yaml_load def _indent(s: str) -> str: @@ -24,12 +25,12 @@ def _migrate_map(contents: str) -> str: header = ''.join(lines[:i]) rest = ''.join(lines[i:]) - if isinstance(ordered_load(contents), list): + if isinstance(yaml_load(contents), list): # If they are using the "default" flow style of yaml, this operation # will yield a valid configuration try: trial_contents = f'{header}repos:\n{rest}' - ordered_load(trial_contents) + yaml_load(trial_contents) contents = trial_contents except yaml.YAMLError: contents = f'{header}repos:\n{_indent(rest)}' diff --git a/pre_commit/commands/try_repo.py b/pre_commit/commands/try_repo.py index 989a0c12..4aee209c 100644 --- a/pre_commit/commands/try_repo.py +++ b/pre_commit/commands/try_repo.py @@ -4,8 +4,6 @@ import os.path from typing import Optional from typing import Tuple -from aspy.yaml import ordered_dump - import pre_commit.constants as C from pre_commit import git from pre_commit import output @@ -14,6 +12,7 @@ from pre_commit.commands.run import run from pre_commit.store import Store from pre_commit.util import cmd_output_b from pre_commit.util import tmpdir +from pre_commit.util import yaml_dump from pre_commit.xargs import xargs logger = logging.getLogger(__name__) @@ -63,7 +62,7 @@ def try_repo(args: argparse.Namespace) -> int: hooks = [{'id': hook['id']} for hook in manifest] config = {'repos': [{'repo': repo, 'rev': ref, 'hooks': hooks}]} - config_s = ordered_dump(config, **C.YAML_DUMP_KWARGS) + config_s = yaml_dump(config) config_filename = os.path.join(tempdir, C.CONFIG_FILE) with open(config_filename, 'w') as cfg: diff --git a/pre_commit/constants.py b/pre_commit/constants.py index 0fc740b2..23622ecb 100644 --- a/pre_commit/constants.py +++ b/pre_commit/constants.py @@ -8,8 +8,6 @@ else: # pragma: no cover (PY38+) CONFIG_FILE = '.pre-commit-config.yaml' MANIFEST_FILE = '.pre-commit-hooks.yaml' -YAML_DUMP_KWARGS = {'default_flow_style': False, 'indent': 4} - # Bump when installation changes in a backwards / forwards incompatible way INSTALLED_STATE_VERSION = '1' # Bump when modifying `empty_template` diff --git a/pre_commit/util.py b/pre_commit/util.py index dfe07ea9..65775710 100644 --- a/pre_commit/util.py +++ b/pre_commit/util.py @@ -1,5 +1,6 @@ import contextlib import errno +import functools import os.path import shutil import stat @@ -17,6 +18,8 @@ from typing import Tuple from typing import Type from typing import Union +import yaml + from pre_commit import parse_shebang if sys.version_info >= (3, 7): # pragma: no cover (PY37+) @@ -28,6 +31,17 @@ else: # pragma: no cover ( str: + # when python/mypy#1484 is solved, this can be `functools.partial` + return yaml.dump( + o, Dumper=Dumper, default_flow_style=False, indent=4, sort_keys=False, + ) + @contextlib.contextmanager def clean_path_on_failure(path: str) -> Generator[None, None, None]: diff --git a/setup.cfg b/setup.cfg index 4e42ddc4..bf5c01c3 100644 --- a/setup.cfg +++ b/setup.cfg @@ -22,11 +22,10 @@ classifiers = [options] packages = find: install_requires = - aspy.yaml cfgv>=2.0.0 identify>=1.0.0 nodeenv>=0.11.1 - pyyaml + pyyaml>=5.1 toml virtualenv>=15.2 importlib-metadata;python_version<"3.8" diff --git a/testing/fixtures.py b/testing/fixtures.py index a9f54a22..f7def081 100644 --- a/testing/fixtures.py +++ b/testing/fixtures.py @@ -2,8 +2,6 @@ import contextlib import os.path import shutil -from aspy.yaml import ordered_dump -from aspy.yaml import ordered_load from cfgv import apply_defaults from cfgv import validate @@ -12,6 +10,8 @@ from pre_commit import git from pre_commit.clientlib import CONFIG_SCHEMA from pre_commit.clientlib import load_manifest from pre_commit.util import cmd_output +from pre_commit.util import yaml_dump +from pre_commit.util import yaml_load from testing.util import get_resource_path from testing.util import git_commit @@ -55,10 +55,10 @@ def modify_manifest(path, commit=True): """ manifest_path = os.path.join(path, C.MANIFEST_FILE) with open(manifest_path) as f: - manifest = ordered_load(f.read()) + manifest = yaml_load(f.read()) yield manifest with open(manifest_path, 'w') as manifest_file: - manifest_file.write(ordered_dump(manifest, **C.YAML_DUMP_KWARGS)) + manifest_file.write(yaml_dump(manifest)) if commit: git_commit(msg=modify_manifest.__name__, cwd=path) @@ -70,10 +70,10 @@ def modify_config(path='.', commit=True): """ config_path = os.path.join(path, C.CONFIG_FILE) with open(config_path) as f: - config = ordered_load(f.read()) + config = yaml_load(f.read()) yield config with open(config_path, 'w', encoding='UTF-8') as config_file: - config_file.write(ordered_dump(config, **C.YAML_DUMP_KWARGS)) + config_file.write(yaml_dump(config)) if commit: git_commit(msg=modify_config.__name__, cwd=path) @@ -114,7 +114,7 @@ def make_config_from_repo(repo_path, rev=None, hooks=None, check=True): def read_config(directory, config_file=C.CONFIG_FILE): config_path = os.path.join(directory, config_file) with open(config_path) as f: - config = ordered_load(f.read()) + config = yaml_load(f.read()) return config @@ -123,7 +123,7 @@ def write_config(directory, config, config_file=C.CONFIG_FILE): assert isinstance(config, dict), config config = {'repos': [config]} with open(os.path.join(directory, config_file), 'w') as outfile: - outfile.write(ordered_dump(config, **C.YAML_DUMP_KWARGS)) + outfile.write(yaml_dump(config)) def add_config_to_repo(git_path, config, config_file=C.CONFIG_FILE):