Skip to content

Commit

Permalink
Fix create_warnings mypy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoshanuikabundi authored and agoose77 committed Jun 23, 2023
1 parent 184856b commit 8b90dce
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
11 changes: 6 additions & 5 deletions myst_nb/core/render.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 6 additions & 8 deletions myst_nb/docutils_.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion myst_nb/sphinx_.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 19 additions & 1 deletion myst_nb/warnings_.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 8b90dce

Please sign in to comment.