Skip to content

Commit 8290faa

Browse files
👌 IMPROVE: Add more typing (#106)
Co-authored-by: Chris Sewell <[email protected]>
1 parent ef60702 commit 8290faa

File tree

8 files changed

+44
-13
lines changed

8 files changed

+44
-13
lines changed

markdown_it/common/normalize_url.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def normalizeLink(url):
113113
# return quote(urlunparse(parsed))
114114

115115

116-
def unescape_unquote(x):
116+
def unescape_unquote(x: str) -> str:
117117
return unquote(unescape_string(x))
118118

119119

markdown_it/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ def __init__(
4242
self.renderer = renderer_cls(self)
4343

4444
self.utils = utils
45-
self.helpers = helpers
46-
self.options: Dict[str, Any] = {}
45+
self.helpers: Any = helpers
46+
self.options = AttrDict()
4747
self.configure(config)
4848

4949
self.linkify = linkify_it.LinkifyIt() if linkify_it else None
@@ -59,7 +59,7 @@ def __getitem__(self, name):
5959
"renderer": self.renderer,
6060
}[name]
6161

62-
def set(self, options):
62+
def set(self, options: AttrDict) -> None:
6363
"""Set parser options (in the same format as in constructor).
6464
Probably, you will never need it, but you can change options after constructor call.
6565

markdown_it/ruler.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,25 @@ class Ruler
1515
rules control use [[MarkdownIt.disable]], [[MarkdownIt.enable]] and
1616
[[MarkdownIt.use]].
1717
"""
18-
from typing import Callable, Dict, Iterable, List, Optional, Union
18+
from typing import (
19+
Callable,
20+
Dict,
21+
Iterable,
22+
List,
23+
Optional,
24+
TYPE_CHECKING,
25+
Union,
26+
)
1927
import attr
2028

29+
from markdown_it.utils import AttrDict
30+
31+
if TYPE_CHECKING:
32+
from markdown_it import MarkdownIt
33+
2134

2235
class StateBase:
23-
def __init__(self, src: str, md, env):
36+
def __init__(self, src: str, md: "MarkdownIt", env: AttrDict):
2437
self.srcCharCode: List[int] = []
2538
self.src = src
2639
self.env = env

markdown_it/rules_core/inline.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,6 @@ def inline(state: StateCore) -> None:
55
"""Parse inlines"""
66
for token in state.tokens:
77
if token.type == "inline":
8+
if token.children is None:
9+
token.children = []
810
state.md.inline.parse(token.content, state.md, state.env, token.children)

markdown_it/rules_core/state_core.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
from typing import List
1+
from typing import List, Optional, TYPE_CHECKING
22

3+
from ..utils import AttrDict
34
from ..token import Token
45
from ..ruler import StateBase
56

7+
if TYPE_CHECKING:
8+
from markdown_it import MarkdownIt
9+
610

711
class StateCore(StateBase):
8-
def __init__(self, src: str, md, env, tokens=None):
12+
def __init__(
13+
self,
14+
src: str,
15+
md: "MarkdownIt",
16+
env: AttrDict,
17+
tokens: Optional[List[Token]] = None,
18+
):
919
self.src = src
1020
self.md = md # link to parser instance
1121
self.env = env

markdown_it/rules_inline/state_inline.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
from collections import namedtuple
2-
from typing import Dict, List, Optional
2+
from typing import Dict, List, Optional, TYPE_CHECKING
33

44
import attr
55

6+
from ..utils import AttrDict
67
from ..token import Token
78
from ..ruler import StateBase
89
from ..common.utils import isWhiteSpace, isPunctChar, isMdAsciiPunct
910

11+
if TYPE_CHECKING:
12+
from markdown_it import MarkdownIt
13+
1014

1115
@attr.s(slots=True)
1216
class Delimiter:
@@ -43,7 +47,9 @@ class Delimiter:
4347

4448

4549
class StateInline(StateBase):
46-
def __init__(self, src: str, md, env, outTokens: List[Token]):
50+
def __init__(
51+
self, src: str, md: "MarkdownIt", env: AttrDict, outTokens: List[Token]
52+
):
4753
self.src = src
4854
self.env = env
4955
self.md = md

markdown_it/token.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def attrJoin(self, name, value):
8181
else:
8282
self.attrs[idx][1] = self.attrs[idx][1] + " " + value
8383

84-
def copy(self):
84+
def copy(self) -> "Token":
8585
"""Return a shallow copy of the instance."""
8686
return attr.evolve(self)
8787

markdown_it/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from pathlib import Path
2-
from typing import TYPE_CHECKING, Any
2+
from typing import TYPE_CHECKING, Any, List, Union
33

44

55
if TYPE_CHECKING:
@@ -17,7 +17,7 @@ def __init__(self, *args, **kwargs):
1717
self[key] = AttrDict(item)
1818

1919

20-
def read_fixture_file(path):
20+
def read_fixture_file(path: Union[str, Path]) -> List[list]:
2121
text = Path(path).read_text(encoding="utf-8")
2222
tests = []
2323
section = 0

0 commit comments

Comments
 (0)