Repository: pre-commit/pre-commit
Pull request #2673 (2023-01-02 22:35:59)

add pre_commit.yaml module


Patch:
diff --git a/pre_commit/clientlib.py b/pre_commit/clientlib.py
index e03d5d6..e191d3a 100644
--- a/pre_commit/clientlib.py
+++ b/pre_commit/clientlib.py
@@ -14,7 +14,7 @@ from identify.identify import ALL_TAGS
 import pre_commit.constants as C
 from pre_commit.errors import FatalError
 from pre_commit.languages.all import all_languages
-from pre_commit.util import yaml_load
+from pre_commit.yaml import yaml_load
 
 logger = logging.getLogger('pre_commit')
 
diff --git a/pre_commit/commands/autoupdate.py b/pre_commit/commands/autoupdate.py
index 6da5311..7ed6e77 100644
--- a/pre_commit/commands/autoupdate.py
+++ b/pre_commit/commands/autoupdate.py
@@ -20,8 +20,8 @@ from pre_commit.store import Store
 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 yaml_dump
-from pre_commit.util import yaml_load
+from pre_commit.yaml import yaml_dump
+from pre_commit.yaml import yaml_load
 
 
 class RevInfo(NamedTuple):
diff --git a/pre_commit/commands/migrate_config.py b/pre_commit/commands/migrate_config.py
index c3d0a50..836936b 100644
--- a/pre_commit/commands/migrate_config.py
+++ b/pre_commit/commands/migrate_config.py
@@ -5,7 +5,7 @@ import textwrap
 
 import yaml
 
-from pre_commit.util import yaml_load
+from pre_commit.yaml import yaml_load
 
 
 def _is_header_line(line: str) -> bool:
diff --git a/pre_commit/commands/try_repo.py b/pre_commit/commands/try_repo.py
index 5244aef..539ed3c 100644
--- a/pre_commit/commands/try_repo.py
+++ b/pre_commit/commands/try_repo.py
@@ -12,8 +12,8 @@ from pre_commit.clientlib import load_manifest
 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 yaml_dump
 from pre_commit.xargs import xargs
+from pre_commit.yaml import yaml_dump
 
 logger = logging.getLogger(__name__)
 
diff --git a/pre_commit/languages/dart.py b/pre_commit/languages/dart.py
index 9fbb63c..223567a 100644
--- a/pre_commit/languages/dart.py
+++ b/pre_commit/languages/dart.py
@@ -15,7 +15,7 @@ from pre_commit.hook import Hook
 from pre_commit.languages import helpers
 from pre_commit.prefix import Prefix
 from pre_commit.util import win_exe
-from pre_commit.util import yaml_load
+from pre_commit.yaml import yaml_load
 
 ENVIRONMENT_DIR = 'dartenv'
 
diff --git a/pre_commit/util.py b/pre_commit/util.py
index 324544c..d51fd32 100644
--- a/pre_commit/util.py
+++ b/pre_commit/util.py
@@ -2,7 +2,6 @@ from __future__ import annotations
 
 import contextlib
 import errno
-import functools
 import importlib.resources
 import os.path
 import shutil
@@ -15,22 +14,8 @@ from typing import Callable
 from typing import Generator
 from typing import IO
 
-import yaml
-
 from pre_commit import parse_shebang
 
-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, **kwargs: 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,
-        **kwargs,
-    )
-
 
 def force_bytes(exc: Any) -> bytes:
     with contextlib.suppress(TypeError):
diff --git a/pre_commit/yaml.py b/pre_commit/yaml.py
new file mode 100644
index 0000000..bdf4ec4
--- /dev/null
+++ b/pre_commit/yaml.py
@@ -0,0 +1,18 @@
+from __future__ import annotations
+
+import functools
+from typing import Any
+
+import yaml
+
+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, **kwargs: 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,
+        **kwargs,
+    )


Test patch:
diff --git a/testing/fixtures.py b/testing/fixtures.py
index 5182a08..79a1160 100644
--- a/testing/fixtures.py
+++ b/testing/fixtures.py
@@ -12,8 +12,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 pre_commit.yaml import yaml_dump
+from pre_commit.yaml import yaml_load
 from testing.util import get_resource_path
 from testing.util import git_commit
 
diff --git a/tests/commands/autoupdate_test.py b/tests/commands/autoupdate_test.py
index 3806b0e..4bcb5d8 100644
--- a/tests/commands/autoupdate_test.py
+++ b/tests/commands/autoupdate_test.py
@@ -4,12 +4,11 @@ import shlex
 from unittest import mock
 
 import pytest
-import yaml
 
 import pre_commit.constants as C
 from pre_commit import envcontext
 from pre_commit import git
-from pre_commit import util
+from pre_commit import yaml
 from pre_commit.commands.autoupdate import _check_hooks_still_exist_at_rev
 from pre_commit.commands.autoupdate import autoupdate
 from pre_commit.commands.autoupdate import RepositoryCannotBeUpdatedError
@@ -206,7 +205,7 @@ def test_autoupdate_with_core_useBuiltinFSMonitor(out_of_date, tmpdir, store):
 
 
 def test_autoupdate_pure_yaml(out_of_date, tmpdir, store):
-    with mock.patch.object(util, 'Dumper', yaml.SafeDumper):
+    with mock.patch.object(yaml, 'Dumper', yaml.yaml.SafeDumper):
         test_autoupdate_out_of_date_repo(out_of_date, tmpdir, store)