Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/changelog/+wildcard-ordered.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:class:`~python_discovery.PythonSpec` now raises :exc:`ValueError` for wildcards on operators other than ``==`` or
``!=``, such as ``>=3.12.*``, and after pre-release suffixes, such as ``==3.13rc1.*``.
5 changes: 3 additions & 2 deletions docs/explanation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,9 @@ On Windows, a literal executable name on ``PATH`` may include ``.exe``, such as
`Version specifiers <https://packaging.python.org/en/latest/specifications/version-specifiers/>`_
(``>=``, ``<=``, ``~=``, ``!=``, ``==``, ``===``) are supported. Multiple specifiers can be comma-separated,
for example ``>=3.11,<3.13``. Each version is a release of up to three numbers with an optional ``a``, ``b`` or
``rc`` pre-release suffix, such as ``3.12`` or ``3.13.0rc1``; ``==`` and ``!=`` also take a trailing ``.*``
wildcard, as in ``==3.12.*``.
``rc`` pre-release suffix, such as ``3.12`` or ``3.13.0rc1``. A trailing ``.*`` wildcard requires ``==`` or ``!=``
and a version with no pre-release suffix, as in ``==3.12.*``. Inputs such as ``>=3.12.*`` and ``==3.13rc1.*`` raise
:exc:`ValueError`.

The ABI flags, ``-arch`` and ``-machine`` parts described above follow the last specifier and apply to the whole set:
``>=3.13t`` asks for a free-threaded 3.13 or newer, ``cpython>=3.11,<3.14-64`` for a 64-bit CPython in that range.
Expand Down
8 changes: 6 additions & 2 deletions src/python_discovery/_py_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,13 @@ def _parse_specifier(string_spec: str) -> PythonSpec | None:
return None
spec_text = match["spec"].strip()
version_specifier = SpecifierSet.from_string(spec_text)
# the set never raises: it keeps an unparsable version as None and drops items without an operator
# Keep the public specifier set permissive for callers that use it directly.
items = [item for item in spec_text.split(",") if item.strip()]
if len(items) != len(version_specifier.specifiers) or any(spec.version is None for spec in version_specifier):
if len(items) != len(version_specifier.specifiers) or any(
spec.version is None
or (spec.is_wildcard and (spec.operator not in {"==", "!="} or spec.version.pre_type is not None))
for spec in version_specifier
):
msg = f"invalid version specifier {string_spec!r}"
raise ValueError(msg)
return _spec_from_match(
Expand Down
12 changes: 12 additions & 0 deletions tests/test_py_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ def test_bad_py_spec(text: str) -> None:
pytest.param(">=3.12,foo", id="item-without-operator"),
pytest.param(">=", id="bare-operator"),
pytest.param(">=3.13t,<3.15", id="modifier-before-last-item"),
pytest.param(">=3.12.*", id="wildcard-ge"),
pytest.param(">3.*", id="wildcard-gt"),
pytest.param("<=3.12.*", id="wildcard-le"),
pytest.param("<3.13.*", id="wildcard-lt"),
pytest.param("~=3.12.*", id="wildcard-compatible"),
pytest.param("===3.12.*", id="wildcard-arbitrary"),
pytest.param(">=3.11,<3.13.*", id="wildcard-ordered-last-item"),
pytest.param("cpython>=3.12.*t", id="wildcard-ordered-with-modifier"),
pytest.param("==3.13rc1.*", id="wildcard-eq-prerelease"),
pytest.param("!=3.13.0b1.*", id="wildcard-ne-prerelease"),
],
)
def test_invalid_specifier_raises(text: str) -> None:
Expand All @@ -72,6 +82,8 @@ def test_invalid_specifier_raises(text: str) -> None:
pytest.param(">=3.11,<3.14t", (">=3.11,<3.14", True, None, None, None), id="set"),
pytest.param("cpython>=3.12-64-arm64", (">=3.12", None, None, 64, "arm64"), id="arch-machine"),
pytest.param(">=3.13", (">=3.13", None, None, None, None), id="any-build"),
pytest.param("cpython==3.13.*t", ("==3.13.*", True, None, None, None), id="wildcard-eq"),
pytest.param("!=3.12.*-64", ("!=3.12.*", None, None, 64, None), id="wildcard-ne"),
],
)
def test_specifier_modifiers(text: str, expected: tuple[str, bool | None, bool | None, int | None, str | None]) -> None:
Expand Down