From 8b90dce7b5f89afad400af17ba55df256c3dca4b Mon Sep 17 00:00:00 2001 From: Josh Mitchell Date: Thu, 8 Jun 2023 16:33:29 +1000 Subject: [PATCH] Fix create_warnings mypy errors --- myst_nb/core/render.py | 11 ++++++----- myst_nb/docutils_.py | 14 ++++++-------- myst_nb/sphinx_.py | 3 ++- myst_nb/warnings_.py | 20 +++++++++++++++++++- 4 files changed, 33 insertions(+), 15 deletions(-) diff --git a/myst_nb/core/render.py b/myst_nb/core/render.py index 86394140..54f1762d 100644 --- a/myst_nb/core/render.py +++ b/myst_nb/core/render.py @@ -30,7 +30,7 @@ from myst_nb.core.execute import NotebookClientBase from myst_nb.core.loggers import LoggerType # DEFAULT_LOG_TYPE, from myst_nb.core.utils import coalesce_streams -from myst_nb.warnings_ import MystNBWarnings +from myst_nb.warnings_ import MystNBWarnings, create_warning if TYPE_CHECKING: from markdown_it.tree import SyntaxTreeNode @@ -58,7 +58,6 @@ class MditRenderMixin: # required by mypy md_options: dict[str, Any] document: nodes.document - create_warning: Any render_children: Any add_line_and_source_path: Any add_line_and_source_path_r: Any @@ -97,7 +96,7 @@ def get_cell_level_config( """ def _callback(msg: str, subtype: MystNBWarnings): - self.create_warning(msg, line=line, subtype=subtype) + create_warning(self.document, msg, line=line, subtype=subtype) return self.nb_config.get_cell_level_config(field, cell_metadata, _callback) @@ -223,7 +222,8 @@ def _get_nb_source_code_lexer( # TODO this will create a warning for every cell, but perhaps # it should only be a single warning for the notebook (as previously) # TODO allow user to set default lexer? - self.create_warning( + create_warning( + self.document, f"No source code lexer found for notebook cell {cell_index + 1}", # wtype=DEFAULT_LOG_TYPE, subtype=MystNBWarnings.LEXER, @@ -977,7 +977,8 @@ def create_figure_context( caption.source = self.document["source"] caption.line = line elif not (isinstance(first_node, nodes.comment) and len(first_node) == 0): - self.create_warning( + create_warning( + self.document, "Figure caption must be a paragraph or empty comment.", line=line, # wtype=DEFAULT_LOG_TYPE, diff --git a/myst_nb/docutils_.py b/myst_nb/docutils_.py index d094dbb3..7da9c6db 100644 --- a/myst_nb/docutils_.py +++ b/myst_nb/docutils_.py @@ -14,11 +14,7 @@ from markdown_it.token import Token from markdown_it.tree import SyntaxTreeNode from myst_parser.config.main import MdParserConfig, merge_file_level -from myst_parser.mdit_to_docutils.base import ( - DocutilsRenderer, - create_warning, - token_line, -) +from myst_parser.mdit_to_docutils.base import DocutilsRenderer, token_line from myst_parser.parsers.docutils_ import Parser as MystParser from myst_parser.parsers.docutils_ import create_myst_config, create_myst_settings_spec from myst_parser.parsers.mdit import create_md_parser @@ -47,7 +43,7 @@ ) from myst_nb.ext.eval import load_eval_docutils from myst_nb.ext.glue import load_glue_docutils -from myst_nb.warnings_ import MystNBWarnings +from myst_nb.warnings_ import MystNBWarnings, create_warning DOCUTILS_EXCLUDED_ARGS = list( {f.name for f in NbParserConfig.get_fields() if f.metadata.get("docutils_exclude")} @@ -304,7 +300,8 @@ def _render_nb_cell_code_outputs( mime_type = next(x for x in mime_priority if x in output["data"]) except StopIteration: if output["data"]: - self.create_warning( + create_warning( + self.document, "No output mime type found from render_priority " f"(cell<{cell_index}>.output<{output_index}>", line=line, @@ -335,7 +332,8 @@ def _render_nb_cell_code_outputs( self.current_node.extend(_nodes) self.add_line_and_source_path_r(_nodes, token) else: - self.create_warning( + create_warning( + self.document, f"Unsupported output type: {output.output_type}", line=line, append_to=self.current_node, diff --git a/myst_nb/sphinx_.py b/myst_nb/sphinx_.py index 56eef56c..dba36531 100644 --- a/myst_nb/sphinx_.py +++ b/myst_nb/sphinx_.py @@ -302,7 +302,8 @@ def _render_nb_cell_code_outputs( self.add_line_and_source_path_r([mime_bundle], token) self.current_node.append(mime_bundle) else: - self.create_warning( + create_warning( + self.document, f"Unsupported output type: {output.output_type}", line=line, append_to=self.current_node, diff --git a/myst_nb/warnings_.py b/myst_nb/warnings_.py index 55cdad5d..3679b2be 100644 --- a/myst_nb/warnings_.py +++ b/myst_nb/warnings_.py @@ -5,6 +5,14 @@ from typing import Sequence from docutils import nodes +from myst_parser.warnings_ import MystWarnings +from myst_parser.warnings_ import create_warning as myst_parser_create_warnings + +__all__ = [ + "MystWarnings", + "MystNBWarnings", + "create_warning", +] class MystNBWarnings(Enum): @@ -50,7 +58,7 @@ def _is_suppressed_warning( def create_warning( document: nodes.document, message: str, - subtype: MystNBWarnings, + subtype: MystNBWarnings | MystWarnings, *, line: int | None = None, append_to: nodes.Element | None = None, @@ -60,6 +68,16 @@ def create_warning( If the warning type is listed in the ``suppress_warnings`` configuration, then ``None`` will be returned and no warning logged. """ + # Pass off Myst Parser warnings to that package + if isinstance(subtype, MystWarnings): + myst_parser_create_warnings( + document=document, + message=message, + subtype=subtype, + line=line, + append_to=append_to, + ) + wtype = "myst-nb" # figure out whether to suppress the warning, if sphinx is available, # it will have been set up by the Sphinx environment,