Skip to content

Commit 2c80e03

Browse files
committed
Add type annotations
1 parent 2776156 commit 2c80e03

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed
Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,53 @@
11
"""Tools for preparing code to be run in the REPL (removing blank lines,
22
etc)"""
33

4-
from ..lazyre import LazyReCompile
4+
from codeop import CommandCompiler
5+
from typing import Match
56
from itertools import tee, islice, chain
67

8+
from ..lazyre import LazyReCompile
9+
710
# TODO specifically catch IndentationErrors instead of any syntax errors
811

912
indent_empty_lines_re = LazyReCompile(r"\s*")
1013
tabs_to_spaces_re = LazyReCompile(r"^\t+")
1114

1215

13-
def indent_empty_lines(s, compiler):
16+
def indent_empty_lines(s: str, compiler: CommandCompiler) -> str:
1417
"""Indents blank lines that would otherwise cause early compilation
1518
1619
Only really works if starting on a new line"""
17-
lines = s.split("\n")
20+
initial_lines = s.split("\n")
1821
ends_with_newline = False
19-
if lines and not lines[-1]:
22+
if initial_lines and not initial_lines[-1]:
2023
ends_with_newline = True
21-
lines.pop()
24+
initial_lines.pop()
2225
result_lines = []
2326

24-
prevs, lines, nexts = tee(lines, 3)
27+
prevs, lines, nexts = tee(initial_lines, 3)
2528
prevs = chain(("",), prevs)
2629
nexts = chain(islice(nexts, 1, None), ("",))
2730

2831
for p_line, line, n_line in zip(prevs, lines, nexts):
2932
if len(line) == 0:
30-
p_indent = indent_empty_lines_re.match(p_line).group()
31-
n_indent = indent_empty_lines_re.match(n_line).group()
33+
# "\s*" always matches
34+
p_indent = indent_empty_lines_re.match(p_line).group() # type: ignore
35+
n_indent = indent_empty_lines_re.match(n_line).group() # type: ignore
3236
result_lines.append(min([p_indent, n_indent], key=len) + line)
3337
else:
3438
result_lines.append(line)
3539

3640
return "\n".join(result_lines) + ("\n" if ends_with_newline else "")
3741

3842

39-
def leading_tabs_to_spaces(s):
40-
lines = s.split("\n")
41-
result_lines = []
42-
43-
def tab_to_space(m):
43+
def leading_tabs_to_spaces(s: str) -> str:
44+
def tab_to_space(m: Match[str]) -> str:
4445
return len(m.group()) * 4 * " "
4546

46-
for line in lines:
47-
result_lines.append(tabs_to_spaces_re.sub(tab_to_space, line))
48-
return "\n".join(result_lines)
47+
return "\n".join(
48+
tabs_to_spaces_re.sub(tab_to_space, line) for line in s.split("\n")
49+
)
4950

5051

51-
def preprocess(s, compiler):
52+
def preprocess(s: str, compiler: CommandCompiler) -> str:
5253
return indent_empty_lines(leading_tabs_to_spaces(s), compiler)

0 commit comments

Comments
 (0)