Skip to content

Commit

Permalink
Enable strict type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
vxgmichel committed Oct 25, 2022
1 parent 065de55 commit 84bbb48
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 36 deletions.
3 changes: 3 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ repos:
"samplerate",
"python-xlib; sys_platform == 'linux'",
"pynput; sys_platform != 'linux'",
"types-setuptools",
"pytest",
]
args: []
10 changes: 5 additions & 5 deletions gambaterm/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

# Late import of samplerate
if TYPE_CHECKING:
import samplerate # type: ignore
import samplerate


class AudioOut:
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions gambaterm/keyboard_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion gambaterm/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions gambaterm/ssh.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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:
Expand All @@ -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()
Expand Down Expand Up @@ -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]
)
Expand Down
8 changes: 4 additions & 4 deletions gambaterm/ssh_app_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand All @@ -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

Expand All @@ -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:
Expand Down
17 changes: 11 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
15 changes: 7 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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__()

Expand All @@ -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"],
)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_gambaterm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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:
Expand Down

0 comments on commit 84bbb48

Please sign in to comment.