Skip to content

Commit bb5e545

Browse files
authored
Deprecate Parser.{config,env} (#13644)
1 parent 58ebe2d commit bb5e545

File tree

5 files changed

+32
-21
lines changed

5 files changed

+32
-21
lines changed

CHANGES.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ Deprecated
2020
Patch by Adam Turner.
2121
* #13637: Deprecate the :py:meth:`!set_application` method
2222
of :py:class:`~sphinx.parsers.Parser` objects.
23-
Sphinx now directly sets the :py:attr:`!config` and :py:attr:`!env` attributes.
23+
Patch by Adam Turner.
24+
* #13644: Deprecate the :py:attr:`!Parser.config` and :py:attr:`!env` attributes.
2425
Patch by Adam Turner.
2526

2627
Features added

sphinx/parsers.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,31 @@
2424

2525

2626
class Parser(docutils.parsers.Parser):
27-
"""A base class of source parsers.
27+
"""A base class for source parsers.
2828
29-
The additional parsers should inherit this class
30-
instead of ``docutils.parsers.Parser``.
31-
Compared with ``docutils.parsers.Parser``,
32-
this class improves accessibility to Sphinx APIs.
33-
34-
The subclasses can access sphinx core runtime objects (app, config and env).
29+
Additional parsers should inherit from this class instead of
30+
``docutils.parsers.Parser``.
31+
This class provides access to core Sphinx objects; *config* and *env*.
3532
"""
3633

37-
#: The config object
38-
config: Config
34+
_config: Config
35+
_env: BuildEnvironment
36+
37+
@property
38+
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=(9, 0))
43+
return self._config
3944

40-
#: The environment object
41-
env: BuildEnvironment
45+
@property
46+
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=(9, 0))
51+
return self._env
4252

4353
def set_application(self, app: Sphinx) -> None:
4454
"""set_application will be called from Sphinx to set app and other instance variables
@@ -47,9 +57,9 @@ def set_application(self, app: Sphinx) -> None:
4757
"""
4858
cls_module = self.__class__.__module__
4959
cls_name = self.__class__.__qualname__
50-
_deprecation_warning(cls_module, f'{cls_name}.set_application', remove=(10, 0))
51-
self.config = app.config
52-
self.env = app.env
60+
_deprecation_warning(cls_module, f'{cls_name}.set_application', remove=(9, 0))
61+
self._config = app.config
62+
self._env = app.env
5363

5464

5565
class RSTParser(docutils.parsers.rst.Parser, Parser):

sphinx/registry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -381,8 +381,8 @@ def create_source_parser(
381381
parser_class = self.get_source_parser(filename)
382382
parser = parser_class()
383383
if isinstance(parser, SphinxParser):
384-
parser.config = config
385-
parser.env = env
384+
parser._config = config
385+
parser._env = env
386386
return parser
387387

388388
def add_translator(

sphinx/testing/restructuredtext.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ def parse(app: Sphinx, text: str, docname: str = 'index') -> nodes.document:
2222
reader = SphinxStandaloneReader()
2323
reader._setup_transforms(app.registry.get_transforms())
2424
parser = RSTParser()
25-
parser.config = app.config
26-
parser.env = app.env
25+
parser._config = app.config
26+
parser._env = app.env
2727
with sphinx_domains(env):
2828
return publish_doctree(
2929
text,

tests/test_markup/test_parser.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def test_RSTParser_prolog_epilog(RSTStateMachine, app):
1616
document = new_document('dummy.rst')
1717
document.settings = Mock(tab_width=8, language_code='')
1818
parser = RSTParser()
19-
parser.config = app.config
20-
parser.env = app.env
19+
parser._config = app.config
20+
parser._env = app.env
2121

2222
# normal case
2323
text = 'hello Sphinx world\nSphinx is a document generator'

0 commit comments

Comments
 (0)