Skip to content

Commit 83e64e2

Browse files
authored
Merge pull request pre-commit#2673 from pre-commit/split-util
add pre_commit.yaml module
2 parents e1567b6 + 8529a0c commit 83e64e2

File tree

9 files changed

+28
-26
lines changed

9 files changed

+28
-26
lines changed

pre_commit/clientlib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import pre_commit.constants as C
1515
from pre_commit.errors import FatalError
1616
from pre_commit.languages.all import all_languages
17-
from pre_commit.util import yaml_load
17+
from pre_commit.yaml import yaml_load
1818

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

pre_commit/commands/autoupdate.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
from pre_commit.util import CalledProcessError
2121
from pre_commit.util import cmd_output
2222
from pre_commit.util import cmd_output_b
23-
from pre_commit.util import yaml_dump
24-
from pre_commit.util import yaml_load
23+
from pre_commit.yaml import yaml_dump
24+
from pre_commit.yaml import yaml_load
2525

2626

2727
class RevInfo(NamedTuple):

pre_commit/commands/migrate_config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import yaml
77

8-
from pre_commit.util import yaml_load
8+
from pre_commit.yaml import yaml_load
99

1010

1111
def _is_header_line(line: str) -> bool:

pre_commit/commands/try_repo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
from pre_commit.commands.run import run
1313
from pre_commit.store import Store
1414
from pre_commit.util import cmd_output_b
15-
from pre_commit.util import yaml_dump
1615
from pre_commit.xargs import xargs
16+
from pre_commit.yaml import yaml_dump
1717

1818
logger = logging.getLogger(__name__)
1919

pre_commit/languages/dart.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from pre_commit.languages import helpers
1616
from pre_commit.prefix import Prefix
1717
from pre_commit.util import win_exe
18-
from pre_commit.util import yaml_load
18+
from pre_commit.yaml import yaml_load
1919

2020
ENVIRONMENT_DIR = 'dartenv'
2121

pre_commit/util.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import contextlib
44
import errno
5-
import functools
65
import importlib.resources
76
import os.path
87
import shutil
@@ -15,22 +14,8 @@
1514
from typing import Generator
1615
from typing import IO
1716

18-
import yaml
19-
2017
from pre_commit import parse_shebang
2118

22-
Loader = getattr(yaml, 'CSafeLoader', yaml.SafeLoader)
23-
yaml_load = functools.partial(yaml.load, Loader=Loader)
24-
Dumper = getattr(yaml, 'CSafeDumper', yaml.SafeDumper)
25-
26-
27-
def yaml_dump(o: Any, **kwargs: Any) -> str:
28-
# when python/mypy#1484 is solved, this can be `functools.partial`
29-
return yaml.dump(
30-
o, Dumper=Dumper, default_flow_style=False, indent=4, sort_keys=False,
31-
**kwargs,
32-
)
33-
3419

3520
def force_bytes(exc: Any) -> bytes:
3621
with contextlib.suppress(TypeError):

pre_commit/yaml.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from __future__ import annotations
2+
3+
import functools
4+
from typing import Any
5+
6+
import yaml
7+
8+
Loader = getattr(yaml, 'CSafeLoader', yaml.SafeLoader)
9+
yaml_load = functools.partial(yaml.load, Loader=Loader)
10+
Dumper = getattr(yaml, 'CSafeDumper', yaml.SafeDumper)
11+
12+
13+
def yaml_dump(o: Any, **kwargs: Any) -> str:
14+
# when python/mypy#1484 is solved, this can be `functools.partial`
15+
return yaml.dump(
16+
o, Dumper=Dumper, default_flow_style=False, indent=4, sort_keys=False,
17+
**kwargs,
18+
)

testing/fixtures.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
from pre_commit.clientlib import CONFIG_SCHEMA
1313
from pre_commit.clientlib import load_manifest
1414
from pre_commit.util import cmd_output
15-
from pre_commit.util import yaml_dump
16-
from pre_commit.util import yaml_load
15+
from pre_commit.yaml import yaml_dump
16+
from pre_commit.yaml import yaml_load
1717
from testing.util import get_resource_path
1818
from testing.util import git_commit
1919

tests/commands/autoupdate_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44
from unittest import mock
55

66
import pytest
7-
import yaml
87

98
import pre_commit.constants as C
109
from pre_commit import envcontext
1110
from pre_commit import git
12-
from pre_commit import util
11+
from pre_commit import yaml
1312
from pre_commit.commands.autoupdate import _check_hooks_still_exist_at_rev
1413
from pre_commit.commands.autoupdate import autoupdate
1514
from pre_commit.commands.autoupdate import RepositoryCannotBeUpdatedError
@@ -206,7 +205,7 @@ def test_autoupdate_with_core_useBuiltinFSMonitor(out_of_date, tmpdir, store):
206205

207206

208207
def test_autoupdate_pure_yaml(out_of_date, tmpdir, store):
209-
with mock.patch.object(util, 'Dumper', yaml.SafeDumper):
208+
with mock.patch.object(yaml, 'Dumper', yaml.yaml.SafeDumper):
210209
test_autoupdate_out_of_date_repo(out_of_date, tmpdir, store)
211210

212211

0 commit comments

Comments
 (0)