Replace aspy.yaml with sort_keys=False

This commit is contained in:
Anthony Sottile
2020-01-31 17:18:59 -08:00
parent f0ee93c5a7
commit a64fa6d478
8 changed files with 36 additions and 26 deletions

View File

@@ -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}

View File

@@ -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}'

View File

@@ -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)}'

View File

@@ -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:

View File

@@ -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`

View File

@@ -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 (<PY37)
EnvironT = Union[Dict[str, str], 'os._Environ']
Loader = getattr(yaml, 'CSafeLoader', yaml.SafeLoader)
yaml_load = functools.partial(yaml.load, Loader=Loader)
Dumper = getattr(yaml, 'CSafeDumper', yaml.SafeDumper)
def yaml_dump(o: Any) -> 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]: