Skip to content

Mypy types for completion #928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 7, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
types for Python 3.6
  • Loading branch information
thomasballinger committed Oct 7, 2021
commit f756d8f848b841a60b1201152b706067e4e02ae4
22 changes: 15 additions & 7 deletions bpython/autocomplete.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import builtins

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


def method_match_none(word, size, text) -> Literal[False]:
def method_match_none(word, size, text) -> bool:
return False


Expand Down Expand Up @@ -215,7 +215,9 @@ def __init__(
self.method_match = MODES_MAP[mode]

@abc.abstractmethod
def matches(self, cursor_offset: int, line: str, **kwargs) -> Union[Set[str], None]:
def matches(
self, cursor_offset: int, line: str, **kwargs
) -> Union[Set[str], None]:
"""Returns a list of possible matches given a line and cursor, or None
if this completion type isn't applicable.

Expand Down Expand Up @@ -282,7 +284,9 @@ def locate(self, current_offset: int, line: str) -> Union[None, NoReturn]:
def format(self, word):
return self._completers[0].format(word)

def matches(self, cursor_offset: int, line: str, **kwargs) -> Union[None, Set]:
def matches(
self, cursor_offset: int, line: str, **kwargs
) -> Union[None, Set]:
return_value = None
all_matches = set()
for completer in self._completers:
Expand Down Expand Up @@ -440,7 +444,7 @@ def matches(self, cursor_offset, line, **kwargs) -> Union[None, Set]:
if r is None:
return None
curDictParts = lineparts.current_dict(cursor_offset, line)
assert curDictParts, 'current_dict when .locate() truthy'
assert curDictParts, "current_dict when .locate() truthy"
_, _, dexpr = curDictParts
try:
obj = safe_eval(dexpr, locals_)
Expand Down Expand Up @@ -553,7 +557,7 @@ def matches(self, cursor_offset, line, **kwargs) -> Union[Set, None]:
locals_ = __main__.__dict__

attr = self.locate(cursor_offset, line)
assert attr, 'locate was already truthy for the same call'
assert attr, "locate was already truthy for the same call"

try:
obj = evaluate_current_expression(cursor_offset, line, locals_)
Expand All @@ -577,6 +581,8 @@ def matches(self, cursor_offset, line, **kwargs) -> None:
else:

class JediCompletion(BaseCompletionType):
_orig_start: Union[int, None]

def matches(self, cursor_offset, line, **kwargs) -> Union[None, Set]:
if "history" not in kwargs:
return None
Expand All @@ -603,6 +609,7 @@ def matches(self, cursor_offset, line, **kwargs) -> Union[None, Set]:
else:
self._orig_start = None
return None
assert isinstance(self._orig_start, int)

first_letter = line[self._orig_start : self._orig_start + 1]

Expand All @@ -617,7 +624,8 @@ def matches(self, cursor_offset, line, **kwargs) -> Union[None, Set]:
# case-sensitive matches only
return {m for m in matches if m.startswith(first_letter)}

def locate(self, cursor_offset, line) -> LinePart:
def locate(self, cursor_offset: int, line: str) -> LinePart:
assert isinstance(self._orig_start, int)
start = self._orig_start
end = cursor_offset
return LinePart(start, end, line[start:end])
Expand Down