Skip to content

Commit c2f1c39

Browse files
Ben-Regsebastinas
authored andcommitted
Mostly done, but have some questions about the missing attributes in repl.py
1 parent 2ae5c45 commit c2f1c39

File tree

2 files changed

+34
-29
lines changed

2 files changed

+34
-29
lines changed

bpython/cli.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,7 @@ def notify(
352352

353353
def file_prompt(self, s: str) -> Optional[str]:
354354
if self.statusbar:
355-
# This thows a mypy error because repl.py isn't typed yet
356-
return self.statusbar.prompt(s) # type:ignore[no-any-return]
355+
return self.statusbar.prompt(s)
357356
else:
358357
return None
359358

bpython/repl.py

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ def __init__(self, interp: Interpreter, config: Config):
430430
self.funcprops = None
431431
self.arg_pos: Union[str, int, None] = None
432432
self.current_func = None
433-
self.highlighted_paren = None
433+
self.highlighted_paren: Optional[Tuple[Any, List[Tuple[_TokenType, str]]]] = None
434434
self._C: Dict[str, int] = {}
435435
self.prev_block_finished: int = 0
436436
self.interact = Interaction(self.config)
@@ -649,15 +649,16 @@ def get_source_of_current_name(self) -> str:
649649
current name in the current input line. Throw `SourceNotFound` if the
650650
source cannot be found."""
651651

652-
obj = self.current_func
652+
obj: Optional[Callable] = self.current_func
653653
try:
654654
if obj is None:
655655
line = self.current_line
656656
if not line.strip():
657657
raise SourceNotFound(_("Nothing to get source of"))
658658
if inspection.is_eval_safe_name(line):
659659
obj = self.get_object(line)
660-
return inspect.getsource(obj)
660+
# Ignoring the next mypy error because we want this to fail if obj is None
661+
return inspect.getsource(obj) # type:ignore[arg-type]
661662
except (AttributeError, NameError) as e:
662663
msg = _("Cannot get source: %s") % (e,)
663664
except OSError as e:
@@ -724,28 +725,31 @@ def complete(self, tab: bool = False) -> Optional[bool]:
724725
self.matches_iter.clear()
725726
return bool(self.funcprops)
726727

727-
self.matches_iter.update(
728-
self.cursor_offset, self.current_line, matches, completer
729-
)
728+
if completer:
729+
self.matches_iter.update(
730+
self.cursor_offset, self.current_line, matches, completer
731+
)
730732

731-
if len(matches) == 1:
732-
if tab:
733-
# if this complete is being run for a tab key press, substitute
734-
# common sequence
735-
(
736-
self._cursor_offset,
737-
self._current_line,
738-
) = self.matches_iter.substitute_cseq()
739-
return Repl.complete(self) # again for
740-
elif self.matches_iter.current_word == matches[0]:
741-
self.matches_iter.clear()
742-
return False
743-
return completer.shown_before_tab
733+
if len(matches) == 1:
734+
if tab:
735+
# if this complete is being run for a tab key press, substitute
736+
# common sequence
737+
(
738+
self._cursor_offset,
739+
self._current_line,
740+
) = self.matches_iter.substitute_cseq()
741+
return Repl.complete(self) # again for
742+
elif self.matches_iter.current_word == matches[0]:
743+
self.matches_iter.clear()
744+
return False
745+
return completer.shown_before_tab
744746

747+
else:
748+
return tab or completer.shown_before_tab
745749
else:
746-
return tab or completer.shown_before_tab
750+
return False
747751

748-
def format_docstring(self, docstring: str, width: int, height: int) -> str:
752+
def format_docstring(self, docstring: str, width: int, height: int) -> List[str]:
749753
"""Take a string and try to format it into a sane list of strings to be
750754
put into the suggestion box."""
751755

@@ -878,11 +882,13 @@ def pastebin(self, s=None) -> Optional[str]:
878882
_("Pastebin buffer? (y/N) ")
879883
):
880884
self.interact.notify(_("Pastebin aborted."))
885+
return None
881886
else:
882887
return self.do_pastebin(s)
883888

884889
def do_pastebin(self, s) -> Optional[str]:
885890
"""Actually perform the upload."""
891+
paste_url: str
886892
if s == self.prev_pastebin_content:
887893
self.interact.notify(
888894
_("Duplicate pastebin. Previous URL: %s. " "Removal URL: %s")
@@ -896,7 +902,7 @@ def do_pastebin(self, s) -> Optional[str]:
896902
paste_url, removal_url = self.paster.paste(s)
897903
except PasteFailed as e:
898904
self.interact.notify(_("Upload failed: %s") % e)
899-
return
905+
return None
900906

901907
self.prev_pastebin_content = s
902908
self.prev_pastebin_url = paste_url
@@ -923,7 +929,7 @@ def push(self, s, insert_into_history=True) -> bool:
923929
if insert_into_history:
924930
self.insert_into_history(s)
925931

926-
more = self.interp.runsource("\n".join(self.buffer))
932+
more: bool = self.interp.runsource("\n".join(self.buffer))
927933

928934
if not more:
929935
self.buffer = []
@@ -1028,7 +1034,7 @@ def tokenize(self, s, newline=False) -> List[Tuple[_TokenType, str]]:
10281034
cursor = len(source) - self.cpos
10291035
if self.cpos:
10301036
cursor += 1
1031-
stack = list()
1037+
stack: List[Any] = list()
10321038
all_tokens = list(Python3Lexer().get_tokens(source))
10331039
# Unfortunately, Pygments adds a trailing newline and strings with
10341040
# no size, so strip them
@@ -1037,8 +1043,8 @@ def tokenize(self, s, newline=False) -> List[Tuple[_TokenType, str]]:
10371043
all_tokens[-1] = (all_tokens[-1][0], all_tokens[-1][1].rstrip("\n"))
10381044
line = pos = 0
10391045
parens = dict(zip("{([", "})]"))
1040-
line_tokens = list()
1041-
saved_tokens = list()
1046+
line_tokens: List[Tuple[_TokenType, str]] = list()
1047+
saved_tokens: List[Tuple[_TokenType, str]] = list()
10421048
search_for_paren = True
10431049
for (token, value) in split_lines(all_tokens):
10441050
pos += len(value)
@@ -1174,7 +1180,7 @@ def edit_config(self):
11741180
def next_indentation(line, tab_length) -> int:
11751181
"""Given a code line, return the indentation of the next line."""
11761182
line = line.expandtabs(tab_length)
1177-
indentation = (len(line) - len(line.lstrip(" "))) // tab_length
1183+
indentation: int = (len(line) - len(line.lstrip(" "))) // tab_length
11781184
if line.rstrip().endswith(":"):
11791185
indentation += 1
11801186
elif indentation >= 1:

0 commit comments

Comments
 (0)