Skip to content

Commit cd39da7

Browse files
[3.13] Improve pyrepl type-annotation coverage (GH-119081) (#119415)
(cherry picked from commit 033f5c8) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent bfd9c3e commit cd39da7

File tree

6 files changed

+31
-14
lines changed

6 files changed

+31
-14
lines changed

Lib/_pyrepl/_minimal_curses.py

+1-1
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

+2-2
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def _parse_key1(key, s):
190190
return ret, s
191191

192192

193-
def parse_keys(key):
193+
def parse_keys(key: str) -> list[str]:
194194
s = 0
195195
r = []
196196
while s < len(key):

Lib/_pyrepl/pager.py

+6-2
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

+5-2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@
4949
from .types import Callback, Completer, KeySpec, CommandName
5050

5151

52+
MoreLinesCallable = Callable[[str], bool]
53+
54+
5255
__all__ = [
5356
"add_history",
5457
"clear_history",
@@ -95,7 +98,7 @@ class ReadlineAlikeReader(historical_reader.HistoricalReader, CompletingReader):
9598

9699
# Instance fields
97100
config: ReadlineConfig
98-
more_lines: Callable[[str], bool] | None = None
101+
more_lines: MoreLinesCallable | None = None
99102

100103
def __post_init__(self) -> None:
101104
super().__post_init__()
@@ -288,7 +291,7 @@ def input(self, prompt: object = "") -> str:
288291
reader.ps1 = str(prompt)
289292
return reader.readline(startup_hook=self.startup_hook)
290293

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

Lib/_pyrepl/unix_console.py

+16-6
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)