Skip to content

Commit 0c68ed1

Browse files
Several typecheck improvements related to mypy 0.941.
This changes the way we test for specific platforms according to what mypy understands and it runs mypy in CI against all platforms (linux/win32/darwin).
1 parent fe50f11 commit 0c68ed1

File tree

17 files changed

+85
-57
lines changed

17 files changed

+85
-57
lines changed

.github/workflows/test.yaml

+3-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ jobs:
3232
# Check wheather the imports were sorted correctly.
3333
# When this fails, please run ./tools/sort-imports.sh
3434
run: |
35-
mypy --strict prompt_toolkit
35+
mypy --strict prompt_toolkit --platform win32
36+
mypy --strict prompt_toolkit --platform linux
37+
mypy --strict prompt_toolkit --platform darwin
3638
isort -c --profile black prompt_toolkit examples tests setup.py
3739
black --check prompt_toolkit examples tests setup.py
3840
- name: Validate README.md

mypy.ini

-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
[mypy]
2-
platform = win32
3-
42
# --strict.
53
check_untyped_defs = True
64
disallow_any_generics = True

prompt_toolkit/application/application.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@
8989
)
9090
from prompt_toolkit.utils import Event, in_main_thread
9191

92-
from ..utils import is_windows
9392
from .current import get_app_session, set_app
9493
from .run_in_terminal import in_terminal, run_in_terminal
9594

