Skip to content

Commit 408b621

Browse files
committed
Enhance type safety in md2term.py by adding type annotations for code_block_lines and asserting token types after markdown rendering. This improves code clarity and ensures expected data structures are used throughout the rendering process.
1 parent ba2ebf4 commit 408b621

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

md2term.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class TerminalRenderer:
3232
def __init__(self, console: Console):
3333
self.console = console
3434
self.in_code_block = False
35-
self.code_block_lines = []
35+
self.code_block_lines: List[str] = []
3636
self.code_block_lang = None
3737

3838
def render(self, tokens: List[Dict[str, Any]]) -> None:
@@ -269,6 +269,8 @@ def _render_callout(self, content: str, callout_info: Dict[str, str]) -> None:
269269
markdown = mistune.create_markdown(renderer=None)
270270
try:
271271
tokens = markdown(callout_content)
272+
# Type assertion since we know renderer=None returns tokens
273+
assert isinstance(tokens, list)
272274
temp_renderer = TerminalRenderer(temp_console)
273275
temp_renderer.render(tokens)
274276
except Exception:
@@ -495,6 +497,8 @@ def _render_and_count(self, content: str) -> None:
495497
# Parse and render to temp console
496498
markdown = mistune.create_markdown(renderer=None)
497499
tokens = markdown(content)
500+
# Type assertion since we know renderer=None returns tokens
501+
assert isinstance(tokens, list)
498502
temp_renderer = TerminalRenderer(temp_console)
499503
temp_renderer.render(tokens)
500504

@@ -529,6 +533,8 @@ def _render_final(self) -> None:
529533
try:
530534
markdown = mistune.create_markdown(renderer=None)
531535
tokens = markdown(self.buffer)
536+
# Type assertion since we know renderer=None returns tokens
537+
assert isinstance(tokens, list)
532538
renderer = TerminalRenderer(self.console)
533539
renderer.render(tokens)
534540
except Exception:
@@ -544,6 +550,8 @@ def finalize(self) -> None:
544550
try:
545551
markdown = mistune.create_markdown(renderer=None)
546552
tokens = markdown(self.buffer)
553+
# Type assertion since we know renderer=None returns tokens
554+
assert isinstance(tokens, list)
547555
renderer = TerminalRenderer(self.console)
548556
renderer.render(tokens)
549557
except Exception:

0 commit comments

Comments
 (0)