Skip to content

Commit d69e531

Browse files
committed
type: add type annotations to render_node.py and output.py
Add complete type annotations to RenderNode class and render_ref_list function using the new type aliases. Changes: - Add types to RenderNode.__init__(), new_child(), to_str() - Add types to render_ref_list() function - Use ASTNode and RefDict type aliases from types.py
1 parent c434b11 commit d69e531

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

md2zhihu/parser/output.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
from typing import List
2+
3+
from ..types import RefDict
4+
5+
16
# TODO: move to renderer module?
2-
def render_ref_list(refs, platform):
3-
ref_lines = ["", "Reference:", ""]
7+
def render_ref_list(refs: RefDict, platform: str) -> List[str]:
8+
ref_lines: List[str] = ["", "Reference:", ""]
49
for ref_id in sorted(refs):
510
# url_and_alt is in form "<url> <alt>"
611
url_alt = refs[ref_id].split()

md2zhihu/renderer/render_node.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,33 @@
1+
from __future__ import annotations
2+
3+
from typing import Optional
4+
5+
from ..types import ASTNode
6+
7+
18
class RenderNode(object):
29
"""
310
RenderNode is a container of current ast-node and parent
411
"""
512

6-
def __init__(self, n, parent=None):
13+
def __init__(self, n: ASTNode, parent: Optional[RenderNode] = None) -> None:
714
"""
815
:param n: ast node: a normal dictionary such as {'type': 'text' ... }
916
:param parent: parent RenderNode
1017
"""
11-
self.node = n
18+
self.node: ASTNode = n
1219

13-
self.level = 0
20+
self.level: int = 0
1421

1522
# parent RenderNode
16-
self.parent = parent
23+
self.parent: Optional[RenderNode] = parent
1724

18-
def new_child(self, n):
25+
def new_child(self, n: ASTNode) -> RenderNode:
1926
c = RenderNode(n, parent=self)
2027
c.level = self.level + 1
2128
return c
2229

23-
def to_str(self):
30+
def to_str(self) -> str:
2431
t = "{}".format(self.node.get("type"))
2532
if self.parent is None:
2633
return t

0 commit comments

Comments
 (0)