Skip to content

Doctest :pyversion: raises InvalidVersion at collection #86

Description

@tony

Summary

:pyversion: on a .. doctest:: block has never worked. The call site passes its two arguments in the opposite order to the signature, so packaging is asked to parse the specifier as a version and raises InvalidVersion — which the surrounding except InvalidSpecifier does not catch. The exception escapes the directive, so collecting any page that uses the option aborts.

Reproduction

pyver.rst:

Title
=====

.. doctest::
   :pyversion: >=3.10

   >>> 1 + 1
   2
$ pytest pyver.rst -q --no-header

Expected

The block runs on Python 3.10 and newer, and is marked SKIP below that.

Actual

InvalidVersion: Invalid version: '>=3.10'

Collection of the page fails outright.

Versions

gp-libs v0.0.19, CPython 3.14, packaging 25.0.

Root cause

gp-libs defines is_allowed_version(version, spec), the reverse of the Sphinx function it was ported from, which is is_allowed_version(spec, version). The docstring examples were updated to the new order and pass:

>>> is_allowed_version('3.3', '<=3.5')
True

The call site kept Sphinx's order:

if not is_allowed_version(spec, python_version):

so it evaluates Version(">=3.10") in SpecifierSet("3.14.2"). Version(">=3.10") raises InvalidVersion, and InvalidVersion is not a subclass of InvalidSpecifier, so the except clause below it never runs.

Proposal

Swap the arguments at the call site to match the local signature, and widen the handler to except (InvalidSpecifier, InvalidVersion) so a genuinely malformed specifier still becomes a reporter warning rather than a traceback. Leave the public signature alone.

Note that this option cannot work today even once the exception is fixed: node["options"] is written but never read, which is #84.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions