Replace aspy.yaml with sort_keys=False · precommit/pre-commit@a64fa6d · GitHub
Skip to content

Commit a64fa6d

Browse files
committed
Replace aspy.yaml with sort_keys=False
1 parent f0ee93c commit a64fa6d

8 files changed

Lines changed: 36 additions & 26 deletions

File tree

pre_commit/clientlib.py

Lines changed: 3 additions & 3 deletions

pre_commit/commands/autoupdate.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
from typing import Sequence
99
from typing import Tuple
1010

11-
from aspy.yaml import ordered_dump
12-
from aspy.yaml import ordered_load
13-
1411
import pre_commit.constants as C
1512
from pre_commit import git
1613
from pre_commit import output
@@ -25,6 +22,8 @@
2522
from pre_commit.util import cmd_output
2623
from pre_commit.util import cmd_output_b
2724
from pre_commit.util import tmpdir
25+
from pre_commit.util import yaml_dump
26+
from pre_commit.util import yaml_load
2827

2928

3029
class RevInfo(NamedTuple):
@@ -105,7 +104,7 @@ def _original_lines(
105104
raise AssertionError('could not find rev lines')
106105
else:
107106
with open(path, 'w') as f:
108-
f.write(ordered_dump(ordered_load(original), **C.YAML_DUMP_KWARGS))
107+
f.write(yaml_dump(yaml_load(original)))
109108
return _original_lines(path, rev_infos, retry=True)
110109

111110

@@ -117,7 +116,7 @@ def _write_new_config(path: str, rev_infos: List[Optional[RevInfo]]) -> None:
117116
continue
118117
match = REV_LINE_RE.match(lines[idx])
119118
assert match is not None
120-
new_rev_s = ordered_dump({'rev': rev_info.rev}, **C.YAML_DUMP_KWARGS)
119+
new_rev_s = yaml_dump({'rev': rev_info.rev})
121120
new_rev = new_rev_s.split(':', 1)[1].strip()
122121
if rev_info.frozen is not None:
123122
comment = f' # frozen: {rev_info.frozen}'

pre_commit/commands/migrate_config.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import re
22

33
import yaml
4-
from aspy.yaml import ordered_load
4+
5+
from pre_commit.util import yaml_load
56

67

78
def _indent(s: str) -> str:
@@ -24,12 +25,12 @@ def _migrate_map(contents: str) -> str:
2425
header = ''.join(lines[:i])
2526
rest = ''.join(lines[i:])
2627

27-
if isinstance(ordered_load(contents), list):
28+
if isinstance(yaml_load(contents), list):
2829
# If they are using the "default" flow style of yaml, this operation
2930
# will yield a valid configuration
3031
try:
3132
trial_contents = f'{header}repos:\n{rest}'
32-
ordered_load(trial_contents)
33+
yaml_load(trial_contents)
3334
contents = trial_contents
3435
except yaml.YAMLError:
3536
contents = f'{header}repos:\n{_indent(rest)}'

pre_commit/commands/try_repo.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
from typing import Optional
55
from typing import Tuple
66

7-
from aspy.yaml import ordered_dump
8-
97
import pre_commit.constants as C
108
from pre_commit import git
119
from pre_commit import output
@@ -14,6 +12,7 @@
1412
from pre_commit.store import Store
1513
from pre_commit.util import cmd_output_b
1614
from pre_commit.util import tmpdir
15+
from pre_commit.util import yaml_dump
1716
from pre_commit.xargs import xargs
1817

1918
logger = logging.getLogger(__name__)
@@ -63,7 +62,7 @@ def try_repo(args: argparse.Namespace) -> int:
6362
hooks = [{'id': hook['id']} for hook in manifest]
6463

6564
config = {'repos': [{'repo': repo, 'rev': ref, 'hooks': hooks}]}
66-
config_s = ordered_dump(config, **C.YAML_DUMP_KWARGS)
65+
config_s = yaml_dump(config)
6766

6867
config_filename = os.path.join(tempdir, C.CONFIG_FILE)
6968
with open(config_filename, 'w') as cfg:

pre_commit/constants.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
CONFIG_FILE = '.pre-commit-config.yaml'
99
MANIFEST_FILE = '.pre-commit-hooks.yaml'
1010

11-
YAML_DUMP_KWARGS = {'default_flow_style': False, 'indent': 4}
12-
1311
# Bump when installation changes in a backwards / forwards incompatible way
1412
INSTALLED_STATE_VERSION = '1'
1513
# Bump when modifying `empty_template`

pre_commit/util.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import contextlib
22
import errno
3+
import functools
34
import os.path
45
import shutil
56
import stat
@@ -17,6 +18,8 @@
1718
from typing import Type
1819
from typing import Union
1920

21+
import yaml
22+
2023
from pre_commit import parse_shebang
2124

2225
if sys.version_info >= (3, 7): # pragma: no cover (PY37+)
@@ -28,6 +31,17 @@
2831

2932
EnvironT = Union[Dict[str, str], 'os._Environ']
3033

34+
Loader = getattr(yaml, 'CSafeLoader', yaml.SafeLoader)
35+
yaml_load = functools.partial(yaml.load, Loader=Loader)
36+
Dumper = getattr(yaml, 'CSafeDumper', yaml.SafeDumper)
37+
38+
39+
def yaml_dump(o: Any) -> str:
40+
# when python/mypy#1484 is solved, this can be `functools.partial`
41+
return yaml.dump(
42+
o, Dumper=Dumper, default_flow_style=False, indent=4, sort_keys=False,
43+
)
44+
3145

3246
@contextlib.contextmanager
3347
def clean_path_on_failure(path: str) -> Generator[None, None, None]:

setup.cfg

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ classifiers =
2222
[options]
2323
packages = find:
2424
install_requires =
25-
aspy.yaml
2625
cfgv>=2.0.0
2726
identify>=1.0.0
2827
nodeenv>=0.11.1
29-
pyyaml
28+
pyyaml>=5.1
3029
toml
3130
virtualenv>=15.2
3231
importlib-metadata;python_version<"3.8"

testing/fixtures.py

Lines changed: 8 additions & 8 deletions

0 commit comments

Comments
 (0)