From ef700e7c2d020789b930abee15e4dd6a136ccab3 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Tue, 12 Dec 2023 12:25:46 +0000 Subject: [PATCH 1/3] MAINT: update for deprecated option parsing --- myst_parser/_docs.py | 18 +++++++++++++----- myst_parser/mdit_to_docutils/base.py | 11 +++++++++-- pyproject.toml | 2 ++ tests/test_docutils.py | 11 +++++++++-- tests/test_renderers/test_fixtures_docutils.py | 5 ++--- tests/test_renderers/test_myst_config.py | 6 ++---- 6 files changed, 37 insertions(+), 16 deletions(-) diff --git a/myst_parser/_docs.py b/myst_parser/_docs.py index 99900a06..964c5aaa 100644 --- a/myst_parser/_docs.py +++ b/myst_parser/_docs.py @@ -1,11 +1,12 @@ """Code to use internally, for documentation.""" from __future__ import annotations +import contextlib import io from typing import Sequence, Union, get_args, get_origin from docutils import nodes -from docutils.frontend import OptionParser +from docutils.core import Publisher from docutils.parsers.rst import directives from sphinx.directives import other from sphinx.transforms.post_transforms import SphinxPostTransform @@ -175,11 +176,18 @@ class DocutilsCliHelpDirective(SphinxDirective): def run(self): """Run the directive.""" + stream = io.StringIO() - OptionParser( - components=(DocutilsParser,), - usage="myst-docutils- [options] [ []]", - ).print_help(stream) + + pub = Publisher(parser=DocutilsParser()) + with contextlib.redirect_stdout(stream): + try: + pub.process_command_line( + ["--help"], + usage="myst-docutils- [options] [ []]", + ) + except SystemExit as exc: + assert not exc.code return [nodes.literal_block("", stream.getvalue())] diff --git a/myst_parser/mdit_to_docutils/base.py b/myst_parser/mdit_to_docutils/base.py index 93714f7d..4a02459d 100644 --- a/myst_parser/mdit_to_docutils/base.py +++ b/myst_parser/mdit_to_docutils/base.py @@ -22,10 +22,10 @@ ) from urllib.parse import urlparse +import docutils import jinja2 import yaml from docutils import nodes -from docutils.frontend import OptionParser from docutils.languages import get_language from docutils.parsers.rst import Directive, DirectiveError, directives, roles from docutils.parsers.rst import Parser as RSTParser @@ -63,7 +63,14 @@ def make_document(source_path="notset", parser_cls=RSTParser) -> nodes.document: """Create a new docutils document, with the parser classes' default settings.""" - settings = OptionParser(components=(parser_cls,)).get_default_values() + if docutils.__version_info__[:2] >= (0, 19): + from docutils.frontend import get_default_settings + + settings = get_default_settings(parser_cls) + else: + from docutils.frontend import OptionParser + + settings = OptionParser(components=(parser_cls,)).get_default_values() return new_document(source_path, settings=settings) diff --git a/pyproject.toml b/pyproject.toml index b57ad5b6..81512c31 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -118,6 +118,8 @@ ignore_missing_imports = true [tool.pytest.ini_options] filterwarnings = [ "ignore:.*The default for the setting.*:FutureWarning", +# 'ignore:.*The frontend\.OptionParser class will be replaced.*:DeprecationWarning', +# 'ignore:.*The frontend\.Option class will be removed.*:DeprecationWarning' ] [tool.coverage.run] diff --git a/tests/test_docutils.py b/tests/test_docutils.py index 6e6a22dc..d951821c 100644 --- a/tests/test_docutils.py +++ b/tests/test_docutils.py @@ -1,4 +1,5 @@ import io +import contextlib from dataclasses import dataclass, field, fields from textwrap import dedent from typing import Literal @@ -91,10 +92,16 @@ def test_cli_pseudoxml(monkeypatch, capsys): def test_help_text(): """Test retrieving settings help text.""" - from docutils.frontend import OptionParser + from docutils.core import Publisher stream = io.StringIO() - OptionParser(components=(Parser,)).print_help(stream) + pub = Publisher(parser=Parser()) + with contextlib.redirect_stdout(stream): + try: + pub.process_command_line(["--help"]) + except SystemExit as exc: + assert not exc.code + assert "MyST options" in stream.getvalue() diff --git a/tests/test_renderers/test_fixtures_docutils.py b/tests/test_renderers/test_fixtures_docutils.py index 216eedad..10c9dd87 100644 --- a/tests/test_renderers/test_fixtures_docutils.py +++ b/tests/test_renderers/test_fixtures_docutils.py @@ -128,9 +128,8 @@ def settings_from_cmdline(cmdline: str | None) -> dict[str, Any]: if cmdline is None or not cmdline.strip(): return {} pub = Publisher(parser=Parser()) - option_parser = pub.setup_option_parser() try: - settings = option_parser.parse_args(shlex.split(cmdline)).__dict__ + pub.process_command_line(shlex.split(cmdline)) except Exception as err: raise AssertionError(f"Failed to parse commandline: {cmdline}\n{err}") - return settings + return vars(pub.settings) diff --git a/tests/test_renderers/test_myst_config.py b/tests/test_renderers/test_myst_config.py index f6c3c551..4c9c0bc8 100644 --- a/tests/test_renderers/test_myst_config.py +++ b/tests/test_renderers/test_myst_config.py @@ -22,15 +22,13 @@ def test_cmdline(file_params: ParamTestData): if "heading_slug_func" in file_params.title and __version_info__ < (0, 18): pytest.skip("dupnames ids changed in docutils 0.18") pub = Publisher(parser=Parser()) - option_parser = pub.setup_option_parser() try: - settings = option_parser.parse_args( - shlex.split(file_params.description) - ).__dict__ + pub.process_command_line(shlex.split(file_params.description)) except Exception as err: raise AssertionError( f"Failed to parse commandline: {file_params.description}\n{err}" ) + settings = vars(pub.settings) report_stream = StringIO() settings["output_encoding"] = "unicode" settings["warning_stream"] = report_stream From 0fa9c7538c5107c6f6f56e054ee9fe978f04b60d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 12 Dec 2023 12:29:55 +0000 Subject: [PATCH 2/3] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_docutils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_docutils.py b/tests/test_docutils.py index d951821c..74ff04b5 100644 --- a/tests/test_docutils.py +++ b/tests/test_docutils.py @@ -1,5 +1,5 @@ -import io import contextlib +import io from dataclasses import dataclass, field, fields from textwrap import dedent from typing import Literal From 066de3c024f09c1a7624785dde25886822d43520 Mon Sep 17 00:00:00 2001 From: Angus Hollands Date: Tue, 12 Dec 2023 12:30:11 +0000 Subject: [PATCH 3/3] Update pyproject.toml --- pyproject.toml | 2 -- 1 file changed, 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 81512c31..b57ad5b6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -118,8 +118,6 @@ ignore_missing_imports = true [tool.pytest.ini_options] filterwarnings = [ "ignore:.*The default for the setting.*:FutureWarning", -# 'ignore:.*The frontend\.OptionParser class will be replaced.*:DeprecationWarning', -# 'ignore:.*The frontend\.Option class will be removed.*:DeprecationWarning' ] [tool.coverage.run]