Skip to content

Commit 97f2fb2

Browse files
authored
Add _get_settings() helper function (#13672)
1 parent 2b8f6da commit 97f2fb2

File tree

10 files changed

+47
-97
lines changed

10 files changed

+47
-97
lines changed

sphinx/__init__.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,12 @@
55

66
from __future__ import annotations
77

8-
import warnings
9-
108
from sphinx.util._pathlib import _StrPath
119

1210
TYPE_CHECKING = False
1311
if TYPE_CHECKING:
1412
from typing import Final
1513

16-
warnings.filterwarnings(
17-
'ignore',
18-
'The frontend.Option class .*',
19-
DeprecationWarning,
20-
module='docutils.frontend',
21-
)
22-
del warnings
23-
2414
__version__: Final = '8.3.0'
2515
__display_version__: Final = __version__ # used for command line version
2616

sphinx/builders/html/__init__.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import re
1111
import shutil
1212
import sys
13-
import warnings
1413
from pathlib import Path
1514
from types import NoneType
1615
from typing import TYPE_CHECKING
@@ -21,7 +20,6 @@
2120
import jinja2.exceptions
2221
from docutils import nodes
2322
from docutils.core import Publisher
24-
from docutils.frontend import OptionParser
2523
from docutils.io import DocTreeInput, StringOutput
2624

2725
from sphinx import __display_version__, package_dir
@@ -50,7 +48,7 @@
5048
from sphinx.util._timestamps import _format_rfc3339_microseconds
5149
from sphinx.util._uri import is_url
5250
from sphinx.util.display import progress_message, status_iterator
53-
from sphinx.util.docutils import new_document
51+
from sphinx.util.docutils import _get_settings, new_document
5452
from sphinx.util.fileutil import copy_asset
5553
from sphinx.util.i18n import format_date
5654
from sphinx.util.inventory import InventoryFile
@@ -459,15 +457,9 @@ def prepare_writing(self, docnames: Set[str]) -> None:
459457
self.load_indexer(docnames)
460458

461459
self.docwriter = HTMLWriter(self)
462-
with warnings.catch_warnings():
463-
warnings.filterwarnings('ignore', category=DeprecationWarning)
464-
# DeprecationWarning: The frontend.OptionParser class will be replaced
465-
# by a subclass of argparse.ArgumentParser in Docutils 0.21 or later.
466-
self.docsettings: Any = OptionParser(
467-
defaults=self.env.settings,
468-
components=(self.docwriter,),
469-
read_config_files=True,
470-
).get_default_values()
460+
self.docsettings = _get_settings(
461+
HTMLWriter, defaults=self.env.settings, read_config_files=True
462+
)
471463
self.docsettings.compact_lists = bool(self.config.html_compact_lists)
472464

473465
# determine the additional indices to include

sphinx/builders/latex/__init__.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44

55
import os
66
import os.path
7-
import warnings
87
from pathlib import Path
98
from typing import TYPE_CHECKING
109

11-
from docutils.frontend import OptionParser
12-
1310
import sphinx.builders.latex.nodes # NoQA: F401 # Workaround: import this before writer to avoid ImportError
1411
from sphinx import addnodes, highlighting, package_dir
1512
from sphinx._cli.util.colour import darkgreen
@@ -27,7 +24,7 @@
2724
from sphinx.locale import _, __
2825
from sphinx.util import logging, texescape
2926
from sphinx.util.display import progress_message, status_iterator
30-
from sphinx.util.docutils import SphinxFileOutput, new_document
27+
from sphinx.util.docutils import SphinxFileOutput, _get_settings, new_document
3128
from sphinx.util.fileutil import copy_asset_file
3229
from sphinx.util.i18n import format_date
3330
from sphinx.util.nodes import inline_all_toctrees
@@ -301,15 +298,9 @@ def copy_assets(self) -> None:
301298

302299
def write_documents(self, _docnames: Set[str]) -> None:
303300
docwriter = LaTeXWriter(self)
304-
with warnings.catch_warnings():
305-
warnings.filterwarnings('ignore', category=DeprecationWarning)
306-
# DeprecationWarning: The frontend.OptionParser class will be replaced
307-
# by a subclass of argparse.ArgumentParser in Docutils 0.21 or later.
308-
docsettings: Any = OptionParser(
309-
defaults=self.env.settings,
310-
components=(docwriter,),
311-
read_config_files=True,
312-
).get_default_values()
301+
docsettings = _get_settings(
302+
LaTeXWriter, defaults=self.env.settings, read_config_files=True
303+
)
313304

314305
for entry in self.document_data:
315306
docname, targetname, title, author, themename = entry[:5]

sphinx/builders/manpage.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
from __future__ import annotations
44

5-
import warnings
65
from typing import TYPE_CHECKING
76

8-
from docutils.frontend import OptionParser
97
from docutils.io import FileOutput
108

119
from sphinx import addnodes
@@ -14,13 +12,13 @@
1412
from sphinx.locale import __
1513
from sphinx.util import logging
1614
from sphinx.util.display import progress_message
15+
from sphinx.util.docutils import _get_settings
1716
from sphinx.util.nodes import inline_all_toctrees
1817
from sphinx.util.osutil import ensuredir, make_filename_from_project
1918
from sphinx.writers.manpage import ManualPageTranslator, ManualPageWriter
2019

2120
if TYPE_CHECKING:
2221
from collections.abc import Set
23-
from typing import Any
2422

2523
from sphinx.application import Sphinx
2624
from sphinx.config import Config
@@ -54,15 +52,9 @@ def get_target_uri(self, docname: str, typ: str | None = None) -> str:
5452
@progress_message(__('writing'))
5553
def write_documents(self, _docnames: Set[str]) -> None:
5654
docwriter = ManualPageWriter(self)
57-
with warnings.catch_warnings():
58-
warnings.filterwarnings('ignore', category=DeprecationWarning)
59-
# DeprecationWarning: The frontend.OptionParser class will be replaced
60-
# by a subclass of argparse.ArgumentParser in Docutils 0.21 or later.
61-
docsettings: Any = OptionParser(
62-
defaults=self.env.settings,
63-
components=(docwriter,),
64-
read_config_files=True,
65-
).get_default_values()
55+
docsettings = _get_settings(
56+
ManualPageWriter, defaults=self.env.settings, read_config_files=True
57+
)
6658

6759
for info in self.config.man_pages:
6860
docname, name, description, authors, section = info

sphinx/builders/texinfo.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@
33
from __future__ import annotations
44

55
import os.path
6-
import warnings
76
from typing import TYPE_CHECKING
87

98
from docutils import nodes
10-
from docutils.frontend import OptionParser
119
from docutils.io import FileOutput
1210

1311
from sphinx import addnodes, package_dir
@@ -18,14 +16,13 @@
1816
from sphinx.locale import _, __
1917
from sphinx.util import logging
2018
from sphinx.util.display import progress_message, status_iterator
21-
from sphinx.util.docutils import new_document
19+
from sphinx.util.docutils import _get_settings, new_document
2220
from sphinx.util.nodes import inline_all_toctrees
2321
from sphinx.util.osutil import SEP, copyfile, ensuredir, make_filename_from_project
2422
from sphinx.writers.texinfo import TexinfoTranslator, TexinfoWriter
2523

2624
if TYPE_CHECKING:
2725
from collections.abc import Iterable, Set
28-
from typing import Any
2926

3027
from docutils.nodes import Node
3128

@@ -119,15 +116,9 @@ def write_documents(self, _docnames: Set[str]) -> None:
119116
with progress_message(__('writing')):
120117
self.post_process_images(doctree)
121118
docwriter = TexinfoWriter(self)
122-
with warnings.catch_warnings():
123-
warnings.filterwarnings('ignore', category=DeprecationWarning)
124-
# DeprecationWarning: The frontend.OptionParser class will be replaced
125-
# by a subclass of argparse.ArgumentParser in Docutils 0.21 or later.
126-
settings: Any = OptionParser(
127-
defaults=self.env.settings,
128-
components=(docwriter,),
129-
read_config_files=True,
130-
).get_default_values()
119+
settings = _get_settings(
120+
TexinfoWriter, defaults=self.env.settings, read_config_files=True
121+
)
131122
settings.author = author
132123
settings.title = title
133124
settings.texinfo_filename = targetname[:-5] + '.info'

sphinx/testing/restructuredtext.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
from __future__ import annotations
22

3-
import warnings
43
from typing import TYPE_CHECKING
54

65
from docutils import nodes
7-
from docutils.frontend import OptionParser
86

97
from sphinx.io import SphinxBaseReader
108
from sphinx.parsers import RSTParser
119
from sphinx.transforms import SphinxTransformer
12-
from sphinx.util.docutils import LoggingReporter, sphinx_domains
10+
from sphinx.util.docutils import LoggingReporter, _get_settings, sphinx_domains
1311

1412
if TYPE_CHECKING:
15-
from docutils.frontend import Values
16-
1713
from sphinx.application import Sphinx
1814

1915

@@ -33,12 +29,7 @@ def parse(app: Sphinx, text: str, docname: str = 'index') -> nodes.document:
3329
'output_encoding': 'unicode',
3430
'traceback': True,
3531
}
36-
with warnings.catch_warnings():
37-
warnings.filterwarnings('ignore', category=DeprecationWarning)
38-
option_parser = OptionParser(
39-
components=(RSTParser, SphinxBaseReader), defaults=settings_overrides
40-
)
41-
settings: Values = option_parser.get_default_values() # type: ignore[assignment]
32+
settings = _get_settings(SphinxBaseReader, RSTParser, defaults=settings_overrides)
4233
settings._source = source_path
4334
settings.env = env
4435