@@ -663,7 +662,7 @@ async def run_async(
663662
"""
664663
assert not self._is_running, "Application is already running."
665664

666-
if not in_main_thread() or is_windows():
665+
if not in_main_thread() or sys.platform == "win32":
667666
# Handling signals in other threads is not supported.
668667
# Also on Windows, `add_signal_handler(signal.SIGINT, ...)` raises
669668
# `NotImplementedError`.

prompt_toolkit/eventloop/inputhook.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,12 @@
2626
import os
2727
import select
2828
import selectors
29+
import sys
2930
import threading
3031
from asyncio import AbstractEventLoop
3132
from selectors import BaseSelector, SelectorKey
3233
from typing import TYPE_CHECKING, Any, Callable, List, Mapping, Optional, Tuple
3334

34-
from prompt_toolkit.utils import is_windows
35-
3635
from .utils import get_event_loop
3736

3837
__all__ = [
@@ -141,7 +140,7 @@ def input_is_ready() -> bool:
141140
# However, if we would ever want to add a select call, it
142141
# should use `windll.kernel32.WaitForMultipleObjects`,
143142
# because `select.select` can't wait for a pipe on Windows.
144-
if not is_windows():
143+
if sys.platform != "win32":
145144
select.select([self._r], [], [], None)
146145

147146
os.read(self._r, 1024)

prompt_toolkit/eventloop/win32.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import sys
2+
3+
assert sys.platform == "win32"
4+
15
from ctypes import pointer
26

37
from ..utils import SPHINX_AUTODOC_RUNNING

prompt_toolkit/input/defaults.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import sys
22
from typing import ContextManager, Optional, TextIO
33

4-
from prompt_toolkit.utils import is_windows
5-
64
from .base import DummyInput, Input, PipeInput
75

86
__all__ = [
@@ -23,7 +21,7 @@ def create_input(
2321
`sys.stdin`. (We can open `stdout` or `stderr` for reading, this is how
2422
a `$PAGER` works.)
2523
"""
26-
if is_windows():
24+
if sys.platform == "win32":
2725
from .win32 import Win32Input
2826

2927
# If `stdin` was assigned `None` (which happens with pythonw.exe), use
@@ -61,7 +59,7 @@ def create_pipe_input() -> ContextManager[PipeInput]:
6159
Breaking change: In prompt_toolkit 3.0.28 and earlier, this was returning
6260
the `PipeInput` directly, rather than through a context manager.
6361
"""
64-
if is_windows():
62+
if sys.platform == "win32":
6563
from .win32_pipe import Win32PipeInput
6664

6765
return Win32PipeInput.create()

prompt_toolkit/input/posix_pipe.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import sys
2+
3+
assert sys.platform != "win32"
4+
15
import os
26
from contextlib import contextmanager
37
from typing import ContextManager, Iterator, TextIO, cast

prompt_toolkit/input/vt100.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import sys
2+
3+
assert sys.platform != "win32"
4+
15
import contextlib
26
import io
3-
import sys
47
import termios
58
import tty
69
from asyncio import AbstractEventLoop

prompt_toolkit/input/win32.py

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
from ..utils import SPHINX_AUTODOC_RUNNING
99

10+
assert sys.platform == "win32"
11+
1012
# Do not import win32-specific stuff when generating documentation.
1113
# Otherwise RTD would be unable to generate docs for this module.
1214
if not SPHINX_AUTODOC_RUNNING:

prompt_toolkit/input/win32_pipe.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import sys
2+
3+
assert sys.platform == "win32"
4+
15
from contextlib import contextmanager
26
from ctypes import windll
37
from ctypes.wintypes import HANDLE

prompt_toolkit/key_binding/bindings/mouse.py

+36-35
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
from typing import TYPE_CHECKING, FrozenSet
23

34
from prompt_toolkit.data_structures import Point
@@ -9,7 +10,6 @@
910
MouseEventType,
1011
MouseModifier,
1112
)
12-
from prompt_toolkit.utils import is_windows
1313

1414
from ..key_bindings import KeyBindings
1515

@@ -303,41 +303,42 @@ def _mouse(event: E) -> "NotImplementedOrNone":
303303
"""
304304
Handling of mouse events for Windows.
305305
"""
306-
assert is_windows() # This key binding should only exist for Windows.
307-
308-
# Parse data.
309-
pieces = event.data.split(";")
310-
311-
button = MouseButton(pieces[0])
312-
event_type = MouseEventType(pieces[1])
313-
x = int(pieces[2])
314-
y = int(pieces[3])
315-
316-
# Make coordinates absolute to the visible part of the terminal.
317-
output = event.app.renderer.output
318-
319-
from prompt_toolkit.output.win32 import Win32Output
320-
from prompt_toolkit.output.windows10 import Windows10_Output
321-
322-
if isinstance(output, (Win32Output, Windows10_Output)):
323-
screen_buffer_info = output.get_win32_screen_buffer_info()
324-
rows_above_cursor = (
325-
screen_buffer_info.dwCursorPosition.Y - event.app.renderer._cursor_pos.y
326-
)
327-
y -= rows_above_cursor
328-
329-
# Call the mouse event handler.
330-
# (Can return `NotImplemented`.)
331-
handler = event.app.renderer.mouse_handlers.mouse_handlers[y][x]
332-
333-
return handler(
334-
MouseEvent(
335-
position=Point(x=x, y=y),
336-
event_type=event_type,
337-
button=button,
338-
modifiers=UNKNOWN_MODIFIER,
306+
# This key binding should only exist for Windows.
307+
if sys.platform == "win32":
308+
# Parse data.
309+
pieces = event.data.split(";")
310+
311+
button = MouseButton(pieces[0])
312+
event_type = MouseEventType(pieces[1])
313+
x = int(pieces[2])
314+
y = int(pieces[3])
315+
316+
# Make coordinates absolute to the visible part of the terminal.
317+
output = event.app.renderer.output
318+
319+
from prompt_toolkit.output.win32 import Win32Output
320+
from prompt_toolkit.output.windows10 import Windows10_Output
321+
322+
if isinstance(output, (Win32Output, Windows10_Output)):
323+
screen_buffer_info = output.get_win32_screen_buffer_info()
324+
rows_above_cursor = (
325+
screen_buffer_info.dwCursorPosition.Y
326+
- event.app.renderer._cursor_pos.y
327+
)
328+
y -= rows_above_cursor
329+
330+
# Call the mouse event handler.
331+
# (Can return `NotImplemented`.)
332+
handler = event.app.renderer.mouse_handlers.mouse_handlers[y][x]
333+
334+
return handler(
335+
MouseEvent(
336+
position=Point(x=x, y=y),
337+
event_type=event_type,
338+
button=button,
339+
modifiers=UNKNOWN_MODIFIER,
340+
)
339341
)
340-
)
341342

342343
# No mouse handler found. Return `NotImplemented` so that we don't
343344
# invalidate the UI.

prompt_toolkit/output/conemu.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import sys
2+
3+
assert sys.platform == "win32"
4+
15
from typing import Any, Optional, TextIO
26

37
from prompt_toolkit.data_structures import Size

prompt_toolkit/output/defaults.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
get_bell_environment_variable,
66
get_term_environment_variable,
77
is_conemu_ansi,
8-
is_windows,
98
)
109

1110
from .base import DummyOutput, Output
@@ -68,7 +67,7 @@ def create_output(
6867
while isinstance(stdout, StdoutProxy):
6968
stdout = stdout.original_stdout
7069

71-
if is_windows():
70+
if sys.platform == "win32":
7271
from .conemu import ConEmuOutput
7372
from .win32 import Win32Output
7473
from .windows10 import Windows10_Output, is_win_vt100_enabled

prompt_toolkit/output/win32.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import sys
2+
3+
assert sys.platform == "win32"
4+
15
import os
26
from ctypes import ArgumentError, byref, c_char, c_long, c_uint, c_ulong, pointer
37

prompt_toolkit/output/windows10.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import sys
2+
3+
assert sys.platform == "win32"
4+
15
from ctypes import byref, windll
26
from ctypes.wintypes import DWORD, HANDLE
37
from typing import Any, Optional, TextIO
48

59
from prompt_toolkit.data_structures import Size
6-
from prompt_toolkit.utils import is_windows
710
from prompt_toolkit.win32_types import STD_OUTPUT_HANDLE
811

912
from .base import Output
@@ -84,7 +87,7 @@ def is_win_vt100_enabled() -> bool:
8487
Returns True when we're running Windows and VT100 escape sequences are
8588
supported.
8689
"""
87-
if not is_windows():
90+
if sys.platform != "win32":
8891
return False
8992

9093
hconsole = HANDLE(windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE))

