Skip to content

Commit 9284640

Browse files
committed
Add type annotations to MatchesIterator
1 parent a96d6e3 commit 9284640

File tree

1 file changed

+30
-20
lines changed

1 file changed

+30
-20
lines changed

bpython/repl.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
from itertools import takewhile
3838
from pathlib import Path
3939
from types import ModuleType, TracebackType
40-
from typing import cast, Tuple, Any, Optional, Type
40+
from typing import cast, List, Tuple, Any, Optional, Type
4141
from ._typing_compat import Literal
4242

4343
from pygments.lexers import Python3Lexer
@@ -226,75 +226,79 @@ class MatchesIterator:
226226
A MatchesIterator can be `clear`ed to reset match iteration, and
227227
`update`ed to set what matches will be iterated over."""
228228

229-
def __init__(self):
229+
def __init__(self) -> None:
230230
# word being replaced in the original line of text
231231
self.current_word = ""
232232
# possible replacements for current_word
233-
self.matches = None
233+
self.matches: List[str] = []
234234
# which word is currently replacing the current word
235235
self.index = -1
236236
# cursor position in the original line
237237
self.orig_cursor_offset = -1
238238
# original line (before match replacements)
239239
self.orig_line = ""
240240
# class describing the current type of completion
241-
self.completer = None
241+
self.completer: Optional[autocomplete.BaseCompletionType] = None
242242

243-
def __nonzero__(self):
243+
def __nonzero__(self) -> bool:
244244
"""MatchesIterator is False when word hasn't been replaced yet"""
245245
return self.index != -1
246246

247-
def __bool__(self):
247+
def __bool__(self) -> bool:
248248
return self.index != -1
249249

250250
@property
251-
def candidate_selected(self):
251+
def candidate_selected(self) -> bool:
252252
"""True when word selected/replaced, False when word hasn't been
253253
replaced yet"""
254254
return bool(self)
255255

256-
def __iter__(self):
256+
def __iter__(self) -> "MatchesIterator":
257257
return self
258258

259-
def current(self):
259+
def current(self) -> str:
260260
if self.index == -1:
261261
raise ValueError("No current match.")
262262
return self.matches[self.index]
263263

264-
def __next__(self):
264+
def __next__(self) -> str:
265265
self.index = (self.index + 1) % len(self.matches)
266266
return self.matches[self.index]
267267

268-
def previous(self):
268+
def previous(self) -> str:
269269
if self.index <= 0:
270270
self.index = len(self.matches)
271271
self.index -= 1
272272

273273
return self.matches[self.index]
274274

275-
def cur_line(self):
275+
def cur_line(self) -> Tuple[int, str]:
276276
"""Returns a cursor offset and line with the current substitution
277277
made"""
278278
return self.substitute(self.current())
279279

280-
def substitute(self, match):
280+
def substitute(self, match) -> Tuple[int, str]:
281281
"""Returns a cursor offset and line with match substituted in"""
282-
start, end, word = self.completer.locate(
282+
assert self.completer is not None
283+
284+
start, end, _ = self.completer.locate(
283285
self.orig_cursor_offset, self.orig_line
284-
)
286+
) # type: ignore
285287
return (
286288
start + len(match),
287289
self.orig_line[:start] + match + self.orig_line[end:],
288290
)
289291

290-
def is_cseq(self):
292+
def is_cseq(self) -> bool:
291293
return bool(
292294
os.path.commonprefix(self.matches)[len(self.current_word) :]
293295
)
294296

295-
def substitute_cseq(self):
297+
def substitute_cseq(self) -> Tuple[int, str]:
296298
"""Returns a new line by substituting a common sequence in, and update
297299
matches"""
300+
assert self.completer is not None
301+
298302
cseq = os.path.commonprefix(self.matches)
299303
new_cursor_offset, new_line = self.substitute(cseq)
300304
if len(self.matches) == 1:
@@ -307,7 +311,13 @@ def substitute_cseq(self):
307311
self.clear()
308312
return new_cursor_offset, new_line
309313

310-
def update(self, cursor_offset, current_line, matches, completer):
314+
def update(
315+
self,
316+
cursor_offset: int,
317+
current_line: str,
318+
matches: List[str],
319+
completer: autocomplete.BaseCompletionType,
320+
) -> None:
311321
"""Called to reset the match index and update the word being replaced
312322
313323
Should only be called if there's a target to update - otherwise, call
@@ -323,9 +333,9 @@ def update(self, cursor_offset, current_line, matches, completer):
323333
self.index = -1
324334
self.start, self.end, self.current_word = self.completer.locate(
325335
self.orig_cursor_offset, self.orig_line
326-
)
336+
) # type: ignore
327337

328-
def clear(self):
338+
def clear(self) -> None:
329339
self.matches = []
330340
self.orig_cursor_offset = -1
331341
self.orig_line = ""

0 commit comments

Comments
 (0)