From e6de21d3d8823fd397693062d44622ef1c6e0199 Mon Sep 17 00:00:00 2001 From: mansur-akhmadi <10739460+mansur-akhmadi@users.noreply.github.com> Date: Thu, 19 Nov 2026 03:31:31 +0700 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9B=20fix(spec):=20reject=20wildca?= =?UTF-8?q?rds=20that=20PEP=20440=20forbids?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/changelog/+wildcard-ordered.bugfix.rst | 3 +++ src/python_discovery/_py_spec.py | 9 +++++++-- tests/test_py_spec.py | 12 ++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 docs/changelog/+wildcard-ordered.bugfix.rst diff --git a/docs/changelog/+wildcard-ordered.bugfix.rst b/docs/changelog/+wildcard-ordered.bugfix.rst new file mode 100644 index 0000000..84547ce --- /dev/null +++ b/docs/changelog/+wildcard-ordered.bugfix.rst @@ -0,0 +1,3 @@ +:class:`~python_discovery.PythonSpec` now raises :exc:`ValueError` for a ``.*`` wildcard that PEP 440 does not allow, +on an operator other than ``==`` and ``!=`` (``>=3.12.*``) or after a pre-release (``==3.13rc1.*``), instead of +accepting a specifier that matches no interpreter or every 3.13 one. diff --git a/src/python_discovery/_py_spec.py b/src/python_discovery/_py_spec.py index 9d3d641..8925ec7 100644 --- a/src/python_discovery/_py_spec.py +++ b/src/python_discovery/_py_spec.py @@ -299,9 +299,14 @@ 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 + # the set never raises: it keeps an unparsable version as None, drops items without an operator, and accepts a + # ``.*`` on any operator or pre-release, though PEP 440 allows it only on == and != after a final release 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: From c701cd0f2e5750a72f27c5e6c44bc6ca1a26f6b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Mon, 23 Nov 2026 10:15:49 -0800 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=93=9D=20docs(spec):=20clarify=20wild?= =?UTF-8?q?card=20restrictions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/changelog/+wildcard-ordered.bugfix.rst | 5 ++--- docs/explanation.rst | 5 +++-- src/python_discovery/_py_spec.py | 3 +-- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/docs/changelog/+wildcard-ordered.bugfix.rst b/docs/changelog/+wildcard-ordered.bugfix.rst index 84547ce..4e62e15 100644 --- a/docs/changelog/+wildcard-ordered.bugfix.rst +++ b/docs/changelog/+wildcard-ordered.bugfix.rst @@ -1,3 +1,2 @@ -:class:`~python_discovery.PythonSpec` now raises :exc:`ValueError` for a ``.*`` wildcard that PEP 440 does not allow, -on an operator other than ``==`` and ``!=`` (``>=3.12.*``) or after a pre-release (``==3.13rc1.*``), instead of -accepting a specifier that matches no interpreter or every 3.13 one. +: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 8925ec7..56af34f 100644 --- a/src/python_discovery/_py_spec.py +++ b/src/python_discovery/_py_spec.py @@ -299,8 +299,7 @@ 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, drops items without an operator, and accepts a - # ``.*`` on any operator or pre-release, though PEP 440 allows it only on == and != after a final release + # 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