Skip to content

Commit ceb80a2

Browse files
Fix several typing issues, revealed after the latest mypy release.
1 parent 5f2268d commit ceb80a2

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

prompt_toolkit/application/application.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@
103103
_AppResult = TypeVar("_AppResult")
104104
ApplicationEventHandler = Callable[["Application[_AppResult]"], None]
105105

106+
_SIGWINCH = getattr(signal, "SIGWINCH", None)
107+
_SIGTSTP = getattr(signal, "SIGTSTP", None)
108+
106109

107110
class Application(Generic[_AppResult]):
108111
"""
@@ -686,10 +689,9 @@ def flush_input() -> None:
686689
self._redraw()
687690
self._start_auto_refresh_task()
688691

689-
has_sigwinch = hasattr(signal, "SIGWINCH") and in_main_thread()
690-
if has_sigwinch:
691-
previous_winch_handler = signal.getsignal(signal.SIGWINCH)
692-
loop.add_signal_handler(signal.SIGWINCH, self._on_resize)
692+
if _SIGWINCH is not None and in_main_thread():
693+
previous_winch_handler = signal.getsignal(_SIGWINCH)
694+
loop.add_signal_handler(_SIGWINCH, self._on_resize)
693695
if previous_winch_handler is None:
694696
# In some situations we receive `None`. This is
695697
# however not a valid value for passing to
@@ -727,9 +729,9 @@ def flush_input() -> None:
727729
if self.input.responds_to_cpr:
728730
await self.renderer.wait_for_cpr_responses()
729731

730-
if has_sigwinch:
731-
loop.remove_signal_handler(signal.SIGWINCH)
732-
signal.signal(signal.SIGWINCH, previous_winch_handler)
732+
if _SIGWINCH is not None:
733+
loop.remove_signal_handler(_SIGWINCH)
734+
signal.signal(_SIGWINCH, previous_winch_handler)
733735

734736
# Wait for the run-in-terminals to terminate.
735737
previous_run_in_terminal_f = self._running_in_terminal_f
@@ -995,18 +997,18 @@ def suspend_to_background(self, suspend_group: bool = True) -> None:
995997
"""
996998
# Only suspend when the operating system supports it.
997999
# (Not on Windows.)
998-
if hasattr(signal, "SIGTSTP"):
1000+
if _SIGTSTP is not None:
9991001

10001002
def run() -> None:
1001-
# Send `SIGSTP` to own process.
1003+
# Send `SIGTSTP` to own process.
10021004
# This will cause it to suspend.
10031005

10041006
# Usually we want the whole process group to be suspended. This
10051007
# handles the case when input is piped from another process.
10061008
if suspend_group:
1007-
os.kill(0, signal.SIGTSTP)
1009+
os.kill(0, _SIGTSTP)
10081010
else:
1009-
os.kill(os.getpid(), signal.SIGTSTP)
1011+
os.kill(os.getpid(), _SIGTSTP)
10101012

10111013
run_in_terminal(run)
10121014

prompt_toolkit/document.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,9 @@ def find_matching_bracket_position(
825825
"""
826826

827827
# Look for a match.
828-
for A, B in "()", "[]", "{}", "<>":
828+
for pair in "()", "[]", "{}", "<>":
829+
A = pair[0]
830+
B = pair[1]
829831
if self.current_char == A:
830832
return self.find_enclosing_bracket_right(A, B, end_pos=end_pos) or 0
831833
elif self.current_char == B:

prompt_toolkit/shortcuts/progress_bar/base.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import traceback
1616
from asyncio import get_event_loop, new_event_loop, set_event_loop
1717
from typing import (
18+
TYPE_CHECKING,
1819
Generic,
1920
Iterable,
2021
List,
@@ -65,6 +66,8 @@
6566

6667
E = KeyPressEvent
6768

69+
_SIGWINCH = getattr(signal, "SIGWINCH", None)
70+
6871

6972
def create_key_bindings() -> KeyBindings:
7073
"""
@@ -142,11 +145,15 @@ def __init__(
142145

143146
self._loop = get_event_loop()
144147
self._app_loop = new_event_loop()
145-
self._previous_winch_handler = (
146-
signal.getsignal(signal.SIGWINCH) if hasattr(signal, "SIGWINCH") else None
147-
)
148+
149+
self._previous_winch_handler = None
148150
self._has_sigwinch = False
149151

152+
if TYPE_CHECKING:
153+
# Infer type from getsignal result, as defined in typeshed. Too
154+
# complex to repeat here.
155+
self._previous_winch_handler = signal.getsignal(_SIGWINCH)
156+
150157
def __enter__(self) -> "ProgressBar":
151158
# Create UI Application.
152159
title_toolbar = ConditionalContainer(
@@ -225,10 +232,10 @@ def run() -> None:
225232

226233
# Attach WINCH signal handler in main thread.
227234
# (Interrupt that we receive during resize events.)
228-
self._has_sigwinch = hasattr(signal, "SIGWINCH") and in_main_thread()
235+
self._has_sigwinch = _SIGWINCH is not None and in_main_thread()
229236
if self._has_sigwinch:
230-
self._previous_winch_handler = signal.getsignal(signal.SIGWINCH)
231-
self._loop.add_signal_handler(signal.SIGWINCH, self.invalidate)
237+
self._previous_winch_handler = signal.getsignal(_SIGWINCH)
238+
self._loop.add_signal_handler(_SIGWINCH, self.invalidate)
232239

233240
return self
234241

@@ -239,8 +246,8 @@ def __exit__(self, *a: object) -> None:
239246

240247
# Remove WINCH handler.
241248
if self._has_sigwinch:
242-
self._loop.remove_signal_handler(signal.SIGWINCH)
243-
signal.signal(signal.SIGWINCH, self._previous_winch_handler)
249+
self._loop.remove_signal_handler(_SIGWINCH)
250+
signal.signal(_SIGWINCH, self._previous_winch_handler)
244251

245252
if self._thread is not None:
246253
self._thread.join()

0 commit comments

Comments
 (0)