Skip to content

Commit 77e9ed4

Browse files
authored
feat(tags): add pure Python tag generator (#1346)
Co-authored-by: r266-tech <[email protected]>
1 parent 7cea5e8 commit 77e9ed4

3 files changed

Lines changed: 63 additions & 2 deletions

File tree

‎docs/tags.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ to the implementation to provide.
9494

9595
.. autofunction:: compatible_tags
9696

97+
.. autofunction:: pure_python_tags
98+
9799
.. autofunction:: cpython_tags
98100

99101
.. autofunction:: generic_tags

‎src/packaging/tags.py‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"mac_platforms",
4747
"parse_tag",
4848
"platform_tags",
49+
"pure_python_tags",
4950
"sys_tags",
5051
]
5152

@@ -577,6 +578,30 @@ def _py_interpreter_range(py_version: PythonVersion) -> Iterator[str]:
577578
yield f"py{_version_nodot((py_version[0], minor))}"
578579

579580

581+
def pure_python_tags(
582+
python_version: PythonVersion | None = None,
583+
) -> Iterator[Tag]:
584+
"""
585+
Yields the pure-Python tags compatible with ``python_version``.
586+
587+
The tags use the ``"none"`` ABI and ``"any"`` platform, so their
588+
generation does not depend on the running platform.
589+
590+
.. versionadded:: 26.3
591+
592+
:param Sequence python_version: A one- or two-item sequence representing the
593+
compatible version of Python. Defaults to
594+
``sys.version_info[:2]``.
595+
:raises ValueError: If ``python_version`` is an empty sequence.
596+
"""
597+
if python_version is None:
598+
python_version = sys.version_info[:2]
599+
elif not python_version:
600+
raise ValueError("python_version must contain at least one item")
601+
for version in _py_interpreter_range(python_version):
602+
yield Tag(version, "none", "any")
603+
604+
580605
def compatible_tags(
581606
python_version: PythonVersion | None = None,
582607
interpreter: str | None = None,
@@ -610,8 +635,7 @@ def compatible_tags(
610635
yield Tag(version, "none", platform_)
611636
if interpreter:
612637
yield Tag(interpreter, "none", "any")
613-
for version in _py_interpreter_range(python_version):
614-
yield Tag(version, "none", "any")
638+
yield from pure_python_tags(python_version)
615639

616640

617641
def _mac_arch(arch: str, is_32bit: bool = _32_BIT_INTERPRETER) -> str:

‎tests/test_tags.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,41 @@ def test_platforms_default(self, monkeypatch: pytest.MonkeyPatch) -> None:
15091509
assert result == [tags.Tag("sillywalk", "none", "plat")]
15101510

15111511

1512+
class TestPurePythonTags:
1513+
def test_python_version(self) -> None:
1514+
result = list(tags.pure_python_tags((3, 3)))
1515+
assert result == [
1516+
tags.Tag("py33", "none", "any"),
1517+
tags.Tag("py3", "none", "any"),
1518+
tags.Tag("py32", "none", "any"),
1519+
tags.Tag("py31", "none", "any"),
1520+
tags.Tag("py30", "none", "any"),
1521+
]
1522+
1523+
def test_major_only_python_version(self) -> None:
1524+
assert list(tags.pure_python_tags((3,))) == [tags.Tag("py3", "none", "any")]
1525+
1526+
def test_empty_python_version(self) -> None:
1527+
with pytest.raises(ValueError, match="must contain at least one item"):
1528+
list(tags.pure_python_tags(()))
1529+
1530+
def test_default_python_version(self, monkeypatch: pytest.MonkeyPatch) -> None:
1531+
monkeypatch.setattr(sys, "version_info", (3, 1))
1532+
assert list(tags.pure_python_tags()) == [
1533+
tags.Tag("py31", "none", "any"),
1534+
tags.Tag("py3", "none", "any"),
1535+
tags.Tag("py30", "none", "any"),
1536+
]
1537+
1538+
def test_does_not_query_platforms(self, monkeypatch: pytest.MonkeyPatch) -> None:
1539+
monkeypatch.setattr(
1540+
tags,
1541+
"platform_tags",
1542+
lambda: pytest.fail("pure_python_tags() queried the running platform"),
1543+
)
1544+
assert list(tags.pure_python_tags((3,))) == [tags.Tag("py3", "none", "any")]
1545+
1546+
15121547
class TestCompatibleTags:
15131548
def test_all_args(self) -> None:
15141549
result = list(tags.compatible_tags((3, 3), "cp33", ["plat1", "plat2"]))

0 commit comments

Comments
 (0)