Skip to content

Commit 17fa2fb

Browse files
FazeelUsmaniclaude
andcommitted
Un-deprecate Parser.config and Parser.env
PRs #13644 and #13637 deprecated ``Parser.config``, ``Parser.env``, and ``Parser.set_application`` in 9.0, scheduled for removal in 10.0. However, the registry already populates ``_config`` and ``_env`` directly when constructing parser instances, and there is no documented public replacement for the property accessors. Third-party parsers such as ``sphinx_bib_domain`` rely on reading config/env during ``parse()``, and projects using ``-W error`` in CI now fail outright on the deprecation warnings. Reverse the deprecation of ``Parser.config`` and ``Parser.env`` so that they remain the supported way for parsers to access the build configuration and environment. ``Parser.set_application`` is kept deprecated since the hook is genuinely redundant once ``_config`` and ``_env`` are set by the registry. Document the supported access pattern in ``doc/extdev/parserapi.rst`` and add a regression test that asserts no ``RemovedInSphinx10Warning`` is emitted on attribute read. Fixes #14371 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent cc7c6f4 commit 17fa2fb

5 files changed

Lines changed: 74 additions & 8 deletions

File tree

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ Contributors
5656
* Erik Bedard -- config options for :mod:`sphinx.ext.duration`
5757
* Etienne Desautels -- apidoc module
5858
* Ezio Melotti -- collapsible sidebar JavaScript
59+
* Fazeel Usmani -- parser API fixes
5960
* Filip Vavera -- napoleon todo directive
6061
* Florian Best -- log improvements
6162
* Glenn Matthews -- python domain signature improvements

CHANGES.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
Release 9.2.0 (in development)
2+
==============================
3+
4+
Bugs fixed
5+
----------
6+
7+
* #14371: Un-deprecate :py:attr:`!Parser.config` and :py:attr:`!Parser.env`,
8+
the supported way for a custom parser to access the build configuration
9+
and environment. Only :py:meth:`!Parser.set_application` remains deprecated.
10+
Patch by Fazeel Usmani
11+
112
Release 9.1.0 (released Dec 31, 2025)
213
=====================================
314

doc/extdev/parserapi.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,33 @@ to configure their settings appropriately.
3636

3737
.. autoclass:: Parser
3838
:members:
39+
40+
Accessing the Sphinx config and environment
41+
-------------------------------------------
42+
43+
A custom parser can read Sphinx :class:`~sphinx.config.Config` values and the
44+
:class:`~sphinx.environment.BuildEnvironment` through the inherited
45+
:attr:`Parser.config` and :attr:`Parser.env` properties. Sphinx sets the
46+
backing ``_config`` and ``_env`` attributes when it constructs the parser via
47+
:meth:`.Sphinx.add_source_parser`, so they are available from the moment
48+
:meth:`Parser.parse` is called.
49+
50+
.. code-block:: python
51+
52+
from sphinx.parsers import Parser
53+
54+
55+
class MyParser(Parser):
56+
supported = ('mytype',)
57+
58+
def parse(self, inputstring, document):
59+
# ``self.config`` and ``self.env`` are populated by Sphinx
60+
encoding = self.config.source_encoding
61+
docname = self.env.docname
62+
...
63+
64+
.. versionchanged:: 9.0
65+
The :meth:`Parser.set_application` hook is deprecated. Sphinx now sets
66+
``_config`` and ``_env`` directly when the parser instance is created, so
67+
custom parsers no longer need to override ``set_application`` to capture
68+
these objects.

sphinx/parsers.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,33 @@ class Parser(docutils.parsers.Parser):
3636

3737
@property
3838
def config(self) -> Config:
39-
"""The config object."""
40-
cls_module = self.__class__.__module__
41-
cls_name = self.__class__.__qualname__
42-
_deprecation_warning(cls_module, f'{cls_name}.config', remove=(10, 0))
39+
"""The config object.
40+
41+
Sphinx sets this attribute when constructing the parser via
42+
:meth:`.Sphinx.add_source_parser`, so it is available throughout
43+
:meth:`parse`.
44+
"""
4345
return self._config
4446

4547
@property
4648
def env(self) -> BuildEnvironment:
47-
"""The environment object."""
48-
cls_module = self.__class__.__module__
49-
cls_name = self.__class__.__qualname__
50-
_deprecation_warning(cls_module, f'{cls_name}.env', remove=(10, 0))
49+
"""The environment object.
50+
51+
Sphinx sets this attribute when constructing the parser via
52+
:meth:`.Sphinx.add_source_parser`, so it is available throughout
53+
:meth:`parse`.
54+
"""
5155
return self._env
5256

5357
def set_application(self, app: Sphinx) -> None:
5458
"""set_application will be called from Sphinx to set app and other instance variables
5559
5660
:param sphinx.application.Sphinx app: Sphinx application object
61+
62+
.. versionchanged:: 9.0
63+
Deprecated. Sphinx now sets :attr:`_config` and :attr:`_env`
64+
directly when the parser is created, so this hook is no longer
65+
needed. It will be removed in Sphinx 10.
5766
"""
5867
cls_module = self.__class__.__module__
5968
cls_name = self.__class__.__qualname__

tests/test_markup/test_parser.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
from __future__ import annotations
44

5+
import warnings
56
from typing import TYPE_CHECKING
67
from unittest.mock import Mock, patch
78

89
import pytest
910

11+
from sphinx.deprecation import RemovedInSphinx10Warning
1012
from sphinx.parsers import RSTParser
1113
from sphinx.util.docutils import new_document
1214

@@ -68,3 +70,16 @@ def test_RSTParser_prolog_epilog(RSTStateMachine: Mock, app: SphinxTestApp) -> N
6870
('dummy.rst', 0, ' hello Sphinx world'),
6971
('dummy.rst', 1, ' Sphinx is a document generator'),
7072
]
73+
74+
75+
@pytest.mark.sphinx('html', testroot='basic')
76+
def test_parser_config_env_not_deprecated(app: SphinxTestApp) -> None:
77+
"""Reading ``Parser.config`` / ``Parser.env`` must not warn (#14371)."""
78+
parser = RSTParser()
79+
parser._config = app.config
80+
parser._env = app.env
81+
82+
with warnings.catch_warnings():
83+
warnings.simplefilter('error', RemovedInSphinx10Warning)
84+
assert parser.config is app.config
85+
assert parser.env is app.env

0 commit comments

Comments
 (0)