prompt_toolkit/utils.py

+8-5
Original file line numberDiff line numberDiff line change
@@ -188,24 +188,27 @@ def is_windows() -> bool:
188188
"""
189189
True when we are using Windows.
190190
"""
191-
return sys.platform.startswith("win") # E.g. 'win32', not 'darwin' or 'linux2'
191+
return sys.platform == "win32" # Not 'darwin' or 'linux2'
192192

193193

194194
def is_windows_vt100_supported() -> bool:
195195
"""
196196
True when we are using Windows, but VT100 escape sequences are supported.
197197
"""
198-
# Import needs to be inline. Windows libraries are not always available.
199-
from prompt_toolkit.output.windows10 import is_win_vt100_enabled
198+
if sys.platform == "win32":
199+
# Import needs to be inline. Windows libraries are not always available.
200+
from prompt_toolkit.output.windows10 import is_win_vt100_enabled
200201

201-
return is_windows() and is_win_vt100_enabled()
202+
return is_win_vt100_enabled()
203+
204+
return False
202205

203206

204207
def is_conemu_ansi() -> bool:
205208
"""
206209
True when the ConEmu Windows console is used.
207210
"""
208-
return is_windows() and os.environ.get("ConEmuANSI", "OFF") == "ON"
211+
return sys.platform == "win32" and os.environ.get("ConEmuANSI", "OFF") == "ON"
209212

210213

211214
def in_main_thread() -> bool:

setup.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ ignore=
2222
E241,
2323
E251,
2424
E301,
25+
E402,
2526
E501,
2627
E701,
2728
E702,

0 commit comments

Comments
 (0)