sphinx/util/docutils.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
import os
66
import re
7+
import warnings
78
from contextlib import contextmanager
89
from copy import copy
910
from pathlib import Path
1011
from typing import TYPE_CHECKING
1112

1213
import docutils
1314
from docutils import nodes
15+
from docutils.frontend import OptionParser
1416
from docutils.io import FileOutput
1517
from docutils.parsers.rst import Directive, directives, roles
1618
from docutils.statemachine import StateMachine
@@ -27,10 +29,11 @@
2729
)
2830

2931
if TYPE_CHECKING:
30-
from collections.abc import Iterator, Sequence
32+
from collections.abc import Iterator, Mapping, Sequence
3133
from types import ModuleType, TracebackType
3234
from typing import Any, Protocol
3335

36+
from docutils import Component
3437
from docutils.frontend import Values
3538
from docutils.nodes import Element, Node, system_message
3639
from docutils.parsers.rst.states import Inliner
@@ -816,3 +819,21 @@ def new_document(source_path: str, settings: Any = None) -> nodes.document:
816819
document = nodes.document(settings, reporter, source=source_path)
817820
document.note_source(source_path, -1)
818821
return document
822+
823+
824+
def _get_settings(
825+
*components: Component | type[Component],
826+
defaults: Mapping[str, Any],
827+
read_config_files: bool = False,
828+
) -> Values:
829+
with warnings.catch_warnings(action='ignore', category=DeprecationWarning):
830+
# DeprecationWarning: The frontend.OptionParser class will be replaced
831+
# by a subclass of argparse.ArgumentParser in Docutils 0.21 or later.
832+
# DeprecationWarning: The frontend.Option class will be removed
833+
# in Docutils 0.21 or later.
834+
option_parser = OptionParser(
835+
components=components,
836+
defaults=defaults,
837+
read_config_files=read_config_files,
838+
)
839+
return option_parser.get_default_values() # type: ignore[return-value]

