Skip to content

Fix needs_extensions version comparison to use packaging.Version#14376

Closed
piyushhhxyz wants to merge 2 commits intosphinx-doc:masterfrom
piyushhhxyz:vorflux/fix-needs-extensions-version-comparison
Closed

Fix needs_extensions version comparison to use packaging.Version#14376
piyushhhxyz wants to merge 2 commits intosphinx-doc:masterfrom
piyushhhxyz:vorflux/fix-needs-extensions-version-comparison

Conversation

@piyushhhxyz
Copy link
Copy Markdown

Summary

Fix the needs_extensions check to compare versions using packaging.version.Version instead of string comparison.

Problem

The needs_extensions check compared version strings lexicographically, which produced incorrect results for versions with components >= 10. For example:

  • As strings: '0.6.0' > '0.10.0' evaluates to True (wrong)
  • As versions: Version('0.6.0') > Version('0.10.0') evaluates to False (correct)

This caused extensions like sphinx-gallery 0.10.0 to be rejected when 0.6.0 was the minimum required version.

Fix

Replace the string comparison reqversion > extension.version with Version(reqversion) > Version(extension.version) using packaging.version.Version, which is already a dependency of Sphinx.

Files Changed

  • sphinx/extension.py -- Use packaging.version.Version for proper semantic version comparison
  • tests/test_extension.py -- New test file with regression tests for the version comparison fix

Testing

Unit tests (new)

$ python3 -m pytest tests/test_extension.py -v
tests/test_extension.py::test_verify_needs_extensions_version_comparison PASSED
tests/test_extension.py::test_verify_needs_extensions_unknown_version PASSED
tests/test_extension.py::test_verify_needs_extensions_missing_extension PASSED
3 passed

Manual verification

>>> from packaging.version import Version
>>> Version('0.6.0') > Version('0.10.0')
False  # Correct! 0.6.0 < 0.10.0
>>> '0.6.0' > '0.10.0'
True   # Bug! String comparison is wrong

Existing tests

Ran tests/test_config.py -- all pre-existing failures are unrelated (sphinxcontrib.applehelp version mismatch with this old Sphinx version). Verified by running the same tests on the base commit without changes.

The needs_extensions check was comparing version strings lexicographically,
which caused incorrect results for versions with components >= 10.
For example, '0.6.0' > '0.10.0' as strings, but 0.6.0 < 0.10.0 as versions.

This fix uses packaging.version.Version for proper semantic version
comparison, which is already a dependency of Sphinx.
@piyushhhxyz piyushhhxyz deleted the vorflux/fix-needs-extensions-version-comparison branch April 10, 2026 01:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant