Skip to content

👌 IMPROVE: Add RendererProtocol typing #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions markdown_it/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from .parser_block import ParserBlock # noqa F401
from .parser_inline import ParserInline # noqa F401
from .rules_core.state_core import StateCore
from .renderer import RendererHTML
from .renderer import RendererHTML, RendererProtocol
from .utils import AttrDict

try:
Expand All @@ -42,7 +42,7 @@ def __init__(
config: Union[str, Mapping] = "commonmark",
options_update: Optional[Mapping] = None,
*,
renderer_cls=RendererHTML,
renderer_cls: Callable[["MarkdownIt"], RendererProtocol] = RendererHTML,
):
"""Main parser class

Expand Down
26 changes: 24 additions & 2 deletions markdown_it/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,35 @@ class Renderer
rules if you create plugin and adds new token types.
"""
import inspect
from typing import Optional, Sequence
import sys
from typing import (
Any,
ClassVar,
Mapping,
MutableMapping,
Optional,
Sequence,
)

from .common.utils import unescapeAll, escapeHtml
from .token import Token

if sys.version_info < (3, 8):
from typing_extensions import Protocol
else:
from typing import Protocol
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if sys.version_info < (3, 8):
from typing_extensions import Protocol
else:
from typing import Protocol
try:
from typing import Protocol
except ImportError:
from typing_extensions import Protocol

I've literally just has an issue where sys.version_info was not reliable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed. Please let me know what the issue was. I'm very interested.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It actually turned out to be an issue with importing of importlib_metadata instead of importlib-metadata in a Conda environment 😒 (see python/importlib_metadata#308 and executablebooks/MyST-NB#321), which actually illustrates my "worry" in #154 (comment)

But anyhow, I think this approach is still slightly "safer"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, so conda not doing the name normalization if I understand correctly? Good to know, thanks! I don't use Conda so no need to change any of my personal projects 👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeh seems to be the case 😬

Copy link
Member

@chrisjsewell chrisjsewell Apr 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

despite that it is generally very good; particularly in resolving/enforcing dependency versions and/or if you are using non-python dependencies


class RendererHTML:

class RendererProtocol(Protocol):
__output__: ClassVar[str]

def render(
self, tokens: Sequence[Token], options: Mapping[str, Any], env: MutableMapping
) -> str:
...


class RendererHTML(RendererProtocol):
"""Contains render rules for tokens. Can be updated and extended.

Example:
Expand Down
6 changes: 5 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ def get_version():
],
keywords="markdown lexer parser development",
python_requires="~=3.6",
install_requires=["attrs>=19,<21", "mdit-py-plugins~=0.2.6"],
install_requires=[
"attrs>=19,<21",
"mdit-py-plugins~=0.2.6",
"typing_extensions>=3.7.4; python_version<'3.8'",
],
extras_require={
"code_style": ["pre-commit==2.6"],
"testing": [
Expand Down