pre-commit-uv 4.4.2__tar.gz → 4.4.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ node: 3e6d0578f36c3c8883d91215ea8e840204a7043c
@@ -2,6 +2,38 @@
2
2
 
3
3
  <!-- towncrier release notes start -->
4
4
 
5
+ <a name="release-4-4-4"></a>
6
+
7
+ ## 4.4.4 (2026-12-06)
8
+
9
+ <a name="release-4-4-4-fixed"></a>
10
+
11
+ ### Fixed
12
+
13
+ - Accept hook environments built on a pre-release Python (such as `3.15.0rc1`): the health check compared uv's
14
+ `version_info` with a version that dropped the pre-release tag, so pre-commit stopped with "expected environment for
15
+ python to be healthy immediately after install" ([#240](https://github.com/tox-dev/pre-commit-uv/pull/240)) by
16
+ [@vinilpolepalli](https://github.com/vinilpolepalli) and [@gaborbernat](https://github.com/gaborbernat).
17
+
18
+ <a name="release-4-4-4-performance"></a>
19
+
20
+ ### Performance
21
+
22
+ - Reduce Python startup time and memory use when pre-commit-uv is installed on Python 3.15
23
+ ([#228](https://github.com/tox-dev/pre-commit-uv/pull/228)) by [@gaborbernat](https://github.com/gaborbernat).
24
+
25
+ <a name="release-4-4-3"></a>
26
+
27
+ ## 4.4.3 (2026-11-18)
28
+
29
+ <a name="release-4-4-3-packaging"></a>
30
+
31
+ ### Packaging
32
+
33
+ - Every wheel now carries a [PEP 770](https://peps.python.org/pep-0770/) CycloneDX SBOM in its `.dist-info/sboms`
34
+ directory, written at build time by [python-sbom](https://github.com/tox-dev/python-sbom)
35
+ ([#215](https://github.com/tox-dev/pre-commit-uv/pull/215)) by [@gaborbernat](https://github.com/gaborbernat).
36
+
5
37
  <a name="release-4-4-2"></a>
6
38
 
7
39
  ## 4.4.2 (2026-11-04)
@@ -38,3 +38,7 @@ version for a `feature` fragment, and a patch version for other categories. The
38
38
  `prek` on the result, and uses the repository-scoped App to push the release commit and tag. The tag workflow builds the
39
39
  distribution, publishes it to PyPI through trusted publishing, then creates the GitHub Release from the tagged
40
40
  changelog. The workflow does not ask for a version string.
41
+
42
+ ## Tests
43
+
44
+ Add a test for every new feature and bug fix. A change without a test needs a reason in the pull request description.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: pre-commit-uv
3
- Version: 4.4.2
3
+ Version: 4.4.4
4
4
  Import-Name: pre_commit_uv
5
5
  Summary: Run pre-commit with uv
6
6
  Project-URL: Bug Tracker, https://github.com/tox-dev/pre-commit-uv/issues
@@ -4,6 +4,7 @@ requires = [
4
4
  "hatch-fancy-pypi-readme>=25.1",
5
5
  "hatch-vcs>=0.5",
6
6
  "hatchling>=1.32.4",
7
+ "python-sbom[hatch]>=0.1.2",
7
8
  ]
8
9
 
9
10
  [project]
@@ -68,6 +69,7 @@ build = [
68
69
  "hatch-fancy-pypi-readme>=25.1",
69
70
  "hatch-vcs>=0.5",
70
71
  "hatchling>=1.32.4",
72
+ "python-sbom[hatch]>=0.1.2",
71
73
  "setuptools>=80",
72
74
  ]
73
75
  lint = [
@@ -105,6 +107,7 @@ metadata.hooks.fancy-pypi-readme.substitutions = [
105
107
  build.dev-mode-dirs = [
106
108
  "src",
107
109
  ]
110
+ build.hooks.python-sbom = {}
108
111
  build.targets.sdist.include = [
109
112
  "/CHANGELOG.md",
110
113
  "/CONTRIBUTING.md",
@@ -130,26 +133,25 @@ lint.select = [
130
133
  "ALL",
131
134
  ]
132
135
  lint.ignore = [
133
- "any-type", # allow Any as type annotation
134
136
  "CPY", # No copyright statements
135
- "docstring-missing-exception", # not working with Sphinx
136
- "hardcoded-bind-all-interfaces", # Possible binding to all interfaces
137
- "incorrect-blank-line-before-class", # `one-blank-line-before-class` (D203) and `no-blank-line-before-class` (D211) are incompatible
137
+ "incorrect-blank-line-before-class", # Conflicts with `blank-line-before-class`
138
138
  "missing-trailing-comma", # Conflict with formatter
139
- "multi-line-summary-first-line", # `multi-line-summary-first-line` (D212) and `multi-line-summary-second-line` (D213) are incompatible
140
- "non-empty-init-module", # `__init__` module should only contain docstrings and re-exports
139
+ "multi-line-summary-first-line", # Conflicts with `multi-line-summary-second-line`
141
140
  "single-line-implicit-string-concatenation", # Conflict with formatter
141
+ "undocumented-magic-method", # undocumented-public-api (simplint) covers the public API instead
142
+ "undocumented-public-class", # undocumented-public-api (simplint) covers the public API instead
143
+ "undocumented-public-function", # undocumented-public-api (simplint) covers the public API instead
144
+ "undocumented-public-init", # undocumented-public-api (simplint) covers the public API instead
145
+ "undocumented-public-method", # undocumented-public-api (simplint) covers the public API instead
146
+ "undocumented-public-nested-class", # undocumented-public-api (simplint) covers the public API instead
142
147
  ]
143
148
  lint.per-file-ignores."tests/**/*.py" = [
144
- "assert", # asserts allowed in tests...
145
- "D", # don"t care about documentation in tests
146
- "FBT", # don"t care about booleans as positional arguments in tests
147
- "implicit-namespace-package", # no implicit namespace
148
- "import-private-name", # private import
149
- "magic-value-comparison", # Magic value used in comparison, consider replacing with a constant variable
150
- "S", # `subprocess` call: check for execution of untrusted input
151
- "too-many-arguments", # any number of arguments in tests
152
- "too-many-positional-arguments", # any number of arguments in tests
149
+ "assert", # asserts allowed in tests...
150
+ "implicit-namespace-package", # no implicit namespace
151
+ "import-private-name", # private import
152
+ "subprocess-without-shell-equals-true", # integration tests pass fixed commands and fixture paths to subprocesses
153
+ "suspicious-subprocess-import", # integration tests execute pre-commit and its installed hook
154
+ "undocumented-public-module", # tests do not need module docstrings
153
155
  ]
154
156
  lint.isort = { known-first-party = [
155
157
  "pre_commit_uv",
@@ -210,6 +212,9 @@ type = [
210
212
  { directory = "packaging", name = "Packaging", showcontent = true },
211
213
  ]
212
214
 
215
+ [tool.python-sbom]
216
+ supplier = { name = "tox-dev", url = [ "https://github.com/tox-dev" ] }
217
+
213
218
  [tool.nab]
214
219
  mode = "universal"
215
220
  base-group = "project-runtime"
@@ -217,6 +222,3 @@ uploaded-prior-to = "P7D"
217
222
  decision-order = "stable"
218
223
  matrix.python = ">=3.15,<3.16"
219
224
  matrix.platforms = [ "linux_x86_64" ]
220
- # release-creator 4.4.0 needs a seven-day cutoff exception.
221
- # expires 2026-11-09
222
- packages.release-creator.uploaded-prior-to = 2026-11-02T21:45:00Z
@@ -0,0 +1,3 @@
1
+ """Package root."""
2
+
3
+ from __future__ import annotations
@@ -2,6 +2,10 @@
2
2
 
3
3
  from __future__ import annotations
4
4
 
5
+ __lazy_modules__ = [
6
+ "pathlib",
7
+ ]
8
+
5
9
  # only import built-ins at top level to avoid interpreter startup overhead
6
10
  import os
7
11
  import pathlib
@@ -19,7 +23,24 @@ if TYPE_CHECKING:
19
23
  health_check: Callable[[Prefix, str], str | None]
20
24
 
21
25
 
22
- _original_main = None
26
+ _ORIGINAL_MAIN = None
27
+
28
+
29
+ def patch() -> None:
30
+ global _ORIGINAL_MAIN
31
+ if _ORIGINAL_MAIN is not None: # already patched, nothing more to do
32
+ return
33
+
34
+ if _is_calling_pre_commit() and os.environ.get("DISABLE_PRE_COMMIT_UV_PATCH") is None:
35
+ from pre_commit import main # ruff:ignore[import-outside-top-level] loaded only when pre-commit runs, keeping startup cheap
36
+
37
+ _ORIGINAL_MAIN, main.main = main.main, _new_main # ty: ignore[invalid-assignment] pre-commit exposes main as a plain function we patch on purpose
38
+ if "--version" in sys.argv:
39
+ from importlib.metadata import version # ruff:ignore[import-outside-top-level] loaded only when pre-commit runs, keeping startup cheap
40
+
41
+ from pre_commit import constants # ruff:ignore[import-outside-top-level] loaded only when pre-commit runs, keeping startup cheap
42
+
43
+ constants.VERSION = f"{constants.VERSION} (pre-commit-uv={version('pre-commit-uv')}, uv={version('uv')})"
23
44
 
24
45
 
25
46
  def _is_calling_pre_commit() -> bool:
@@ -38,44 +59,22 @@ def _is_calling_pre_commit() -> bool:
38
59
  return "-m" in sys.argv and "hook-impl" in sys.argv
39
60
 
40
61
 
41
- def _patch() -> None:
42
- global _original_main
43
- if _original_main is not None: # already patched, nothing more to do
44
- return
45
-
46
- if _is_calling_pre_commit() and os.environ.get("DISABLE_PRE_COMMIT_UV_PATCH") is None:
47
- from pre_commit import main # ruff:ignore[import-outside-top-level]
48
-
49
- _original_main, main.main = main.main, _new_main # ty: ignore[invalid-assignment]
50
- if "--version" in sys.argv:
51
- from importlib.metadata import version as _metadata_version # ruff:ignore[import-outside-top-level]
52
-
53
- from pre_commit import constants # ruff:ignore[import-outside-top-level]
54
-
55
- constants.VERSION = (
56
- f"{constants.VERSION} ("
57
- f"pre-commit-uv={_metadata_version('pre-commit-uv')}, "
58
- f"uv={_metadata_version('uv')}"
59
- f")"
60
- )
61
-
62
-
63
62
  def _new_main(argv: Sequence[str] | None = None) -> int:
64
63
  # imports applied locally to avoid patching import overhead cost
65
- from functools import cache # ruff:ignore[import-outside-top-level]
64
+ from functools import cache # ruff:ignore[import-outside-top-level] loaded only when pre-commit runs, keeping startup cheap
66
65
 
67
- from pre_commit.languages import python # ruff:ignore[import-outside-top-level]
66
+ from pre_commit.languages import python # ruff:ignore[import-outside-top-level] loaded only when pre-commit runs, keeping startup cheap
68
67
 
69
68
  def _install_environment(
70
69
  prefix: Prefix,
71
70
  version: str,
72
71
  additional_dependencies: Sequence[str],
73
72
  ) -> None:
74
- import logging # ruff:ignore[import-outside-top-level]
73
+ import logging # ruff:ignore[import-outside-top-level] loaded only when pre-commit runs, keeping startup cheap
75
74
 
76
- from pre_commit.git import get_root # ruff:ignore[import-outside-top-level]
77
- from pre_commit.lang_base import environment_dir, setup_cmd # ruff:ignore[import-outside-top-level]
78
- from pre_commit.util import cmd_output_b # ruff:ignore[import-outside-top-level]
75
+ from pre_commit.git import get_root # ruff:ignore[import-outside-top-level] loaded only when pre-commit runs, keeping startup cheap
76
+ from pre_commit.lang_base import environment_dir, setup_cmd # ruff:ignore[import-outside-top-level] loaded only when pre-commit runs, keeping startup cheap
77
+ from pre_commit.util import cmd_output_b # ruff:ignore[import-outside-top-level] loaded only when pre-commit runs, keeping startup cheap
79
78
 
80
79
  project_root_dir = get_root()
81
80
 
@@ -111,57 +110,46 @@ def _new_main(argv: Sequence[str] | None = None) -> int:
111
110
 
112
111
  @cache
113
112
  def _uv() -> str:
114
- from uv import find_uv_bin # ruff:ignore[import-outside-top-level]
113
+ from uv import find_uv_bin # ruff:ignore[import-outside-top-level] loaded only when pre-commit runs, keeping startup cheap
115
114
 
116
115
  return find_uv_bin()
117
116
 
118
117
  @cache
119
118
  def self_version() -> str:
120
- from importlib.metadata import version as _metadata_version # ruff:ignore[import-outside-top-level]
119
+ from importlib.metadata import version # ruff:ignore[import-outside-top-level] loaded only when pre-commit runs, keeping startup cheap
121
120
 
122
- return _metadata_version("pre-commit-uv")
121
+ return version("pre-commit-uv")
123
122
 
124
123
  @cache
125
124
  def uv_version() -> str:
126
- from importlib.metadata import version as _metadata_version # ruff:ignore[import-outside-top-level]
125
+ from importlib.metadata import version # ruff:ignore[import-outside-top-level] loaded only when pre-commit runs, keeping startup cheap
127
126
 
128
- return _metadata_version("uv")
127
+ return version("uv")
129
128
 
130
129
  patched = cast("_PatchablePython", python)
131
130
  patched.install_environment = _install_environment
132
131
  patched.health_check = _health_check
133
- assert _original_main is not None # ruff:ignore[assert]
134
- return _original_main(argv)
135
-
136
-
137
- def _version_info(exe: str) -> str:
138
- from pre_commit.util import CalledProcessError, cmd_output # ruff:ignore[import-outside-top-level]
139
-
140
- prog = 'import sys;print(".".join(str(p) for p in sys.version_info[0:3]))'
141
- try:
142
- return cmd_output(exe, "-S", "-c", prog)[1].strip()
143
- except CalledProcessError:
144
- return f"<<error retrieving version from {exe}>>"
132
+ assert _ORIGINAL_MAIN is not None # ruff:ignore[assert] patch sets _ORIGINAL_MAIN before installing _new_main
133
+ return _ORIGINAL_MAIN(argv)
145
134
 
146
135
 
147
136
  def _health_check(prefix: Prefix, version: str) -> str | None:
148
137
  # uv may record fewer version components in pyvenv.cfg than pre-commit expects (e.g. "3.14" vs "3.14.6"),
149
138
  # so compare only the components uv actually wrote rather than requiring an exact string match
150
- from pre_commit.lang_base import environment_dir # ruff:ignore[import-outside-top-level]
151
- from pre_commit.languages import python # ruff:ignore[import-outside-top-level]
152
- from pre_commit.util import win_exe # ruff:ignore[import-outside-top-level]
139
+ from pre_commit.lang_base import environment_dir # ruff:ignore[import-outside-top-level] loaded only when pre-commit runs, keeping startup cheap
140
+ from pre_commit.languages import python # ruff:ignore[import-outside-top-level] loaded only when pre-commit runs, keeping startup cheap
141
+ from pre_commit.util import win_exe # ruff:ignore[import-outside-top-level] loaded only when pre-commit runs, keeping startup cheap
153
142
 
154
143
  pyvenv_cfg = pathlib.Path(environment_dir(prefix, python.ENVIRONMENT_DIR, version)) / "pyvenv.cfg"
155
144
  if not pyvenv_cfg.exists():
156
145
  return "pyvenv.cfg does not exist (old virtualenv?)"
157
146
 
158
- cfg = python._read_pyvenv_cfg(str(pyvenv_cfg)) # ruff:ignore[private-member-access]
147
+ cfg = python._read_pyvenv_cfg(str(pyvenv_cfg)) # ruff:ignore[private-member-access] no public parser in pre-commit
159
148
  if "version_info" not in cfg:
160
149
  return "created virtualenv's pyvenv.cfg is missing `version_info`"
161
150
  expected = cfg["version_info"].split(".")
162
151
 
163
- py_exe = prefix.path(python.bin_dir(str(pyvenv_cfg.parent)), win_exe("python"))
164
- targets = [("virtualenv python", py_exe)]
152
+ targets = [("virtualenv python", prefix.path(python.bin_dir(str(pyvenv_cfg.parent)), win_exe("python")))]
165
153
  if "base-executable" in cfg:
166
154
  targets.append(("base executable", cfg["base-executable"]))
167
155
  for label, exe in targets:
@@ -172,3 +160,18 @@ def _health_check(prefix: Prefix, version: str) -> str | None:
172
160
  f"- expected version: {cfg['version_info']}\n"
173
161
  )
174
162
  return None
163
+
164
+
165
+ def _version_info(exe: str) -> str:
166
+ from pre_commit.util import CalledProcessError, cmd_output # ruff:ignore[import-outside-top-level] loaded only when pre-commit runs, keeping startup cheap
167
+
168
+ try:
169
+ # uv writes this string as version_info: a pre-release keeps its tag (3.15.0rc1), a local build loses its "+"
170
+ return cmd_output(exe, "-S", "-c", 'import platform;print(platform.python_version().rstrip("+"))')[1].strip()
171
+ except CalledProcessError:
172
+ return f"<<error retrieving version from {exe}>>"
173
+
174
+
175
+ __all__ = [
176
+ "patch",
177
+ ]
@@ -0,0 +1 @@
1
+ import pre_commit_uv._pre_commit_patch; pre_commit_uv._pre_commit_patch.patch()
@@ -0,0 +1,293 @@
1
+ from __future__ import annotations
2
+
3
+ import sys
4
+ from importlib import reload
5
+ from importlib.metadata import version
6
+ from shutil import which
7
+ from subprocess import check_call, check_output
8
+ from textwrap import dedent
9
+ from typing import TYPE_CHECKING, Final
10
+
11
+ import pytest
12
+ from pre_commit import main
13
+ from pre_commit.languages import python
14
+ from pre_commit.prefix import Prefix
15
+
16
+ from pre_commit_uv._pre_commit_patch import patch
17
+
18
+ if TYPE_CHECKING:
19
+ from pathlib import Path
20
+
21
+ _PRECOMMIT_FILE: Final[str] = ".pre-commit-config.yaml"
22
+ _UV_VERSION: Final[str] = version("uv")
23
+ _PRE_COMMIT_UV_VERSION: Final[str] = version("pre-commit-uv")
24
+ _VERSION: Final[str] = "default"
25
+ _MAJOR_MINOR: Final[str] = f"{sys.version_info[0]}.{sys.version_info[1]}"
26
+ _FULL_VERSION: Final[str] = ".".join(str(p) for p in sys.version_info[0:3])
27
+ _WRONG_VERSION: Final[str] = f"{sys.version_info[0]}.{sys.version_info[1] + 1}"
28
+
29
+
30
+ @pytest.fixture
31
+ def git_repo(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
32
+ conf_file = tmp_path / _PRECOMMIT_FILE
33
+ conf = """
34
+ repos:
35
+ - repo: https://github.com/tox-dev/pyproject-fmt
36
+ rev: "2.2.0"
37
+ hooks:
38
+ - id: pyproject-fmt
39
+ """
40
+ conf_file.write_text(dedent(conf))
41
+ monkeypatch.setenv("PRE_COMMIT_HOME", str(tmp_path / "store"))
42
+ monkeypatch.chdir(tmp_path)
43
+ git = which("git")
44
+ assert git is not None
45
+ check_call([git, "init"])
46
+ return tmp_path
47
+
48
+
49
+ @pytest.fixture
50
+ def install_hook(git_repo: Path) -> None:
51
+ pre_commit = which("pre-commit")
52
+ assert pre_commit is not None
53
+ check_call([pre_commit, "install", "--install-hooks", "-c", str(git_repo / _PRECOMMIT_FILE)])
54
+ check_call([pre_commit, "clean"]) # ensures that 'install_environment' gets called
55
+
56
+
57
+ @pytest.mark.usefixtures("install_hook")
58
+ def test_run_precommit_hook(git_repo: Path) -> None:
59
+ hook_result = check_output([str(git_repo / ".git" / "hooks" / "pre-commit")], encoding="utf-8")
60
+ assert (
61
+ f"[INFO] Using pre-commit with uv {_UV_VERSION} via pre-commit-uv {_PRE_COMMIT_UV_VERSION}"
62
+ in hook_result.splitlines()
63
+ )
64
+
65
+
66
+ @pytest.mark.usefixtures("install_hook")
67
+ def test_call_as_module() -> None:
68
+ run_result = check_output([sys.executable, "-m", "pre_commit", "run", "-a", "--color", "never"], encoding="utf-8")
69
+ assert (
70
+ f"[INFO] Using pre-commit with uv {_UV_VERSION} via pre-commit-uv {_PRE_COMMIT_UV_VERSION}"
71
+ in run_result.splitlines()
72
+ )
73
+
74
+
75
+ def test_install(git_repo: Path, caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch) -> None:
76
+ monkeypatch.setenv("FORCE_PRE_COMMIT_UV_PATCH", "1")
77
+
78
+ reload(main)
79
+ reload(sys.modules[patch.__module__])
80
+ patch()
81
+ main.main(["install-hooks", "-c", str(git_repo / _PRECOMMIT_FILE)])
82
+
83
+ assert caplog.messages == [
84
+ "Initializing environment for https://github.com/tox-dev/pyproject-fmt.",
85
+ "Installing environment for https://github.com/tox-dev/pyproject-fmt.",
86
+ "Once installed this environment will be reused.",
87
+ "This may take a few minutes...",
88
+ f"Using pre-commit with uv {_UV_VERSION} via pre-commit-uv {_PRE_COMMIT_UV_VERSION}",
89
+ ]
90
+
91
+
92
+ def test_install_seeds_pip(git_repo: Path, monkeypatch: pytest.MonkeyPatch) -> None:
93
+ monkeypatch.setenv("FORCE_PRE_COMMIT_UV_PATCH", "1")
94
+
95
+ patch()
96
+ main.main(["install-hooks", "-c", str(git_repo / _PRECOMMIT_FILE)])
97
+
98
+ env_dirs = list((git_repo / "store").rglob("py_env-*"))
99
+ assert env_dirs, "expected at least one hook environment"
100
+ result = check_output([str(next((env_dirs[0] / "bin").glob("python*"))), "-c", "import pip"], encoding="utf-8")
101
+ assert not result
102
+
103
+
104
+ _TEST_INSTALL_WITH_UV_CONFIG_CASES: Final[list[tuple[str, str]]] = [
105
+ (
106
+ "pyproject.toml",
107
+ """
108
+ [[tool.uv.index]]
109
+ name = "internal"
110
+ url = "https://pypi.org/simple/"
111
+ default = true
112
+ """,
113
+ ),
114
+ (
115
+ "uv.toml",
116
+ """
117
+ [[index]]
118
+ name = "internal"
119
+ url = "https://pypi.org/simple/"
120
+ default = true
121
+ """,
122
+ ),
123
+ ]
124
+
125
+
126
+ @pytest.mark.parametrize(
127
+ ("file_name", "content"),
128
+ _TEST_INSTALL_WITH_UV_CONFIG_CASES,
129
+ )
130
+ def test_install_with_uv_config(
131
+ git_repo: Path,
132
+ caplog: pytest.LogCaptureFixture,
133
+ monkeypatch: pytest.MonkeyPatch,
134
+ file_name: str,
135
+ content: str,
136
+ ) -> None:
137
+ (git_repo / file_name).write_text(dedent(content))
138
+
139
+ monkeypatch.setenv("FORCE_PRE_COMMIT_UV_PATCH", "1")
140
+
141
+ patch()
142
+ main.main(["install-hooks", "-c", str(git_repo / _PRECOMMIT_FILE)])
143
+
144
+ assert caplog.messages == [
145
+ "Initializing environment for https://github.com/tox-dev/pyproject-fmt.",
146
+ "Installing environment for https://github.com/tox-dev/pyproject-fmt.",
147
+ "Once installed this environment will be reused.",
148
+ "This may take a few minutes...",
149
+ f"Using pre-commit with uv {_UV_VERSION} via pre-commit-uv {_PRE_COMMIT_UV_VERSION}",
150
+ ]
151
+
152
+
153
+ _TEST_INSTALL_WITH_UV_CONFIG_RAISES_ERROR_CASES: Final[list[tuple[str, str]]] = [
154
+ (
155
+ "pyproject.toml",
156
+ """
157
+ [[tool.uv.index]]
158
+ name = "internal"
159
+ url = "https://pypi.example/simple/"
160
+ default = true
161
+ """,
162
+ ),
163
+ (
164
+ "uv.toml",
165
+ """
166
+ [[index]]
167
+ name = "internal"
168
+ url = "https://pypi.example/simple/"
169
+ default = true
170
+ """,
171
+ ),
172
+ ]
173
+
174
+
175
+ @pytest.mark.parametrize(("file_name", "content"), _TEST_INSTALL_WITH_UV_CONFIG_RAISES_ERROR_CASES)
176
+ def test_install_with_uv_config_raises_error(
177
+ git_repo: Path,
178
+ monkeypatch: pytest.MonkeyPatch,
179
+ file_name: str,
180
+ content: str,
181
+ ) -> None:
182
+ # Test to make sure that uv config is used for non default pypi repos.
183
+ (git_repo / file_name).write_text(dedent(content))
184
+
185
+ monkeypatch.setenv("FORCE_PRE_COMMIT_UV_PATCH", "1")
186
+
187
+ patch()
188
+
189
+ # would raise SystemExit due to bad config
190
+ with pytest.raises(SystemExit):
191
+ main.main(["install-hooks", "-c", str(git_repo / _PRECOMMIT_FILE)])
192
+
193
+
194
+ @pytest.fixture
195
+ def patched_pre_commit(monkeypatch: pytest.MonkeyPatch) -> None:
196
+ monkeypatch.setenv("FORCE_PRE_COMMIT_UV_PATCH", "1")
197
+ patch()
198
+ with pytest.raises(SystemExit) as result:
199
+ main.main(["--version"])
200
+ assert result.value.code == 0
201
+
202
+
203
+ @pytest.mark.usefixtures("patched_pre_commit")
204
+ def test_health_check_truncated_version_passes(tmp_path: Path) -> None:
205
+ # uv >=0.11.22 writes only the major.minor into pyvenv.cfg (see issue #152).
206
+ prefix = _make_env(tmp_path, f"version_info = {_MAJOR_MINOR}\nbase-executable = {sys.executable}\n")
207
+
208
+ assert python.health_check(prefix, _VERSION) is None
209
+
210
+
211
+ def _make_env(tmp_path: Path, pyvenv_cfg: str | None) -> Prefix:
212
+ envdir = tmp_path / f"py_env-{_VERSION}"
213
+ bin_dir = python.bin_dir(str(envdir))
214
+ (tmp_path / bin_dir).mkdir(parents=True)
215
+ (tmp_path / bin_dir / python.win_exe("python")).symlink_to(sys.executable)
216
+ if pyvenv_cfg is not None:
217
+ (envdir / "pyvenv.cfg").write_text(pyvenv_cfg)
218
+ return Prefix(str(tmp_path))
219
+
220
+
221
+ @pytest.mark.usefixtures("patched_pre_commit")
222
+ def test_health_check_full_version_passes(tmp_path: Path) -> None:
223
+ prefix = _make_env(tmp_path, f"version_info = {_FULL_VERSION}\nbase-executable = {sys.executable}\n")
224
+
225
+ assert python.health_check(prefix, _VERSION) is None
226
+
227
+
228
+ @pytest.mark.skipif(sys.platform == "win32", reason="the stand-in interpreter is a shebang script")
229
+ @pytest.mark.usefixtures("patched_pre_commit")
230
+ def test_health_check_pre_release_version_passes(tmp_path: Path) -> None:
231
+ # uv writes platform.python_version() for an interpreter it does not manage, so a pre-release keeps its tag
232
+ prefix = _make_env(tmp_path, "version_info = 3.16.0a2\n")
233
+ python_exe = tmp_path / python.bin_dir(str(tmp_path / f"py_env-{_VERSION}")) / "python"
234
+ python_exe.unlink()
235
+ pre_release = sys.version.replace(sys.version.split()[0], "3.16.0a2", 1)
236
+ python_exe.write_text(
237
+ dedent(f"""\
238
+ #!{sys.executable}
239
+ import sys
240
+ sys.version, sys.version_info = {pre_release!r}, (3, 16, 0, "alpha", 2)
241
+ exec(sys.argv[-1])
242
+ """)
243
+ )
244
+ python_exe.chmod(0o755)
245
+
246
+ assert python.health_check(prefix, _VERSION) is None
247
+
248
+
249
+ @pytest.mark.usefixtures("patched_pre_commit")
250
+ def test_health_check_no_base_executable_passes(tmp_path: Path) -> None:
251
+ prefix = _make_env(tmp_path, f"version_info = {_MAJOR_MINOR}\n")
252
+
253
+ assert python.health_check(prefix, _VERSION) is None
254
+
255
+
256
+ @pytest.mark.usefixtures("patched_pre_commit")
257
+ def test_health_check_version_mismatch_fails(tmp_path: Path) -> None:
258
+ result = python.health_check(_make_env(tmp_path, f"version_info = {_WRONG_VERSION}\n"), _VERSION)
259
+
260
+ assert result is not None
261
+ assert "virtualenv python version did not match created version" in result
262
+ assert f"- expected version: {_WRONG_VERSION}" in result
263
+
264
+
265
+ @pytest.mark.usefixtures("patched_pre_commit")
266
+ def test_health_check_base_executable_mismatch_fails(tmp_path: Path) -> None:
267
+ prefix = _make_env(tmp_path, f"version_info = {_MAJOR_MINOR}\nbase-executable = /does/not/exist/python\n")
268
+
269
+ result = python.health_check(prefix, _VERSION)
270
+
271
+ assert result is not None
272
+ assert "base executable version did not match created version" in result
273
+
274
+
275
+ @pytest.mark.usefixtures("patched_pre_commit")
276
+ def test_health_check_missing_pyvenv_cfg(tmp_path: Path) -> None:
277
+ prefix = _make_env(tmp_path, None)
278
+
279
+ assert python.health_check(prefix, _VERSION) == "pyvenv.cfg does not exist (old virtualenv?)"
280
+
281
+
282
+ @pytest.mark.parametrize(
283
+ "contents",
284
+ [
285
+ pytest.param("# comment\nhome = /usr\n", id="no-version"),
286
+ pytest.param("version_info = 3=15\n", id="extra-equals"),
287
+ ],
288
+ )
289
+ @pytest.mark.usefixtures("patched_pre_commit")
290
+ def test_health_check_missing_version_info(tmp_path: Path, contents: str) -> None:
291
+ prefix = _make_env(tmp_path, contents)
292
+
293
+ assert python.health_check(prefix, _VERSION) == "created virtualenv's pyvenv.cfg is missing `version_info`"
@@ -1 +0,0 @@
1
- import pre_commit_uv; pre_commit_uv._patch()
@@ -1,77 +0,0 @@
1
- from __future__ import annotations
2
-
3
- import sys
4
- from typing import TYPE_CHECKING
5
-
6
- from pre_commit.languages import python
7
- from pre_commit.prefix import Prefix
8
-
9
- from pre_commit_uv import _health_check
10
-
11
- if TYPE_CHECKING:
12
- from pathlib import Path
13
-
14
- version = "default"
15
- major_minor = f"{sys.version_info[0]}.{sys.version_info[1]}"
16
- full_version = ".".join(str(p) for p in sys.version_info[0:3])
17
- wrong_version = f"{sys.version_info[0]}.{sys.version_info[1] + 1}"
18
-
19
-
20
- def _make_env(tmp_path: Path, pyvenv_cfg: str | None) -> Prefix:
21
- envdir = tmp_path / f"py_env-{version}"
22
- bin_dir = python.bin_dir(str(envdir))
23
- (tmp_path / bin_dir).mkdir(parents=True)
24
- (tmp_path / bin_dir / python.win_exe("python")).symlink_to(sys.executable)
25
- if pyvenv_cfg is not None:
26
- (envdir / "pyvenv.cfg").write_text(pyvenv_cfg)
27
- return Prefix(str(tmp_path))
28
-
29
-
30
- def test_health_check_truncated_version_passes(tmp_path: Path) -> None:
31
- """uv >=0.11.22 writes only the major.minor into pyvenv.cfg (see issue #152)."""
32
- prefix = _make_env(tmp_path, f"version_info = {major_minor}\nbase-executable = {sys.executable}\n")
33
-
34
- assert _health_check(prefix, version) is None
35
-
36
-
37
- def test_health_check_full_version_passes(tmp_path: Path) -> None:
38
- prefix = _make_env(tmp_path, f"version_info = {full_version}\nbase-executable = {sys.executable}\n")
39
-
40
- assert _health_check(prefix, version) is None
41
-
42
-
43
- def test_health_check_no_base_executable_passes(tmp_path: Path) -> None:
44
- prefix = _make_env(tmp_path, f"version_info = {major_minor}\n")
45
-
46
- assert _health_check(prefix, version) is None
47
-
48
-
49
- def test_health_check_version_mismatch_fails(tmp_path: Path) -> None:
50
- prefix = _make_env(tmp_path, f"version_info = {wrong_version}\n")
51
-
52
- result = _health_check(prefix, version)
53
-
54
- assert result is not None
55
- assert "virtualenv python version did not match created version" in result
56
- assert f"- expected version: {wrong_version}" in result
57
-
58
-
59
- def test_health_check_base_executable_mismatch_fails(tmp_path: Path) -> None:
60
- prefix = _make_env(tmp_path, f"version_info = {major_minor}\nbase-executable = /does/not/exist/python\n")
61
-
62
- result = _health_check(prefix, version)
63
-
64
- assert result is not None
65
- assert "base executable version did not match created version" in result
66
-
67
-
68
- def test_health_check_missing_pyvenv_cfg(tmp_path: Path) -> None:
69
- prefix = _make_env(tmp_path, None)
70
-
71
- assert _health_check(prefix, version) == "pyvenv.cfg does not exist (old virtualenv?)"
72
-
73
-
74
- def test_health_check_missing_version_info(tmp_path: Path) -> None:
75
- prefix = _make_env(tmp_path, "home = /usr\n")
76
-
77
- assert _health_check(prefix, version) == "created virtualenv's pyvenv.cfg is missing `version_info`"
@@ -1,178 +0,0 @@
1
- from __future__ import annotations
2
-
3
- import sys
4
- from importlib.metadata import version
5
- from subprocess import check_call, check_output
6
- from textwrap import dedent
7
- from typing import TYPE_CHECKING
8
-
9
- import pytest
10
- from pre_commit import main
11
-
12
- if TYPE_CHECKING:
13
- from pathlib import Path
14
-
15
- precommit_file = ".pre-commit-config.yaml"
16
- uv = version("uv")
17
- self = version("pre-commit-uv")
18
-
19
-
20
- @pytest.fixture
21
- def git_repo(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> Path:
22
- conf = """
23
- repos:
24
- - repo: https://github.com/tox-dev/pyproject-fmt
25
- rev: "2.2.0"
26
- hooks:
27
- - id: pyproject-fmt
28
- """
29
- conf_file = tmp_path / precommit_file
30
- conf_file.write_text(dedent(conf))
31
- monkeypatch.setenv("PRE_COMMIT_HOME", str(tmp_path / "store"))
32
- monkeypatch.chdir(tmp_path)
33
- check_call(["git", "init"])
34
- return tmp_path
35
-
36
-
37
- @pytest.fixture
38
- def install_hook(git_repo: Path) -> None:
39
- check_call(["pre-commit", "install", "--install-hooks", "-c", str(git_repo / precommit_file)])
40
- check_call(["pre-commit", "clean"]) # ensures that 'install_environment' gets called
41
-
42
-
43
- @pytest.mark.usefixtures("install_hook")
44
- def test_run_precommit_hook() -> None:
45
- hook_result = check_output([".git/hooks/pre-commit"], encoding="utf-8")
46
- assert f"[INFO] Using pre-commit with uv {uv} via pre-commit-uv {self}" in hook_result.splitlines()
47
-
48
-
49
- @pytest.mark.usefixtures("install_hook")
50
- def test_call_as_module() -> None:
51
- run_result = check_output([sys.executable, "-m", "pre_commit", "run", "-a", "--color", "never"], encoding="utf-8")
52
- assert f"[INFO] Using pre-commit with uv {uv} via pre-commit-uv {self}" in run_result.splitlines()
53
-
54
-
55
- def test_install(git_repo: Path, caplog: pytest.LogCaptureFixture, monkeypatch: pytest.MonkeyPatch) -> None:
56
- monkeypatch.setenv("FORCE_PRE_COMMIT_UV_PATCH", "1")
57
-
58
- import pre_commit_uv # ruff:ignore[import-outside-top-level]
59
-
60
- pre_commit_uv._patch() # ruff:ignore[private-member-access]
61
- main.main(["install-hooks", "-c", str(git_repo / precommit_file)])
62
-
63
- assert caplog.messages == [
64
- "Initializing environment for https://github.com/tox-dev/pyproject-fmt.",
65
- "Installing environment for https://github.com/tox-dev/pyproject-fmt.",
66
- "Once installed this environment will be reused.",
67
- "This may take a few minutes...",
68
- f"Using pre-commit with uv {uv} via pre-commit-uv {self}",
69
- ]
70
-
71
-
72
- def test_install_seeds_pip(git_repo: Path, monkeypatch: pytest.MonkeyPatch) -> None:
73
- monkeypatch.setenv("FORCE_PRE_COMMIT_UV_PATCH", "1")
74
-
75
- import pre_commit_uv # ruff:ignore[import-outside-top-level]
76
-
77
- pre_commit_uv._patch() # ruff:ignore[private-member-access]
78
- main.main(["install-hooks", "-c", str(git_repo / precommit_file)])
79
-
80
- env_dirs = list((git_repo / "store").rglob("py_env-*"))
81
- assert env_dirs, "expected at least one hook environment"
82
- py = next((env_dirs[0] / "bin").glob("python*"))
83
- result = check_output([str(py), "-c", "import pip"], encoding="utf-8")
84
- assert not result
85
-
86
-
87
- test_install_with_uv_config_cases: list[tuple[str, str]] = [
88
- (
89
- "pyproject.toml",
90
- """
91
- [[tool.uv.index]]
92
- name = "internal"
93
- url = "https://pypi.org/simple/"
94
- default = true
95
- """,
96
- ),
97
- (
98
- "uv.toml",
99
- """
100
- [[index]]
101
- name = "internal"
102
- url = "https://pypi.org/simple/"
103
- default = true
104
- """,
105
- ),
106
- ]
107
-
108
-
109
- @pytest.mark.parametrize(
110
- ("file_name", "content"),
111
- test_install_with_uv_config_cases,
112
- )
113
- def test_install_with_uv_config(
114
- git_repo: Path,
115
- caplog: pytest.LogCaptureFixture,
116
- monkeypatch: pytest.MonkeyPatch,
117
- file_name: str,
118
- content: str,
119
- ) -> None:
120
- (git_repo / file_name).write_text(dedent(content))
121
-
122
- monkeypatch.setenv("FORCE_PRE_COMMIT_UV_PATCH", "1")
123
-
124
- import pre_commit_uv # ruff:ignore[import-outside-top-level]
125
-
126
- pre_commit_uv._patch() # ruff:ignore[private-member-access]
127
- main.main(["install-hooks", "-c", str(git_repo / precommit_file)])
128
-
129
- assert caplog.messages == [
130
- "Initializing environment for https://github.com/tox-dev/pyproject-fmt.",
131
- "Installing environment for https://github.com/tox-dev/pyproject-fmt.",
132
- "Once installed this environment will be reused.",
133
- "This may take a few minutes...",
134
- f"Using pre-commit with uv {uv} via pre-commit-uv {self}",
135
- ]
136
-
137
-
138
- test_install_with_uv_config_raises_error_cases: list[tuple[str, str]] = [
139
- (
140
- "pyproject.toml",
141
- """
142
- [[tool.uv.index]]
143
- name = "internal"
144
- url = "https://pypi.example/simple/"
145
- default = true
146
- """,
147
- ),
148
- (
149
- "uv.toml",
150
- """
151
- [[index]]
152
- name = "internal"
153
- url = "https://pypi.example/simple/"
154
- default = true
155
- """,
156
- ),
157
- ]
158
-
159
-
160
- @pytest.mark.parametrize(("file_name", "content"), test_install_with_uv_config_raises_error_cases)
161
- def test_install_with_uv_config_raises_error(
162
- git_repo: Path,
163
- monkeypatch: pytest.MonkeyPatch,
164
- file_name: str,
165
- content: str,
166
- ) -> None:
167
- """Test to make sure that uv config is used for non default pypi repos."""
168
- (git_repo / file_name).write_text(dedent(content))
169
-
170
- monkeypatch.setenv("FORCE_PRE_COMMIT_UV_PATCH", "1")
171
-
172
- import pre_commit_uv # ruff:ignore[import-outside-top-level]
173
-
174
- pre_commit_uv._patch() # ruff:ignore[private-member-access]
175
-
176
- # would raise SystemExit due to bad config
177
- with pytest.raises(SystemExit):
178
- main.main(["install-hooks", "-c", str(git_repo / precommit_file)])
File without changes
File without changes
File without changes
File without changes