Skip to content

Commit 15ba9b6

Browse files
👌 IMPROVE: Add RendererProtocol typing (#126)
Co-authored-by: Chris Sewell <[email protected]>
1 parent 99ebfac commit 15ba9b6

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

markdown_it/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from .parser_block import ParserBlock # noqa F401
2020
from .parser_inline import ParserInline # noqa F401
2121
from .rules_core.state_core import StateCore
22-
from .renderer import RendererHTML
22+
from .renderer import RendererHTML, RendererProtocol
2323
from .utils import OptionsDict
2424

2525
try:
@@ -43,7 +43,7 @@ def __init__(
4343
config: Union[str, Mapping] = "commonmark",
4444
options_update: Optional[Mapping] = None,
4545
*,
46-
renderer_cls=RendererHTML,
46+
renderer_cls: Callable[["MarkdownIt"], RendererProtocol] = RendererHTML,
4747
):
4848
"""Main parser class
4949

markdown_it/renderer.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,34 @@ class Renderer
66
rules if you create plugin and adds new token types.
77
"""
88
import inspect
9-
from typing import MutableMapping, Optional, Sequence
9+
from typing import (
10+
Any,
11+
ClassVar,
12+
MutableMapping,
13+
Optional,
14+
Sequence,
15+
)
1016

1117
from .common.utils import unescapeAll, escapeHtml
1218
from .token import Token
1319
from .utils import OptionsDict
1420

21+
try:
22+
from typing import Protocol
23+
except ImportError: # Python <3.8 doesn't have `Protocol` in the stdlib
24+
from typing_extensions import Protocol # type: ignore[misc]
1525

16-
class RendererHTML:
26+
27+
class RendererProtocol(Protocol):
28+
__output__: ClassVar[str]
29+
30+
def render(
31+
self, tokens: Sequence[Token], options: OptionsDict, env: MutableMapping
32+
) -> Any:
33+
...
34+
35+
36+
class RendererHTML(RendererProtocol):
1737
"""Contains render rules for tokens. Can be updated and extended.
1838
1939
Example:

setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ project_urls =
3030
packages = find:
3131
install_requires =
3232
attrs>=19,<21
33+
typing_extensions>=3.7.4;python_version<'3.8'
3334
python_requires = ~=3.6
3435
include_package_data = True
3536
zip_safe = False

0 commit comments

Comments
 (0)