Skip to content

Commit 033f5c8

Browse files
authored
Improve pyrepl type-annotation coverage (#119081)
1 parent 100c7ab commit 033f5c8

File tree

6 files changed

+31
-14
lines changed

6 files changed

+31
-14
lines changed

Lib/_pyrepl/_minimal_curses.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class error(Exception):
1717
pass
1818

1919

20-
def _find_clib():
20+
def _find_clib() -> str:
2121
trylibs = ["ncursesw", "ncurses", "curses"]
2222

2323
for lib in trylibs:

Lib/_pyrepl/input.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def empty(self) -> bool:
6060

6161

6262
class KeymapTranslator(InputTranslator):
63-
def __init__(self, keymap, verbose=0, invalid_cls=None, character_cls=None):
63+
def __init__(self, keymap, verbose=False, invalid_cls=None, character_cls=None):
6464
self.verbose = verbose
6565
from .keymap import compile_keymap, parse_keys
6666

@@ -110,5 +110,5 @@ def get(self):
110110
else:
111111
return None
112112

113-
def empty(self):
113+
def empty(self) -> bool:
114114
return not self.results

Lib/_pyrepl/keymap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ def _parse_key1(key, s):
187187
return ret, s
188188

189189

190-
def parse_keys(key):
190+
def parse_keys(key: str) -> list[str]:
191191
s = 0
192192
r = []
193193
while s < len(key):

Lib/_pyrepl/pager.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,14 @@ def tty_pager(text: str, title: str = '') -> None:
7676
fd = sys.stdin.fileno()
7777
old = termios.tcgetattr(fd)
7878
tty.setcbreak(fd)
79-
getchar = lambda: sys.stdin.read(1)
8079
has_tty = True
80+
81+
def getchar() -> str:
82+
return sys.stdin.read(1)
83+
8184
except (ImportError, AttributeError, io.UnsupportedOperation):
82-
getchar = lambda: sys.stdin.readline()[:-1][:1]
85+
def getchar() -> str:
86+
return sys.stdin.readline()[:-1][:1]
8387

8488
try:
8589
try:

Lib/_pyrepl/readline.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
from .types import Callback, Completer, KeySpec, CommandName
4949

5050

51+
MoreLinesCallable = Callable[[str], bool]
52+
53+
5154
__all__ = [
5255
"add_history",
5356
"clear_history",
@@ -94,7 +97,7 @@ class ReadlineAlikeReader(historical_reader.HistoricalReader, CompletingReader):
9497

9598
# Instance fields
9699
config: ReadlineConfig
97-
more_lines: Callable[[str], bool] | None = None
100+
more_lines: MoreLinesCallable | None = None
98101

99102
def __post_init__(self) -> None:
100103
super().__post_init__()
@@ -287,7 +290,7 @@ def input(self, prompt: object = "") -> str:
287290
reader.ps1 = str(prompt)
288291
return reader.readline(startup_hook=self.startup_hook)
289292

290-
def multiline_input(self, more_lines, ps1, ps2):
293+
def multiline_input(self, more_lines: MoreLinesCallable, ps1: str, ps2: str) -> tuple[str, bool]:
291294
"""Read an input on possibly multiple lines, asking for more
292295
lines as long as 'more_lines(unicodetext)' returns an object whose
293296
boolean value is true.

Lib/_pyrepl/unix_console.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,13 @@
4040
from .utils import wlen
4141

4242

43+
TYPE_CHECKING = False
44+
4345
# types
44-
if False:
45-
from typing import IO
46+
if TYPE_CHECKING:
47+
from typing import IO, Literal, overload
48+
else:
49+
overload = lambda func: None
4650

4751

4852
class InvalidTerminal(RuntimeError):
@@ -157,7 +161,13 @@ def __init__(
157161
curses.setupterm(term or None, self.output_fd)
158162
self.term = term
159163

160-
def _my_getstr(cap, optional=0):
164+
@overload
165+
def _my_getstr(cap: str, optional: Literal[False] = False) -> bytes: ...
166+
167+
@overload
168+
def _my_getstr(cap: str, optional: bool) -> bytes | None: ...
169+
170+
def _my_getstr(cap: str, optional: bool = False) -> bytes | None:
161171
r = curses.tigetstr(cap)
162172
if not optional and r is None:
163173
raise InvalidTerminal(
@@ -672,18 +682,18 @@ def __move_y_cuu_cud(self, y):
672682
elif dy < 0:
673683
self.__write_code(self._cuu, -dy)
674684

675-
def __move_x_hpa(self, x):
685+
def __move_x_hpa(self, x: int) -> None:
676686
if x != self.__posxy[0]:
677687
self.__write_code(self._hpa, x)
678688

679-
def __move_x_cub1_cuf1(self, x):
689+
def __move_x_cub1_cuf1(self, x: int) -> None:
680690
dx = x - self.__posxy[0]
681691
if dx > 0:
682692
self.__write_code(self._cuf1 * dx)
683693
elif dx < 0:
684694
self.__write_code(self._cub1 * (-dx))
685695

686-
def __move_x_cub_cuf(self, x):
696+
def __move_x_cub_cuf(self, x: int) -> None:
687697
dx = x - self.__posxy[0]
688698
if dx > 0:
689699
self.__write_code(self._cuf, dx)

0 commit comments

Comments
 (0)