diff --git a/CHANGES.rst b/CHANGES.rst index 26512c7c019..9388c43e5d3 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,8 @@ Release 8.0.1 (in development) Bugs fixed ---------- +* Patch ``pygments.Formatter.__class_getitem__`` in Pygments 2.17. + Patch by Adam Turner. Release 8.0.0 (released Jul 29, 2024) ===================================== diff --git a/sphinx/highlighting.py b/sphinx/highlighting.py index 899a82fe43e..c5ac536ff84 100644 --- a/sphinx/highlighting.py +++ b/sphinx/highlighting.py @@ -6,6 +6,7 @@ from importlib import import_module from typing import TYPE_CHECKING, Any +import pygments from pygments import highlight from pygments.filters import ErrorToken from pygments.formatters import HtmlFormatter, LatexFormatter @@ -30,6 +31,11 @@ from pygments.lexer import Lexer from pygments.style import Style +if tuple(map(int, pygments.__version__.split('.')))[:2] < (2, 18): + from pygments.formatter import Formatter + + Formatter.__class_getitem__ = lambda cls, name: cls + logger = logging.getLogger(__name__) lexers: dict[str, Lexer] = {} diff --git a/tests/test_highlighting.py b/tests/test_highlighting.py index e9564d1679c..7149cf13c7e 100644 --- a/tests/test_highlighting.py +++ b/tests/test_highlighting.py @@ -2,12 +2,17 @@ from unittest import mock +import pygments from pygments.formatters.html import HtmlFormatter from pygments.lexer import RegexLexer from pygments.token import Name, Text from sphinx.highlighting import PygmentsBridge +if tuple(map(int, pygments.__version__.split('.')))[:2] < (2, 18): + from pygments.formatter import Formatter + Formatter.__class_getitem__ = lambda cls, name: cls + class MyLexer(RegexLexer): name = 'testlexer'