Skip to content

Commit a64fa6d

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

File tree

8 files changed

+36
-26
lines changed

8 files changed

+36
-26
lines changed

pre_commit/clientlib.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
from typing import Sequence
1010

1111
import cfgv
12-
from aspy.yaml import ordered_load
1312
from identify.identify import ALL_TAGS
1413

1514
import pre_commit.constants as C
1615
from pre_commit.error_handler import FatalError
1716
from pre_commit.languages.all import all_languages
1817
from pre_commit.util import parse_version
18+
from pre_commit.util import yaml_load
1919

2020
logger = logging.getLogger('pre_commit')
2121

@@ -84,7 +84,7 @@ class InvalidManifestError(FatalError):
8484
load_manifest = functools.partial(
8585
cfgv.load_from_filename,
8686
schema=MANIFEST_SCHEMA,
87-
load_strategy=ordered_load,
87+
load_strategy=yaml_load,
8888
exc_tp=InvalidManifestError,
8989
)
9090

@@ -288,7 +288,7 @@ class InvalidConfigError(FatalError):
288288

289289

290290
def ordered_load_normalize_legacy_config(contents: str) -> Dict[str, Any]:
291-
data = ordered_load(contents)
291+
data = yaml_load(contents)
292292
if isinstance(data, list):
293293
# TODO: Once happy, issue a deprecation warning and instructions
294294
return {'repos': data}

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
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
import os.path
33
import shutil
44

5-
from aspy.yaml import ordered_dump
6-
from aspy.yaml import ordered_load
75
from cfgv import apply_defaults
86
from cfgv import validate
97

@@ -12,6 +10,8 @@
1210
from pre_commit.clientlib import CONFIG_SCHEMA
1311
from pre_commit.clientlib import load_manifest
1412
from pre_commit.util import cmd_output
13+
from pre_commit.util import yaml_dump
14+
from pre_commit.util import yaml_load
1515
from testing.util import get_resource_path
1616
from testing.util import git_commit
1717

@@ -55,10 +55,10 @@ def modify_manifest(path, commit=True):
5555
"""
5656
manifest_path = os.path.join(path, C.MANIFEST_FILE)
5757
with open(manifest_path) as f:
58-
manifest = ordered_load(f.read())
58+
manifest = yaml_load(f.read())
5959
yield manifest
6060
with open(manifest_path, 'w') as manifest_file:
61-
manifest_file.write(ordered_dump(manifest, **C.YAML_DUMP_KWARGS))
61+
manifest_file.write(yaml_dump(manifest))
6262
if commit:
6363
git_commit(msg=modify_manifest.__name__, cwd=path)
6464

@@ -70,10 +70,10 @@ def modify_config(path='.', commit=True):
7070
"""
7171
config_path = os.path.join(path, C.CONFIG_FILE)
7272
with open(config_path) as f:
73-
config = ordered_load(f.read())
73+
config = yaml_load(f.read())
7474
yield config
7575
with open(config_path, 'w', encoding='UTF-8') as config_file:
76-
config_file.write(ordered_dump(config, **C.YAML_DUMP_KWARGS))
76+
config_file.write(yaml_dump(config))
7777
if commit:
7878
git_commit(msg=modify_config.__name__, cwd=path)
7979

@@ -114,7 +114,7 @@ def make_config_from_repo(repo_path, rev=None, hooks=None, check=True):
114114
def read_config(directory, config_file=C.CONFIG_FILE):
115115
config_path = os.path.join(directory, config_file)
116116
with open(config_path) as f:
117-
config = ordered_load(f.read())
117+
config = yaml_load(f.read())
118118
return config
119119

120120

@@ -123,7 +123,7 @@ def write_config(directory, config, config_file=C.CONFIG_FILE):
123123
assert isinstance(config, dict), config
124124
config = {'repos': [config]}
125125
with open(os.path.join(directory, config_file), 'w') as outfile:
126-
outfile.write(ordered_dump(config, **C.YAML_DUMP_KWARGS))
126+
outfile.write(yaml_dump(config))
127127

128128

129129
def add_config_to_repo(git_path, config, config_file=C.CONFIG_FILE):

0 commit comments

Comments
 (0)