]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] Improve `pyrepl` type-annotation coverage (GH-119081) (#119415)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 22 May 2024 18:38:32 +0000 (20:38 +0200)
committerGitHub <noreply@github.com>
Wed, 22 May 2024 18:38:32 +0000 (18:38 +0000)
(cherry picked from commit 033f5c87f1f876088701d1ae078dc39c41177d4a)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Lib/_pyrepl/_minimal_curses.py
Lib/_pyrepl/input.py
Lib/_pyrepl/keymap.py
Lib/_pyrepl/pager.py
Lib/_pyrepl/readline.py
Lib/_pyrepl/unix_console.py

index 0757fb2c664addfdba4ba542e90ffcf3599ea4bd..849617bf7585e469e1ee7a89fbed50a8f56772c0 100644 (file)
@@ -17,7 +17,7 @@ class error(Exception):
     pass
 
 
-def _find_clib():
+def _find_clib() -> str:
     trylibs = ["ncursesw", "ncurses", "curses"]
 
     for lib in trylibs:
index 300e16d1d2544148ab90a79d2cc95897aae99f95..21c24eb5cde3e36459506f23f6e29e90472a5f92 100644 (file)
@@ -60,7 +60,7 @@ class InputTranslator(ABC):
 
 
 class KeymapTranslator(InputTranslator):
-    def __init__(self, keymap, verbose=0, invalid_cls=None, character_cls=None):
+    def __init__(self, keymap, verbose=False, invalid_cls=None, character_cls=None):
         self.verbose = verbose
         from .keymap import compile_keymap, parse_keys
 
@@ -110,5 +110,5 @@ class KeymapTranslator(InputTranslator):
         else:
             return None
 
-    def empty(self):
+    def empty(self) -> bool:
         return not self.results
index 9f1aff88e06f6a003519170e74c73fedfdad0035..a303589280e7f192571fa07c7581b77af6798f69 100644 (file)
@@ -190,7 +190,7 @@ def _parse_key1(key, s):
     return ret, s
 
 
-def parse_keys(key):
+def parse_keys(key: str) -> list[str]:
     s = 0
     r = []
     while s < len(key):
index 1c8c1bb13925e3a7bb345036f5310132be2375d3..1ac733ed3573a4b9bea458785d39ea81cbf5abb9 100644 (file)
@@ -76,10 +76,14 @@ def tty_pager(text: str, title: str = '') -> None:
         fd = sys.stdin.fileno()
         old = termios.tcgetattr(fd)
         tty.setcbreak(fd)
-        getchar = lambda: sys.stdin.read(1)
         has_tty = True
+
+        def getchar() -> str:
+            return sys.stdin.read(1)
+
     except (ImportError, AttributeError, io.UnsupportedOperation):
-        getchar = lambda: sys.stdin.readline()[:-1][:1]
+        def getchar() -> str:
+            return sys.stdin.readline()[:-1][:1]
 
     try:
         try:
index e3444d90477d3527747f5ca6af784b8fcab87f80..787dbc06e58e18661d45cf16995642e8003dfd01 100644 (file)
@@ -49,6 +49,9 @@ from collections.abc import Callable, Collection
 from .types import Callback, Completer, KeySpec, CommandName
 
 
+MoreLinesCallable = Callable[[str], bool]
+
+
 __all__ = [
     "add_history",
     "clear_history",
@@ -95,7 +98,7 @@ class ReadlineAlikeReader(historical_reader.HistoricalReader, CompletingReader):
 
     # Instance fields
     config: ReadlineConfig
-    more_lines: Callable[[str], bool] | None = None
+    more_lines: MoreLinesCallable | None = None
 
     def __post_init__(self) -> None:
         super().__post_init__()
@@ -288,7 +291,7 @@ class _ReadlineWrapper:
         reader.ps1 = str(prompt)
         return reader.readline(startup_hook=self.startup_hook)
 
-    def multiline_input(self, more_lines, ps1, ps2):
+    def multiline_input(self, more_lines: MoreLinesCallable, ps1: str, ps2: str) -> tuple[str, bool]:
         """Read an input on possibly multiple lines, asking for more
         lines as long as 'more_lines(unicodetext)' returns an object whose
         boolean value is true.
index c213012bfb2bde89770b83fa0d7d92294441b7f5..ec7d0636b9aeb3faa6d69681e43a3e6e600000fe 100644 (file)
@@ -40,9 +40,13 @@ from .unix_eventqueue import EventQueue
 from .utils import wlen
 
 
+TYPE_CHECKING = False
+
 # types
-if False:
-    from typing import IO
+if TYPE_CHECKING:
+    from typing import IO, Literal, overload
+else:
+    overload = lambda func: None
 
 
 class InvalidTerminal(RuntimeError):
@@ -157,7 +161,13 @@ class UnixConsole(Console):
         curses.setupterm(term or None, self.output_fd)
         self.term = term
 
-        def _my_getstr(cap, optional=0):
+        @overload
+        def _my_getstr(cap: str, optional: Literal[False] = False) -> bytes: ...
+
+        @overload
+        def _my_getstr(cap: str, optional: bool) -> bytes | None: ...
+
+        def _my_getstr(cap: str, optional: bool = False) -> bytes | None:
             r = curses.tigetstr(cap)
             if not optional and r is None:
                 raise InvalidTerminal(
@@ -672,18 +682,18 @@ class UnixConsole(Console):
         elif dy < 0:
             self.__write_code(self._cuu, -dy)
 
-    def __move_x_hpa(self, x):
+    def __move_x_hpa(self, x: int) -> None:
         if x != self.__posxy[0]:
             self.__write_code(self._hpa, x)
 
-    def __move_x_cub1_cuf1(self, x):
+    def __move_x_cub1_cuf1(self, x: int) -> None:
         dx = x - self.__posxy[0]
         if dx > 0:
             self.__write_code(self._cuf1 * dx)
         elif dx < 0:
             self.__write_code(self._cub1 * (-dx))
 
-    def __move_x_cub_cuf(self, x):
+    def __move_x_cub_cuf(self, x: int) -> None:
         dx = x - self.__posxy[0]
         if dx > 0:
             self.__write_code(self._cuf, dx)