Skip to content

Commit

Permalink
Fix mypy errors
Browse files Browse the repository at this point in the history
  • Loading branch information
fcollonval authored Dec 31, 2024
1 parent d0b336d commit 7976a4a
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions nbconvert/filters/markdown_mistune.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import mimetypes
import os
from html import escape
from typing import Any, Callable, Dict, Iterable, Match, Optional, Tuple
from typing import TYPE_CHECKING, Any, Callable, Dict, Iterable, Match, Optional, Tuple

import bs4
from pygments import highlight
Expand Down Expand Up @@ -53,6 +53,14 @@ def import_plugin(name: str) -> "MarkdownPlugin": # type: ignore[misc]
return PLUGINS[name] # type: ignore[no-any-return]


if TYPE_CHECKING:
try:
from mistune.plugins import Plugin
except ImportError:
class Plugin(Protocol):
def __call__(self, markdown: "Markdown") -> None: ...


class InvalidNotebook(Exception):
"""An invalid notebook model."""

Expand Down Expand Up @@ -103,7 +111,7 @@ class MathBlockParser(BlockParser):
}

# Multiline math must be searched before other rules
DEFAULT_RULES: Tuple[str, ...] = ("multiline_math", *BlockParser.DEFAULT_RULES) # type: ignore[assignment]
DEFAULT_RULES: ClassVar[Iterable[str]] = ("multiline_math", *BlockParser.DEFAULT_RULES) # type: ignore[assignment]

def parse_multiline_math(self, m: Match[str], state: BlockState) -> int:
"""Send mutiline math as a single paragraph to MathInlineParser."""
Expand Down Expand Up @@ -145,7 +153,7 @@ class MathInlineParser(InlineParser):
}

# Block math must be matched first, and all math must come before text
DEFAULT_RULES: Tuple[str, ...] = (
DEFAULT_RULES: ClassVar[Iterable[str]] = (
"block_math_tex",
"block_math_latex",
"inline_math_tex",
Expand Down Expand Up @@ -448,10 +456,6 @@ def _html_embed_images(self, html: str) -> str:
return str(parsed_html)


# Represents an already imported plugin for Mistune
MarkdownPlugin = Callable[[Markdown], None]


class MarkdownWithMath(Markdown):
"""Markdown text with math enabled."""

Expand All @@ -470,7 +474,7 @@ def __init__(
renderer: HTMLRenderer,
block: Optional[BlockParser] = None,
inline: Optional[InlineParser] = None,
plugins: Optional[Iterable[MarkdownPlugin]] = None,
plugins: Optional[Iterable["Plugin"]] = None,
):
"""Initialize the parser."""
if block is None:
Expand Down

0 comments on commit 7976a4a

Please sign in to comment.