Skip to content

Commit

Permalink
Improve type hints and linting
Browse files Browse the repository at this point in the history
  • Loading branch information
ESultanik committed Feb 14, 2022
1 parent 9c2d20b commit 037e197
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions polyfile/repl.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import readline
import sys
import traceback
from typing import Any, Callable, Dict, Generic, Iterable, Iterator, List, Optional, Set, Tuple, Type, TypeVar, Union
from typing import Any, Callable, Dict, Iterable, Iterator, List, Optional, Set, Tuple, Type, TypeVar, Union

from .logger import getStatusLogger

Expand Down Expand Up @@ -215,27 +215,27 @@ def add_completer(cls, for_command_name: str, completer: Callable[["REPL", str,
cls._completers[for_command_name] = completer

@property
def command_types(mcls) -> Iterator[Tuple[str, Type[Command]]]:
def command_types(self) -> Iterator[Tuple[str, Type[Command]]]:
yielded_names = set()
for cls in mcls.mro():
for cls in self.mro():
if isinstance(cls, REPLMeta):
for name, cmd in cls._commands.items():
if name not in yielded_names:
yield name, cmd
yielded_names.add(name)

@property
def completers(mcls) -> Iterator[Tuple[str, Callable[["REPL", str, int, int], List[str]]]]:
def completers(self) -> Iterator[Tuple[str, Callable[["REPL", str, int, int], List[str]]]]:
yielded_names = set()
for cls in mcls.mro():
for cls in self.mro():
if isinstance(cls, REPLMeta):
for name, completer in cls._completers.items():
for name, cmd_completer in cls._completers.items():
if name not in yielded_names:
yield name, completer
yield name, cmd_completer
yielded_names.add(name)

def __new__(mcls, name, bases, namespace, **kwargs):
cls = super().__new__(mcls, name, bases, namespace, **kwargs)
def __new__(mcs, name, bases, namespace, **kwargs):
cls = super().__new__(mcs, name, bases, namespace, **kwargs)
cls._commands = {}
cls._completers = {}
for func_name, func in namespace.items():
Expand Down Expand Up @@ -273,11 +273,11 @@ def __call__(self, args: str, begin_idx: Optional[int] = None, end_idx: Optional

class REPL(metaclass=REPLMeta):
def __init__(self, name: str, prompt: Optional[str] = None,
completer: Optional[Callable[[str, int], Iterable[str]]] = None):
completer: Optional[Callable[[str, int], Optional[str]]] = None):
self.name: str = name
self._prev_history_length: int = 0
if completer is None:
self.completer: Callable[[str, int], Iterable[str]] = REPLCompleter(self).complete
self.completer: Callable[[str, int], Optional[str]] = REPLCompleter(self).complete
else:
self.completer = completer
self.commands: Dict[str, Command] = {
Expand All @@ -299,9 +299,9 @@ def command_names(self) -> Iterator[str]:
yield from cmd.aliases

def get_completer(self, for_command: Command) -> Callable[[str, int, int], List[str]]:
for cmd_name, completer in self.__class__.completers:
for cmd_name, cmd_completer in self.__class__.completers:
if cmd_name == for_command.name:
return partial(completer, self)
return partial(cmd_completer, self) # type: ignore
return for_command.complete

@arg_completer(for_command="help")
Expand Down Expand Up @@ -410,7 +410,7 @@ def input(self, prompt: str = "") -> str:
prev_completer = readline.get_completer()
readline.set_completer(self.completer)
try:
return input(self.repl_prompt)
return input(prompt)
except EOFError:
# the user pressed ^D to quit
return self.handle_eof()
Expand All @@ -425,6 +425,7 @@ def write(self, message: Any, bold: bool = False, dim: bool = False, color: Opti
def handle_eof(self) -> str:
# called when the user presses ^D on input()
exit(0)
return ""

def before_prompt(self):
pass
Expand Down Expand Up @@ -507,14 +508,11 @@ def traverse(self, tokens, tree):
return []
if len(tokens) == 1:
return [x+' ' for x in tree if x.startswith(tokens[0])]
else:
if tokens[0] in tree.keys():
return self.traverse(tokens[1:],tree[tokens[0]])
else:
return []
elif tokens[0] in tree.keys():
return self.traverse(tokens[1:],tree[tokens[0]])
return []

def complete(self, text: str, state: int):
def complete(self, text: str, state: int) -> Optional[str]:
commandline = readline.get_line_buffer()
space_index = commandline.find(" ")
if space_index < 0 or (space_index > 0 and readline.get_endidx() < space_index):
Expand All @@ -538,6 +536,7 @@ def complete(self, text: str, state: int):
if space_index > 0:
command_name, args = commandline[:space_index], commandline[space_index + 1:]
else:
command_name = ""
args = ""
try:
command = self.repl.get_command(command_name)
Expand All @@ -547,12 +546,13 @@ def complete(self, text: str, state: int):
try:
possibilities = completer(
args, readline.get_begidx() - space_index - 1, readline.get_endidx() - space_index - 1
) + [None]
) + [None] # type: ignore
except Exception as e:
traceback.print_tb(e.__traceback__)
log.warning(f"Command {completer!r}({args!r}, "
f"{readline.get_begidx() - space_index - 1}, {readline.get_endidx() - space_index - 1}) "
f"raised an exception: {e!r}")
return None

if state < len(possibilities):
if state == 0 and len(possibilities) == 2:
Expand Down

0 comments on commit 037e197

Please sign in to comment.