tests/test_markup/test_markup.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
from __future__ import annotations
44

55
import re
6-
import warnings
76
from types import SimpleNamespace
87

98
import pytest
10-
from docutils import frontend, nodes, utils
9+
from docutils import nodes, utils
1110
from docutils.parsers.rst import Parser as RstParser
1211

1312
from sphinx import addnodes
@@ -17,7 +16,7 @@
1716
from sphinx.testing.util import assert_node
1817
from sphinx.transforms import SphinxSmartQuotes
1918
from sphinx.util import texescape
20-
from sphinx.util.docutils import sphinx_domains
19+
from sphinx.util.docutils import _get_settings, sphinx_domains
2120
from sphinx.writers.html import HTMLWriter
2221
from sphinx.writers.html5 import HTML5Translator
2322
from sphinx.writers.latex import LaTeXTranslator, LaTeXWriter
@@ -27,15 +26,9 @@
2726
def settings(app):
2827
env = app.env
2928
texescape.init() # otherwise done by the latex builder
30-
with warnings.catch_warnings():
31-
warnings.filterwarnings('ignore', category=DeprecationWarning)
32-
# DeprecationWarning: The frontend.OptionParser class will be replaced
33-
# by a subclass of argparse.ArgumentParser in Docutils 0.21 or later.
34-
optparser = frontend.OptionParser(
35-
components=(RstParser, HTMLWriter, LaTeXWriter),
36-
defaults=default_settings,
37-
)
38-
settings = optparser.get_default_values()
29+
settings = _get_settings(
30+
RstParser, HTMLWriter, LaTeXWriter, defaults=default_settings
31+
)
3932
settings.smart_quotes = True
4033
settings.env = env
4134
settings.env.current_document.docname = 'dummy'

tests/test_search.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from __future__ import annotations
44

55
import json
6-
import warnings
76
from io import BytesIO
87
from typing import TYPE_CHECKING
98

@@ -169,12 +168,7 @@ def test_term_in_raw_directive(app: SphinxTestApp) -> None:
169168

170169

171170
def test_IndexBuilder():
172-
with warnings.catch_warnings():
173-
warnings.filterwarnings('ignore', category=DeprecationWarning)
174-
# DeprecationWarning: The frontend.OptionParser class will be replaced
175-
# by a subclass of argparse.ArgumentParser in Docutils 0.21 or later.
176-
optparser = frontend.OptionParser(components=(rst.Parser,))
177-
settings = optparser.get_default_values()
171+
settings = frontend.get_default_settings(rst.Parser)
178172
parser = rst.Parser()
179173

180174
domain1 = DummyDomain(

tests/test_util/test_util_nodes.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
from __future__ import annotations
44

5-
import warnings
65
from textwrap import dedent
76
from typing import TYPE_CHECKING, Any
87

@@ -30,11 +29,7 @@ def _transform(doctree) -> None:
3029

3130

3231
def create_new_document() -> document:
33-
with warnings.catch_warnings():
34-
warnings.filterwarnings('ignore', category=DeprecationWarning)
35-
# DeprecationWarning: The frontend.OptionParser class will be replaced
36-
# by a subclass of argparse.ArgumentParser in Docutils 0.21 or later.
37-
settings = frontend.OptionParser(components=(rst.Parser,)).get_default_values()
32+
settings = frontend.get_default_settings(rst.Parser)
3833
settings.id_prefix = 'id'
3934
document = new_document('dummy.txt', settings)
4035
return document

0 commit comments

Comments
 (0)