Skip to content

Commit 9678a84

Browse files
Yoshanuikabundicholdgraf
authored andcommitted
Fix create_warnings mypy errors
1 parent a150582 commit 9678a84

4 files changed

Lines changed: 33 additions & 15 deletions

File tree

myst_nb/core/render.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from myst_nb.core.execute import NotebookClientBase
3131
from myst_nb.core.loggers import LoggerType # DEFAULT_LOG_TYPE,
3232
from myst_nb.core.utils import coalesce_streams
33-
from myst_nb.warnings_ import MystNBWarnings
33+
from myst_nb.warnings_ import MystNBWarnings, create_warning
3434

3535
if TYPE_CHECKING:
3636
from markdown_it.tree import SyntaxTreeNode
@@ -58,7 +58,6 @@ class MditRenderMixin:
5858
# required by mypy
5959
md_options: dict[str, Any]
6060
document: nodes.document
61-
create_warning: Any
6261
render_children: Any
6362
add_line_and_source_path: Any
6463
add_line_and_source_path_r: Any
@@ -97,7 +96,7 @@ def get_cell_level_config(
9796
"""
9897

9998
def _callback(msg: str, subtype: MystNBWarnings):
100-
self.create_warning(msg, line=line, subtype=subtype)
99+
create_warning(self.document, msg, line=line, subtype=subtype)
101100

102101
return self.nb_config.get_cell_level_config(field, cell_metadata, _callback)
103102

@@ -223,7 +222,8 @@ def _get_nb_source_code_lexer(
223222
# TODO this will create a warning for every cell, but perhaps
224223
# it should only be a single warning for the notebook (as previously)
225224
# TODO allow user to set default lexer?
226-
self.create_warning(
225+
create_warning(
226+
self.document,
227227
f"No source code lexer found for notebook cell {cell_index + 1}",
228228
# wtype=DEFAULT_LOG_TYPE,
229229
subtype=MystNBWarnings.LEXER,
@@ -977,7 +977,8 @@ def create_figure_context(
977977
caption.source = self.document["source"]
978978
caption.line = line
979979
elif not (isinstance(first_node, nodes.comment) and len(first_node) == 0):
980-
self.create_warning(
980+
create_warning(
981+
self.document,
981982
"Figure caption must be a paragraph or empty comment.",
982983
line=line,
983984
# wtype=DEFAULT_LOG_TYPE,

myst_nb/docutils_.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@
1414
from markdown_it.token import Token
1515
from markdown_it.tree import SyntaxTreeNode
1616
from myst_parser.config.main import MdParserConfig, merge_file_level
17-
from myst_parser.mdit_to_docutils.base import (
18-
DocutilsRenderer,
19-
create_warning,
20-
token_line,
21-
)
17+
from myst_parser.mdit_to_docutils.base import DocutilsRenderer, token_line
2218
from myst_parser.parsers.docutils_ import Parser as MystParser
2319
from myst_parser.parsers.docutils_ import create_myst_config, create_myst_settings_spec
2420
from myst_parser.parsers.mdit import create_md_parser
@@ -47,7 +43,7 @@
4743
)
4844
from myst_nb.ext.eval import load_eval_docutils
4945
from myst_nb.ext.glue import load_glue_docutils
50-
from myst_nb.warnings_ import MystNBWarnings
46+
from myst_nb.warnings_ import MystNBWarnings, create_warning
5147

5248
DOCUTILS_EXCLUDED_ARGS = list(
5349
{f.name for f in NbParserConfig.get_fields() if f.metadata.get("docutils_exclude")}
@@ -304,7 +300,8 @@ def _render_nb_cell_code_outputs(
304300
mime_type = next(x for x in mime_priority if x in output["data"])
305301
except StopIteration:
306302
if output["data"]:
307-
self.create_warning(
303+
create_warning(
304+
self.document,
308305
"No output mime type found from render_priority "
309306
f"(cell<{cell_index}>.output<{output_index}>",
310307
line=line,
@@ -335,7 +332,8 @@ def _render_nb_cell_code_outputs(
335332
self.current_node.extend(_nodes)
336333
self.add_line_and_source_path_r(_nodes, token)
337334
else:
338-
self.create_warning(
335+
create_warning(
336+
self.document,
339337
f"Unsupported output type: {output.output_type}",
340338
line=line,
341339
append_to=self.current_node,

myst_nb/sphinx_.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ def _render_nb_cell_code_outputs(
302302
self.add_line_and_source_path_r([mime_bundle], token)
303303
self.current_node.append(mime_bundle)
304304
else:
305-
self.create_warning(
305+
create_warning(
306+
self.document,
306307
f"Unsupported output type: {output.output_type}",
307308
line=line,
308309
append_to=self.current_node,

myst_nb/warnings_.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
from typing import Sequence
66

77
from docutils import nodes
8+
from myst_parser.warnings_ import MystWarnings
9+
from myst_parser.warnings_ import create_warning as myst_parser_create_warnings
10+
11+
__all__ = [
12+
"MystWarnings",
13+
"MystNBWarnings",
14+
"create_warning",
15+
]
816

917

1018
class MystNBWarnings(Enum):
@@ -50,7 +58,7 @@ def _is_suppressed_warning(
5058
def create_warning(
5159
document: nodes.document,
5260
message: str,
53-
subtype: MystNBWarnings,
61+
subtype: MystNBWarnings | MystWarnings,
5462
*,
5563
line: int | None = None,
5664
append_to: nodes.Element | None = None,
@@ -60,6 +68,16 @@ def create_warning(
6068
If the warning type is listed in the ``suppress_warnings`` configuration,
6169
then ``None`` will be returned and no warning logged.
6270
"""
71+
# Pass off Myst Parser warnings to that package
72+
if isinstance(subtype, MystWarnings):
73+
myst_parser_create_warnings(
74+
document=document,
75+
message=message,
76+
subtype=subtype,
77+
line=line,
78+
append_to=append_to,
79+
)
80+
6381
wtype = "myst-nb"
6482
# figure out whether to suppress the warning, if sphinx is available,
6583
# it will have been set up by the Sphinx environment,

0 commit comments

Comments
 (0)