Skip to content

Commit 5629bbe

Browse files
Ben-Regthomasballinger
authored andcommitted
Adding type hints to formatter.py
1 parent a60ae15 commit 5629bbe

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

bpython/formatter.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,14 @@
2424
# Pygments really kicks ass, it made it really easy to
2525
# get the exact behaviour I wanted, thanks Pygments.:)
2626

27+
# mypy: disallow_untyped_defs=True
28+
# mypy: disallow_untyped_calls=True
2729

30+
31+
from typing import Any, MutableMapping, Iterable, TextIO
2832
from pygments.formatter import Formatter
2933
from pygments.token import (
34+
_TokenType,
3035
Keyword,
3136
Name,
3237
Comment,
@@ -96,7 +101,9 @@ class BPythonFormatter(Formatter):
96101
See the Pygments source for more info; it's pretty
97102
straightforward."""
98103

99-
def __init__(self, color_scheme, **options):
104+
def __init__(
105+
self, color_scheme: MutableMapping[str, str], **options: Any
106+
) -> None:
100107
self.f_strings = {}
101108
for k, v in theme_map.items():
102109
self.f_strings[k] = f"\x01{color_scheme[v]}"
@@ -106,14 +113,21 @@ def __init__(self, color_scheme, **options):
106113
self.f_strings[k] += "I"
107114
super().__init__(**options)
108115

109-
def format(self, tokensource, outfile):
110-
o = ""
116+
def format(
117+
self,
118+
tokensource: Iterable[MutableMapping[_TokenType, str]],
119+
outfile: TextIO,
120+
) -> None:
121+
o: str = ""
111122
for token, text in tokensource:
112123
if text == "\n":
113124
continue
114125

115126
while token not in self.f_strings:
116-
token = token.parent
127+
if token.parent is None:
128+
break
129+
else:
130+
token = token.parent
117131
o += f"{self.f_strings[token]}\x03{text}\x04"
118132
outfile.write(o.rstrip())
119133

0 commit comments

Comments
 (0)