Skip to content

Commit eaffcc0

Browse files
committed
Add _constraints_to_uv: turn resolved constraints into uv array items
1 parent cddfab8 commit eaffcc0

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

‎src/mxdev/uv.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,36 @@
1515
logger = logging.getLogger("mxdev")
1616

1717

18+
def _constraints_to_uv(constraints: list[str]) -> list[tuple[str, str]]:
19+
"""Turn resolved constraint lines into ordered uv array items.
20+
21+
Mirrors ``constraints-mxdev.txt`` into TOML-array form: specifier lines
22+
become ``("entry", specifier)`` and comment lines become
23+
``("comment", text)``, preserving source order. Decorative ``####`` rules,
24+
blank lines, and non-PEP-508 lines (e.g. ``--hash``) are dropped.
25+
"""
26+
from packaging.requirements import Requirement
27+
28+
items: list[tuple[str, str]] = []
29+
for raw in constraints:
30+
stripped = raw.strip()
31+
if not stripped:
32+
continue
33+
# Decorative full-width rule (line consisting only of '#').
34+
if set(stripped) == {"#"}:
35+
continue
36+
if stripped.startswith("#"):
37+
items.append(("comment", stripped.lstrip("#").strip()))
38+
continue
39+
try:
40+
Requirement(stripped)
41+
except Exception:
42+
logger.debug("[uv] Skipping non-PEP-508 constraint line: %s", stripped)
43+
continue
44+
items.append(("entry", stripped))
45+
return items
46+
47+
1848
class UvPyprojectUpdater(Hook):
1949
"""An mxdev hook that updates pyproject.toml during the write phase for uv-managed projects."""
2050

‎tests/test_uv.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from mxdev.config import Configuration
22
from mxdev.state import State
3+
from mxdev.uv import _constraints_to_uv
34
from mxdev.uv import UvPyprojectUpdater
45

56
import pytest
@@ -459,3 +460,30 @@ def fake_import(name, *args, **kw):
459460

460461
# Should not raise any error, even though tomlkit import is mocked to fail
461462
hook.write(state)
463+
464+
465+
def test_constraints_to_uv_filters_and_preserves_order():
466+
constraints = [
467+
"#" * 79 + "\n",
468+
"# begin constraints from: https://example.com/a.txt\n",
469+
"\n",
470+
"Zope==6.0\n",
471+
"# AccessControl==7.3 -> mxdev disabled (source)\n",
472+
'backports.tarfile==1.2.0 ; python_version < "3.12"\n',
473+
"--hash=sha256:deadbeef\n",
474+
"# end constraints from: https://example.com/a.txt\n",
475+
"#" * 79 + "\n",
476+
]
477+
result = _constraints_to_uv(constraints)
478+
assert result == [
479+
("comment", "begin constraints from: https://example.com/a.txt"),
480+
("entry", "Zope==6.0"),
481+
("comment", "AccessControl==7.3 -> mxdev disabled (source)"),
482+
("entry", 'backports.tarfile==1.2.0 ; python_version < "3.12"'),
483+
("comment", "end constraints from: https://example.com/a.txt"),
484+
]
485+
486+
487+
def test_constraints_to_uv_empty_input():
488+
assert _constraints_to_uv([]) == []
489+
assert _constraints_to_uv(["\n", "#" * 79 + "\n", " \n"]) == []

0 commit comments

Comments
 (0)