diff --git a/docs/changelog/+wildcard-ordered.bugfix.rst b/docs/changelog/+wildcard-ordered.bugfix.rst new file mode 100644 index 0000000..4e62e15 --- /dev/null +++ b/docs/changelog/+wildcard-ordered.bugfix.rst @@ -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.*``. diff --git a/docs/explanation.rst b/docs/explanation.rst index bbc39f0..403f532 100644 --- a/docs/explanation.rst +++ b/docs/explanation.rst @@ -419,8 +419,9 @@ On Windows, a literal executable name on ``PATH`` may include ``.exe``, such as `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. diff --git a/src/python_discovery/_py_spec.py b/src/python_discovery/_py_spec.py index 9d3d641..56af34f 100644 --- a/src/python_discovery/_py_spec.py +++ b/src/python_discovery/_py_spec.py @@ -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( diff --git a/tests/test_py_spec.py b/tests/test_py_spec.py index 2b589d6..50b2a5c 100644 --- a/tests/test_py_spec.py +++ b/tests/test_py_spec.py @@ -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: @@ -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: