Skip to content

Commit a7c4fe3

Browse files
committed
Fix pytype errors
1 parent d9d3972 commit a7c4fe3

3 files changed

Lines changed: 17 additions & 13 deletions

File tree

mkdocstrings/handlers/crystal/crystal_html.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import collections
22
import html.parser
33
import io
4-
from typing import Callable, List, Sequence, Tuple
4+
from typing import Callable, Iterable, List, Sequence, Tuple
55

66
from markupsafe import Markup, escape
77

8-
LinkTokens = Sequence[Tuple[int, int, str]]
8+
LinkToken = Tuple[int, int, str]
99

1010

1111
class TextWithLinks(collections.UserString):
@@ -16,10 +16,10 @@ class TextWithLinks(collections.UserString):
1616
The link information is currently for internal use only.
1717
"""
1818

19-
tokens: LinkTokens
19+
tokens: Sequence[LinkToken]
2020
"""The list of embedded links."""
2121

22-
def __init__(self, string, tokens: LinkTokens):
22+
def __init__(self, string, tokens: Sequence[LinkToken]):
2323
super().__init__(string)
2424
self.tokens = tokens
2525

@@ -34,7 +34,7 @@ def parse_crystal_html(crystal_html: str) -> TextWithLinks:
3434

3535

3636
def linkify_highlighted_html(
37-
pygments_html: str, html_tokens: LinkTokens, make_link: Callable[[str, str], str]
37+
pygments_html: str, html_tokens: Sequence[LinkToken], make_link: Callable[[str, str], str]
3838
) -> str:
3939
pygments_parser = _PygmentsHTMLHandler(html_tokens, make_link)
4040
pygments_parser.feed(pygments_html)
@@ -45,7 +45,7 @@ class _CrystalHTMLHandler(html.parser.HTMLParser):
4545
def __init__(self):
4646
super().__init__()
4747
self.text = io.StringIO()
48-
self.tokens: LinkTokens = []
48+
self.tokens: List[LinkToken] = []
4949
self._link_starts: List[Tuple[int, str]] = []
5050

5151
def handle_starttag(self, tag, attrs):
@@ -71,18 +71,19 @@ def link_to_path(cls, href):
7171

7272

7373
class _PygmentsHTMLHandler(html.parser.HTMLParser):
74-
def __init__(self, tokens: LinkTokens, make_link: Callable[[str, str], str]):
74+
def __init__(self, tokens: Iterable[LinkToken], make_link: Callable[[str, str], str]):
7575
super().__init__()
76-
self.tokens = tokens
76+
self.tokens = iter(tokens)
7777
self.make_link = make_link
7878

79+
self.token = next(self.tokens, None)
7980
self.pos = 0
8081
self.html = io.StringIO()
8182
self.inlink: Optional[int] = None
8283

8384
def handle_starttag(self, tag, attrs):
8485
if tag == "span" and self.inlink is None:
85-
if self.tokens and self.tokens[0][0] <= self.pos:
86+
if self.token and self.token[0] <= self.pos:
8687
self.inlink = self.html.tell()
8788

8889
if self.inlink is None:
@@ -94,10 +95,12 @@ def handle_endtag(self, tag):
9495
self.html.write(f"</{tag}>")
9596

9697
if tag == "span" and self.inlink is not None:
97-
if self.tokens and self.tokens[0][1] <= self.pos:
98+
if self.token and self.token[1] <= self.pos:
9899
self.html.seek(self.inlink)
99100
subhtml = Markup(self.html.read())
100-
subhtml = self.make_link(self.tokens.pop(0)[2], subhtml)
101+
subhtml = self.make_link(self.token[2], subhtml)
102+
self.token = next(self.tokens, None)
103+
101104
self.html.seek(self.inlink)
102105
self.html.truncate()
103106
self.html.write(subhtml)

mkdocstrings/handlers/crystal/inventory.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import itertools
22
import json
33
import posixpath
4+
from typing import cast
45

56
from .items import DocModule
67

78

89
def read(file) -> DocModule:
910
data = json.load(file)
1011
data["program"]["full_name"] = ""
11-
return DocModule(data["program"], None, None)
12+
return cast(DocModule, DocModule(data["program"], None, None))
1213

1314

1415
def list_objects(obj):

mkdocstrings/handlers/crystal/items.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ class DocType(DocItem):
129129

130130
_TEMPLATE = "type.html"
131131

132-
def __new__(cls, data: Mapping[str, Any] = None, *args, **kwargs) -> DocType:
132+
def __new__(cls, data: Optional[Mapping[str, Any]] = None, *args, **kwargs) -> DocType:
133133
"""Based on Crystal's JSON, create an object of an appropriate subclass of DocType"""
134134
if cls is DocType:
135135
try:

0 commit comments

Comments
 (0)