From 84bbb484639f3348353ebf20d7efa2a8f5b7ac7f Mon Sep 17 00:00:00 2001 From: Vincent Michel Date: Tue, 25 Oct 2022 01:42:23 +0200 Subject: [PATCH] Enable strict type checking --- .pre-commit-config.yaml | 3 +++ gambaterm/audio.py | 10 +++++----- gambaterm/keyboard_input.py | 12 ++++++------ gambaterm/run.py | 2 +- gambaterm/ssh.py | 8 ++++---- gambaterm/ssh_app_session.py | 8 ++++---- pyproject.toml | 17 +++++++++++------ setup.py | 15 +++++++-------- tests/test_gambaterm.py | 4 ++-- 9 files changed, 43 insertions(+), 36 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 43ce8b1..e1b40d1 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -25,4 +25,7 @@ repos: "samplerate", "python-xlib; sys_platform == 'linux'", "pynput; sys_platform != 'linux'", + "types-setuptools", + "pytest", ] + args: [] diff --git a/gambaterm/audio.py b/gambaterm/audio.py index e846d95..93da24d 100644 --- a/gambaterm/audio.py +++ b/gambaterm/audio.py @@ -11,7 +11,7 @@ # Late import of samplerate if TYPE_CHECKING: - import samplerate # type: ignore + import samplerate class AudioOut: @@ -21,13 +21,13 @@ class AudioOut: input_rate: float speed: float - resampler: "samplerate.Resampler" + resampler: samplerate.Resampler queue: Queue[npt.NDArray[np.int16]] buffer: npt.NDArray[np.int16] offset: int def __init__( - self, input_rate: float, resampler: "samplerate.Resampler", speed: float = 1.0 + self, input_rate: float, resampler: samplerate.Resampler, speed: float = 1.0 ): self.input_rate = input_rate self.speed = speed @@ -80,8 +80,8 @@ def audio_player( # Perform late imports # Those can fail if a linux machine doesn't have portaudio or libsamplerate # installed - import samplerate # type: ignore - import sounddevice # type: ignore + import samplerate + import sounddevice input_rate = console.FPS * console.TICKS_IN_FRAME resampler = samplerate.Resampler("linear", channels=2) diff --git a/gambaterm/keyboard_input.py b/gambaterm/keyboard_input.py index ecd1981..028d681 100755 --- a/gambaterm/keyboard_input.py +++ b/gambaterm/keyboard_input.py @@ -10,11 +10,11 @@ from .console import Console, InputGetter if TYPE_CHECKING: - import pynput # type: ignore + import pynput def get_xlib_input_mapping(console: Console) -> dict[int, Console.Input]: - from Xlib import XK # type: ignore + from Xlib import XK return { # Directions @@ -41,7 +41,7 @@ def get_xlib_input_mapping(console: Console) -> dict[int, Console.Input]: def get_xlib_event_mapping(console: Console) -> dict[int, Console.Event]: - from Xlib import XK # type: ignore + from Xlib import XK return { XK.XK_0: console.Event.SELECT_STATE_0, @@ -105,8 +105,8 @@ def get_keyboard_event_mapping(console: Console) -> dict[str, Console.Event]: def xlib_key_pressed_context( display: str | None = None, ) -> Iterator[Callable[[], set[int]]]: - from Xlib.ext import xinput # type: ignore - from Xlib.display import Display # type: ignore + from Xlib.ext import xinput + from Xlib.display import Display with closing(Display(display)) as xdisplay: extension_info = xdisplay.query_extension("XInputExtension") @@ -182,7 +182,7 @@ def get_pressed() -> set[int]: def pynput_key_pressed_context( display: str | None = None, ) -> Iterator[Callable[[], set[str]]]: - from pynput import keyboard # type: ignore + from pynput import keyboard def on_press(key: pynput.keyboard.Key) -> None: try: diff --git a/gambaterm/run.py b/gambaterm/run.py index 05d9889..13a7832 100644 --- a/gambaterm/run.py +++ b/gambaterm/run.py @@ -17,7 +17,7 @@ @contextlib.contextmanager -def timing(deltas: Deque) -> Iterator[None]: +def timing(deltas: Deque[float]) -> Iterator[None]: try: start = time.perf_counter() yield diff --git a/gambaterm/ssh.py b/gambaterm/ssh.py index 6e4617b..e81b1c6 100644 --- a/gambaterm/ssh.py +++ b/gambaterm/ssh.py @@ -25,7 +25,7 @@ async def detect_true_color_support( - process: SSHServerProcess, timeout: float = 0.5 + process: SSHServerProcess[str], timeout: float = 0.5 ) -> bool: # Set unlikely RGB value process.stdout.write("\033[48:2:1:2:3m") @@ -47,7 +47,7 @@ async def detect_true_color_support( return "P1$r" in header and "48:2" in header and "1:2:3m" in header -async def safe_ssh_process_handler(process: SSHServerProcess) -> None: +async def safe_ssh_process_handler(process: SSHServerProcess[str]) -> None: try: result = await ssh_process_handler(process) except KeyboardInterrupt: @@ -60,7 +60,7 @@ async def safe_ssh_process_handler(process: SSHServerProcess) -> None: return process.exit(result or 0) -async def ssh_process_handler(process: SSHServerProcess) -> int: +async def ssh_process_handler(process: SSHServerProcess[str]) -> int: executor = process.get_extra_info("executor") app_config = process.get_extra_info("app_config") display = process.channel.get_x11_display() @@ -215,7 +215,7 @@ def connection_made(self, conn: asyncssh.SSHServerConnection) -> None: def begin_auth(self, username: str) -> bool: return True - def session_requested(self) -> SSHServerProcess: + def session_requested(self) -> SSHServerProcess[str]: return asyncssh.SSHServerProcess( safe_ssh_process_handler, sftp_factory=None, sftp_version=3, allow_scp=False # type: ignore[arg-type] ) diff --git a/gambaterm/ssh_app_session.py b/gambaterm/ssh_app_session.py index ce0629b..fadd083 100644 --- a/gambaterm/ssh_app_session.py +++ b/gambaterm/ssh_app_session.py @@ -24,7 +24,7 @@ @asynccontextmanager async def vt100_output_from_process( - process: SSHServerProcess, + process: SSHServerProcess[str], ) -> AsyncIterator[Vt100_Output]: def get_size() -> Size: width, height, _, _ = process.get_terminal_size() @@ -46,7 +46,7 @@ def get_size() -> Size: @asynccontextmanager async def vt100_input_from_process( - process: SSHServerProcess, + process: SSHServerProcess[str], ) -> AsyncIterator[PipeInput]: with create_pipe_input() as vt100_input: assert isinstance(vt100_input, PosixPipeInput) @@ -63,7 +63,7 @@ async def vt100_input_from_process( @contextmanager def bind_resize_process_to_app_session( - process: SSHServerProcess, app_session: AppSession + process: SSHServerProcess[str], app_session: AppSession ) -> Iterator[None]: original_method = process.terminal_size_changed @@ -83,7 +83,7 @@ def terminal_size_changed( @asynccontextmanager async def process_to_app_session( - process: SSHServerProcess, + process: SSHServerProcess[str], ) -> AsyncIterator[AppSession]: async with vt100_input_from_process(process) as vt100_input: async with vt100_output_from_process(process) as vt100_output: diff --git a/pyproject.toml b/pyproject.toml index 254188f..ad8ee1f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -8,13 +8,18 @@ requires = [ build-backend = "setuptools.build_meta" [tool.mypy] -warn_return_any = true -disallow_untyped_calls = true -disallow_untyped_defs = true -disallow_incomplete_defs = true -check_untyped_defs = true -disallow_untyped_decorators = true +strict = true +show_error_codes = true +[[tool.mypy.overrides]] +module = [ + "pygame", + "samplerate", + "sounddevice", + "pynput", + "Xlib.*" +] +ignore_missing_imports = true [tool.cibuildwheel] build = "cp*-win_amd64 cp*-manylinux_x86_64 cp*-macosx_x86_64" diff --git a/setup.py b/setup.py index 08ea60d..2bc6ab9 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,9 @@ +from __future__ import annotations + import os import glob from pathlib import Path -from setuptools import Extension, setup # type: ignore +from setuptools import Extension, setup # Read the contents of the README file LONG_DESCRIPTION = (Path(__file__).parent / "README.md").read_text() @@ -14,16 +16,13 @@ ] # List all directories containing `.h` files -libgambatte_include_dirs = list( - set( - os.path.dirname(path) - for path in glob.glob("libgambatte/**/*.h", recursive=True) - ) +libgambatte_include_dirs: list[os.PathLike[str]] = list( + set(Path(path).parent for path in glob.glob("libgambatte/**/*.h", recursive=True)) ) # Defer call to `numpy.get_include` -class NumpyIncludePath(os.PathLike): +class NumpyIncludePath: def __str__(self) -> str: return self.__fspath__() @@ -38,7 +37,7 @@ def __fspath__(self) -> str: gambatte_extension = Extension( "gambaterm.libgambatte", language="c++", - include_dirs=libgambatte_include_dirs + [NumpyIncludePath()], # type: ignore + include_dirs=libgambatte_include_dirs + [NumpyIncludePath()], extra_compile_args=["-DHAVE_STDINT_H"], sources=libgambatte_sources + ["libgambatte_ext/libgambatte.pyx"], ) diff --git a/tests/test_gambaterm.py b/tests/test_gambaterm.py index 21a31f3..adf9625 100644 --- a/tests/test_gambaterm.py +++ b/tests/test_gambaterm.py @@ -9,7 +9,7 @@ TEST_ROM = Path(__file__).parent / "test_rom.gb" -@pytest.fixture # type: ignore[misc] +@pytest.fixture def ssh_config(tmp_path: Path) -> Iterator[Path]: rsa_key = asyncssh.generate_private_key("ssh-rsa") (tmp_path / "id_rsa").write_bytes(rsa_key.export_private_key()) @@ -21,7 +21,7 @@ def ssh_config(tmp_path: Path) -> Iterator[Path]: del os.environ["GAMBATERM_SSH_KEY_DIR"] -@pytest.mark.parametrize( # type: ignore[misc] +@pytest.mark.parametrize( "interactive", (False, True), ids=("non-interactive", "interactive") ) def test_gambaterm(interactive: bool) -> None: