From c73628dfcac844f89198ccd805e8e35609b37636 Mon Sep 17 00:00:00 2001 From: Adam Turner <9087854+aa-turner@users.noreply.github.com> Date: Thu, 11 May 2023 13:34:09 +0100 Subject: [PATCH] Accept a version tuple in ``app.require_sphinx()`` --- CHANGES | 2 ++ sphinx/application.py | 16 ++++++++++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/CHANGES b/CHANGES index 7adb7cad583..9ac823cdb4c 100644 --- a/CHANGES +++ b/CHANGES @@ -24,6 +24,8 @@ Features added * #11415: Add a checksum to JavaScript and CSS asset URIs included within generated HTML, using the CRC32 algorithm. +* :meth:`~sphinx.application.Sphinx.require_sphinx` now allows the version + requirement to be specified as ``(major, minor)``. Bugs fixed ---------- diff --git a/sphinx/application.py b/sphinx/application.py index 67b32269303..495e905704d 100644 --- a/sphinx/application.py +++ b/sphinx/application.py @@ -401,18 +401,26 @@ def setup_extension(self, extname: str) -> None: logger.debug('[app] setting up extension: %r', extname) self.registry.load_extension(self, extname) - def require_sphinx(self, version: str) -> None: + @staticmethod + def require_sphinx(version: tuple[int, int] | str) -> None: """Check the Sphinx version if requested. Compare *version* with the version of the running Sphinx, and abort the build when it is too old. - :param version: The required version in the form of ``major.minor``. + :param version: The required version in the form of ``major.minor`` or + ``(major, minor)``. .. versionadded:: 1.0 + .. versionchanged:: 7.1 + Type of *version* now allows ``(major, minor)`` form. """ - if version > sphinx.__display_version__[:3]: - raise VersionRequirementError(version) + if isinstance(version, tuple): + major, minor = version + else: + major, minor = map(int, version.split('.')[:2]) + if (major, minor) > sphinx.version_info[:2]: + raise VersionRequirementError(f'{major}.{minor}') # event interface def connect(self, event: str, callback: Callable, priority: int = 500) -> int: