Skip to content

Commit f8d3889

Browse files
committed
Add type annotations
1 parent dc03265 commit f8d3889

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

bpython/line.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from itertools import chain
88
from collections import namedtuple
9+
from typing import Optional
910

1011
from .lazyre import LazyReCompile
1112

@@ -14,7 +15,7 @@
1415
current_word_re = LazyReCompile(r"(?<![)\]\w_.])" r"([\w_][\w0-9._]*[(]?)")
1516

1617

17-
def current_word(cursor_offset, line):
18+
def current_word(cursor_offset: int, line: str) -> Optional[LinePart]:
1819
"""the object.attribute.attribute just before or under the cursor"""
1920
pos = cursor_offset
2021
start = pos
@@ -33,7 +34,7 @@ def current_word(cursor_offset, line):
3334
current_dict_key_re = LazyReCompile(r"""[\w_][\w0-9._]*\[([\w0-9._(), '"]*)""")
3435

3536

36-
def current_dict_key(cursor_offset, line):
37+
def current_dict_key(cursor_offset: int, line: str) -> Optional[LinePart]:
3738
"""If in dictionary completion, return the current key"""
3839
for m in current_dict_key_re.finditer(line):
3940
if m.start(1) <= cursor_offset and m.end(1) >= cursor_offset:
@@ -44,7 +45,7 @@ def current_dict_key(cursor_offset, line):
4445
current_dict_re = LazyReCompile(r"""([\w_][\w0-9._]*)\[([\w0-9._(), '"]*)""")
4546

4647

47-
def current_dict(cursor_offset, line):
48+
def current_dict(cursor_offset: int, line: str) -> Optional[LinePart]:
4849
"""If in dictionary completion, return the dict that should be used"""
4950
for m in current_dict_re.finditer(line):
5051
if m.start(2) <= cursor_offset and m.end(2) >= cursor_offset:
@@ -58,7 +59,7 @@ def current_dict(cursor_offset, line):
5859
)
5960

6061

61-
def current_string(cursor_offset, line):
62+
def current_string(cursor_offset: int, line: str) -> Optional[LinePart]:
6263
"""If inside a string of nonzero length, return the string (excluding
6364
quotes)
6465
@@ -74,7 +75,7 @@ def current_string(cursor_offset, line):
7475
current_object_re = LazyReCompile(r"([\w_][\w0-9_]*)[.]")
7576

7677

77-
def current_object(cursor_offset, line):
78+
def current_object(cursor_offset: int, line: str) -> Optional[LinePart]:
7879
"""If in attribute completion, the object on which attribute should be
7980
looked up."""
8081
match = current_word(cursor_offset, line)
@@ -95,7 +96,9 @@ def current_object(cursor_offset, line):
9596
current_object_attribute_re = LazyReCompile(r"([\w_][\w0-9_]*)[.]?")
9697

9798

98-
def current_object_attribute(cursor_offset, line):
99+
def current_object_attribute(
100+
cursor_offset: int, line: str
101+
) -> Optional[LinePart]:
99102
"""If in attribute completion, the attribute being completed"""
100103
# TODO replace with more general current_expression_attribute
101104
match = current_word(cursor_offset, line)
@@ -118,7 +121,9 @@ def current_object_attribute(cursor_offset, line):
118121
)
119122

120123

121-
def current_from_import_from(cursor_offset, line):
124+
def current_from_import_from(
125+
cursor_offset: int, line: str
126+
) -> Optional[LinePart]:
122127
"""If in from import completion, the word after from
123128
124129
returns None if cursor not in or just after one of the two interesting
@@ -138,7 +143,9 @@ def current_from_import_from(cursor_offset, line):
138143
current_from_import_import_re_3 = LazyReCompile(r", *([\w0-9_]*)")
139144

140145

141-
def current_from_import_import(cursor_offset, line):
146+
def current_from_import_import(
147+
cursor_offset: int, line: str
148+
) -> Optional[LinePart]:
142149
"""If in from import completion, the word after import being completed
143150
144151
returns None if cursor not in or just after one of these words
@@ -165,7 +172,7 @@ def current_from_import_import(cursor_offset, line):
165172
current_import_re_3 = LazyReCompile(r"[,][ ]*([\w0-9_.]*)")
166173

167174

168-
def current_import(cursor_offset, line):
175+
def current_import(cursor_offset: int, line: str) -> Optional[LinePart]:
169176
# TODO allow for multiple as's
170177
baseline = current_import_re_1.search(line)
171178
if baseline is None:
@@ -180,12 +187,15 @@ def current_import(cursor_offset, line):
180187
end = baseline.end() + m.end(1)
181188
if start < cursor_offset and end >= cursor_offset:
182189
return LinePart(start, end, m.group(1))
190+
return None
183191

184192

185193
current_method_definition_name_re = LazyReCompile(r"def\s+([a-zA-Z_][\w]*)")
186194

187195

188-
def current_method_definition_name(cursor_offset, line):
196+
def current_method_definition_name(
197+
cursor_offset: int, line: str
198+
) -> Optional[LinePart]:
189199
"""The name of a method being defined"""
190200
for m in current_method_definition_name_re.finditer(line):
191201
if m.start(1) <= cursor_offset and m.end(1) >= cursor_offset:
@@ -196,30 +206,35 @@ def current_method_definition_name(cursor_offset, line):
196206
current_single_word_re = LazyReCompile(r"(?<![.])\b([a-zA-Z_][\w]*)")
197207

198208

199-
def current_single_word(cursor_offset, line):
209+
def current_single_word(cursor_offset: int, line: str) -> Optional[LinePart]:
200210
"""the un-dotted word just before or under the cursor"""
201211
for m in current_single_word_re.finditer(line):
202212
if m.start(1) <= cursor_offset and m.end(1) >= cursor_offset:
203213
return LinePart(m.start(1), m.end(1), m.group(1))
204214
return None
205215

206216

207-
def current_dotted_attribute(cursor_offset, line):
217+
def current_dotted_attribute(
218+
cursor_offset: int, line: str
219+
) -> Optional[LinePart]:
208220
"""The dotted attribute-object pair before the cursor"""
209221
match = current_word(cursor_offset, line)
210222
if match is None:
211223
return None
212224
start, end, word = match
213225
if "." in word[1:]:
214226
return LinePart(start, end, word)
227+
return None
215228

216229

217230
current_expression_attribute_re = LazyReCompile(
218231
r"[.]\s*((?:[\w_][\w0-9_]*)|(?:))"
219232
)
220233

221234

222-
def current_expression_attribute(cursor_offset, line):
235+
def current_expression_attribute(
236+
cursor_offset: int, line: str
237+
) -> Optional[LinePart]:
223238
"""If after a dot, the attribute being completed"""
224239
# TODO replace with more general current_expression_attribute
225240
for m in current_expression_attribute_re.finditer(line):

0 commit comments

Comments
 (0)