Skip to content
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

fix: use TYPE_CHECKING for mdformat type imports #36

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
15 changes: 9 additions & 6 deletions mdformat_frontmatter/plugin.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import io
import sys
from typing import Mapping
from typing import Mapping, TYPE_CHECKING

from markdown_it import MarkdownIt
import mdformat.renderer
from mdformat.renderer import RenderContext, RenderTreeNode
from mdformat.renderer.typing import Render

from mdit_py_plugins.front_matter import front_matter_plugin
import ruamel.yaml

if TYPE_CHECKING:
from mdformat.renderer import RenderContext, RenderTreeNode
from mdformat.renderer.typing import Render


yaml = ruamel.yaml.YAML()
# Make sure to always have `sequence >= offset + 2`
yaml.indent(mapping=2, sequence=4, offset=2)
Expand All @@ -20,7 +23,7 @@ def update_mdit(mdit: MarkdownIt) -> None:
mdit.use(front_matter_plugin)


def _render_frontmatter(node: RenderTreeNode, context: RenderContext) -> str:
def _render_frontmatter(node: "RenderTreeNode", context: "RenderContext") -> str:
# Safety check - parse and dump yaml to ensure it is correctly formatted
dump_stream = io.StringIO()
try:
Expand All @@ -39,4 +42,4 @@ def _render_frontmatter(node: RenderTreeNode, context: RenderContext) -> str:


# apply the render function to the block identified by the mdit plugin
RENDERERS: Mapping[str, Render] = {"front_matter": _render_frontmatter}
RENDERERS: Mapping[str, "Render"] = {"front_matter": _render_frontmatter}