Skip to content

Commit

Permalink
Additional Pylint fixes (#268)
Browse files Browse the repository at this point in the history
- Added type hints for `_time_left()`, `_read_until`, and `DEFAULT_ESCDELAY` in `keyboard.pyi`
- Removed `functools.partial()` in `Terminal.inkey()`
  - Pylint complained about no return value
  - It doesn't seem necessary anymore. I think at one point it consolidated some complicated code, but now these calls are pretty simple
- Ignore two cases where Pylint reported a false positive
  • Loading branch information
avylove committed Apr 5, 2024
1 parent e904de3 commit 75a34ef
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
19 changes: 18 additions & 1 deletion blessed/keyboard.pyi
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
"""Type hints for 'keyboard awareness'"""

# std imports
from typing import TYPE_CHECKING, Set, Dict, Type, Mapping, TypeVar, Iterable, Optional, OrderedDict
from typing import (TYPE_CHECKING,
Set,
Dict,
Type,
Match,
Tuple,
Mapping,
TypeVar,
Iterable,
Optional,
OrderedDict)

if TYPE_CHECKING:
# local
Expand Down Expand Up @@ -31,3 +41,10 @@ def get_leading_prefixes(sequences: Iterable[str]) -> Set[str]: ...
def resolve_sequence(
text: str, mapper: Mapping[str, int], codes: Mapping[int, str]
) -> Keystroke: ...
def _time_left(stime: float, timeout: Optional[float]) -> Optional[float]: ...
def _read_until(
term: 'Terminal', pattern: str, timeout: Optional[float]
) -> Tuple[Optional[Match[str]], str]: ...


DEFAULT_ESCDELAY: float
14 changes: 5 additions & 9 deletions blessed/terminal.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import struct
import platform
import warnings
import functools
import contextlib
import collections

Expand Down Expand Up @@ -529,7 +528,7 @@ def _query_response(self, query_str, response_re, timeout):
self.stream.flush()

# Wait for response
match, data = _read_until(term=self,
match, data = _read_until(term=self, # pylint: disable=unpacking-non-sequence
pattern=response_re,
timeout=timeout)

Expand Down Expand Up @@ -1463,9 +1462,6 @@ def inkey(self, timeout=None, esc_delay=DEFAULT_ESCDELAY):
_`ncurses(3)`: https://www.man7.org/linux/man-pages/man3/ncurses.3x.html
"""
resolve = functools.partial(resolve_sequence,
mapper=self._keymap,
codes=self._keycodes)
stime = time.time()

# re-buffer previously received keystrokes,
Expand All @@ -1478,14 +1474,14 @@ def inkey(self, timeout=None, esc_delay=DEFAULT_ESCDELAY):
ucs += self.getch()

# decode keystroke, if any
ks = resolve(text=ucs)
ks = resolve_sequence(ucs, self._keymap, self._keycodes)

# so long as the most immediately received or buffered keystroke is
# incomplete, (which may be a multibyte encoding), block until until
# one is received.
while not ks and self.kbhit(timeout=_time_left(stime, timeout)):
ucs += self.getch()
ks = resolve(text=ucs)
ks = resolve_sequence(ucs, self._keymap, self._keycodes)

# handle escape key (KEY_ESCAPE) vs. escape sequence (like those
# that begin with \x1b[ or \x1bO) up to esc_delay when
Expand All @@ -1500,10 +1496,10 @@ def inkey(self, timeout=None, esc_delay=DEFAULT_ESCDELAY):
if ks.code == self.KEY_ESCAPE:
esctime = time.time()
while (ks.code == self.KEY_ESCAPE and
ucs in self._keymap_prefixes and
ucs in self._keymap_prefixes and # pylint: disable=unsupported-membership-test
self.kbhit(timeout=_time_left(esctime, esc_delay))):
ucs += self.getch()
ks = resolve(text=ucs)
ks = resolve_sequence(ucs, self._keymap, self._keycodes)

# buffer any remaining text received
self.ungetch(ucs[len(ks):])
Expand Down

0 comments on commit 75a34ef

Please sign in to comment.