Skip to content

Commit 8da0381

Browse files
Mypy types for completion
We're not checking these in CI nor are we providing a config file yet. I used mypy 0.910 run directly on bpython/autocomplete.py and still had 4 errors after these changes.
1 parent 78ead4c commit 8da0381

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

bpython/autocomplete.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import builtins
3434

3535
from enum import Enum
36-
from typing import Any, Dict, Iterator, List, Match, NoReturn, Set, Union
36+
from typing import Any, Dict, Iterator, List, Match, NoReturn, Set, Union, Literal, Tuple
3737
from . import inspection
3838
from . import line as lineparts
3939
from .line import LinePart
@@ -180,7 +180,7 @@ def few_enough_underscores(current, match) -> bool:
180180
return not match.startswith("_")
181181

182182

183-
def method_match_none(word, size, text) -> False:
183+
def method_match_none(word, size, text) -> Literal[False]:
184184
return False
185185

186186

@@ -214,7 +214,8 @@ def __init__(
214214
self._shown_before_tab = shown_before_tab
215215
self.method_match = MODES_MAP[mode]
216216

217-
def matches(self, cursor_offset, line, **kwargs) -> NoReturn:
217+
@abc.abstractmethod
218+
def matches(self, cursor_offset: int, line: str, **kwargs) -> Union[Set[str], None]:
218219
"""Returns a list of possible matches given a line and cursor, or None
219220
if this completion type isn't applicable.
220221
@@ -232,7 +233,8 @@ def matches(self, cursor_offset, line, **kwargs) -> NoReturn:
232233
"""
233234
raise NotImplementedError
234235

235-
def locate(self, cursor_offset, line) -> NoReturn:
236+
@abc.abstractmethod
237+
def locate(self, cursor_offset: int, line: str) -> Union[LinePart, None]:
236238
"""Returns a Linepart namedtuple instance or None given cursor and line
237239
238240
A Linepart namedtuple contains a start, stop, and word. None is
@@ -243,9 +245,10 @@ def locate(self, cursor_offset, line) -> NoReturn:
243245
def format(self, word):
244246
return word
245247

246-
def substitute(self, cursor_offset, line, match) -> NoReturn:
248+
def substitute(self, cursor_offset, line, match) -> Tuple[int, str]:
247249
"""Returns a cursor offset and line with match swapped in"""
248250
lpart = self.locate(cursor_offset, line)
251+
assert lpart
249252
offset = lpart.start + len(match)
250253
changed_line = line[: lpart.start] + match + line[lpart.stop :]
251254
return offset, changed_line
@@ -269,16 +272,17 @@ def __init__(self, completers, mode=AutocompleteModes.SIMPLE) -> None:
269272

270273
super().__init__(True, mode)
271274

272-
def locate(self, current_offset, line) -> Union[None, NoReturn]:
275+
def locate(self, current_offset: int, line: str) -> Union[None, NoReturn]:
273276
for completer in self._completers:
274277
return_value = completer.locate(current_offset, line)
275278
if return_value is not None:
276279
return return_value
280+
return None
277281

278282
def format(self, word):
279283
return self._completers[0].format(word)
280284

281-
def matches(self, cursor_offset, line, **kwargs) -> Union[None, Set]:
285+
def matches(self, cursor_offset: int, line: str, **kwargs) -> Union[None, Set]:
282286
return_value = None
283287
all_matches = set()
284288
for completer in self._completers:
@@ -344,7 +348,7 @@ class AttrCompletion(BaseCompletionType):
344348

345349
attr_matches_re = LazyReCompile(r"(\w+(\.\w+)*)\.(\w*)")
346350

347-
def matches(self, cursor_offset, line, **kwargs) -> Union[None, Dict]:
351+
def matches(self, cursor_offset, line, **kwargs) -> Union[None, Set]:
348352
if "locals_" not in kwargs:
349353
return None
350354
locals_ = kwargs["locals_"]
@@ -427,15 +431,17 @@ def list_attributes(self, obj) -> List[str]:
427431

428432

429433
class DictKeyCompletion(BaseCompletionType):
430-
def matches(self, cursor_offset, line, **kwargs) -> Union[None, Dict]:
434+
def matches(self, cursor_offset, line, **kwargs) -> Union[None, Set]:
431435
if "locals_" not in kwargs:
432436
return None
433437
locals_ = kwargs["locals_"]
434438

435439
r = self.locate(cursor_offset, line)
436440
if r is None:
437441
return None
438-
_, _, dexpr = lineparts.current_dict(cursor_offset, line)
442+
curDictParts = lineparts.current_dict(cursor_offset, line)
443+
assert curDictParts, 'current_dict when .locate() truthy'
444+
_, _, dexpr = curDictParts
439445
try:
440446
obj = safe_eval(dexpr, locals_)
441447
except EvaluationError:
@@ -456,7 +462,7 @@ def format(self, match):
456462

457463

458464
class MagicMethodCompletion(BaseCompletionType):
459-
def matches(self, cursor_offset, line, **kwargs) -> Union[None, Dict]:
465+
def matches(self, cursor_offset, line, **kwargs) -> Union[None, Set]:
460466
if "current_block" not in kwargs:
461467
return None
462468
current_block = kwargs["current_block"]
@@ -508,7 +514,7 @@ def locate(self, current_offset, line) -> Union[LinePart, None]:
508514

509515

510516
class ParameterNameCompletion(BaseCompletionType):
511-
def matches(self, cursor_offset, line, **kwargs) -> Union[None, Dict]:
517+
def matches(self, cursor_offset, line, **kwargs) -> Union[None, Set]:
512518
if "argspec" not in kwargs:
513519
return None
514520
argspec = kwargs["argspec"]
@@ -538,7 +544,7 @@ class ExpressionAttributeCompletion(AttrCompletion):
538544
def locate(self, current_offset, line) -> Union[LinePart, None]:
539545
return lineparts.current_expression_attribute(current_offset, line)
540546

541-
def matches(self, cursor_offset, line, **kwargs) -> Union[Set, Dict, None]:
547+
def matches(self, cursor_offset, line, **kwargs) -> Union[Set, None]:
542548
if "locals_" not in kwargs:
543549
return None
544550
locals_ = kwargs["locals_"]
@@ -547,6 +553,7 @@ def matches(self, cursor_offset, line, **kwargs) -> Union[Set, Dict, None]:
547553
locals_ = __main__.__dict__
548554

549555
attr = self.locate(cursor_offset, line)
556+
assert attr, 'locate was already truthy for the same call'
550557

551558
try:
552559
obj = evaluate_current_expression(cursor_offset, line, locals_)
@@ -570,7 +577,7 @@ def matches(self, cursor_offset, line, **kwargs) -> None:
570577
else:
571578

572579
class JediCompletion(BaseCompletionType):
573-
def matches(self, cursor_offset, line, **kwargs) -> Union[None, Dict]:
580+
def matches(self, cursor_offset, line, **kwargs) -> Union[None, Set]:
574581
if "history" not in kwargs:
575582
return None
576583
history = kwargs["history"]
@@ -616,7 +623,7 @@ def locate(self, cursor_offset, line) -> LinePart:
616623
return LinePart(start, end, line[start:end])
617624

618625
class MultilineJediCompletion(JediCompletion):
619-
def matches(self, cursor_offset, line, **kwargs) -> Union[Dict, None]:
626+
def matches(self, cursor_offset, line, **kwargs) -> Union[Set, None]:
620627
if "current_block" not in kwargs or "history" not in kwargs:
621628
return None
622629
current_block = kwargs["current_block"]

0 commit comments

Comments
 (0)