Skip to content

Commit

Permalink
python bindings: enable mypy --strict
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksa Sarai <cyphar@cyphar.com>
  • Loading branch information
cyphar committed Oct 8, 2024
1 parent bf03fcb commit a4cdb57
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 12 deletions.
2 changes: 2 additions & 0 deletions contrib/bindings/python/mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[mypy]
strict = true
24 changes: 16 additions & 8 deletions contrib/bindings/python/pathrs/_pathrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
import fcntl

import typing
from typing import Any, IO, Optional, TextIO, Type, TypeVar, Union
from types import TracebackType
from typing import Any, Dict, IO, Optional, TextIO, Type, TypeVar, Union

# TODO: Remove this once we only support Python >= 3.11.
from typing_extensions import Self, TypeAlias
Expand Down Expand Up @@ -88,7 +89,7 @@ class Error(Exception):
errno: Optional[int]
strerror: Optional[str]

def __init__(self, message: str, *_, errno: Optional[int] = None):
def __init__(self, message: str, *, errno: Optional[int] = None):
# Construct Exception.
super().__init__(message)

Expand All @@ -110,7 +111,7 @@ def _fetch(cls, err_id: int) -> Optional[Self]:
return None

err = libpathrs_so.pathrs_errorinfo(err_id)
if err == ffi.NULL:
if err == ffi.NULL: # type: ignore # TODO: Make this check nicer...
return None

description = _pystr(err.description)
Expand Down Expand Up @@ -249,7 +250,7 @@ def fdopen(self, mode: str = "r") -> IO[Any]:
raise

@classmethod
def from_raw_fd(cls, fd: int) -> Self:
def from_raw_fd(cls: Type[Fd], fd: int) -> Fd:
"Shorthand for WrappedFd(fd)."
return cls(fd)

Expand Down Expand Up @@ -300,7 +301,7 @@ def __copy__(self) -> Self:
# A "shallow copy" of a file is the same as a deep copy.
return copy.deepcopy(self)

def __deepcopy__(self, memo) -> Self:
def __deepcopy__(self, memo: Dict[int, Any]) -> Self:
"Identical to WrappedFd.clone()"
return self.clone()

Expand All @@ -311,7 +312,12 @@ def __del__(self) -> None:
def __enter__(self) -> Self:
return self

def __exit__(self, exc_type, exc_value, exc_traceback) -> None:
def __exit__(
self,
exc_type: Optional[Type[BaseException]],
exc_value: Optional[BaseException],
exc_traceback: Optional[TracebackType],
) -> None:
self.close()


Expand Down Expand Up @@ -442,7 +448,8 @@ def proc_readlink(base: ProcfsBase, path: str) -> str:
if n < 0:
raise Error._fetch(n) or INTERNAL_ERROR
elif n <= linkbuf_size:
return ffi.buffer(linkbuf, linkbuf_size)[:n].decode("latin1")
buf = typing.cast(bytes, ffi.buffer(linkbuf, linkbuf_size)[:n])
return buf.decode("latin1")
else:
# The contents were truncated. Unlike readlinkat, pathrs returns
# the size of the link when it checked. So use the returned size
Expand Down Expand Up @@ -551,7 +558,8 @@ def readlink(self, path: str) -> str:
if n < 0:
raise Error._fetch(n) or INTERNAL_ERROR
elif n <= linkbuf_size:
return ffi.buffer(linkbuf, linkbuf_size)[:n].decode("latin1")
buf = typing.cast(bytes, ffi.buffer(linkbuf, linkbuf_size)[:n])
return buf.decode("latin1")
else:
# The contents were truncated. Unlike readlinkat, pathrs returns
# the size of the link when it checked. So use the returned size
Expand Down
4 changes: 2 additions & 2 deletions contrib/bindings/python/pathrs/pathrs_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import os
import sys

from typing import Optional
from typing import Any, Optional
from collections.abc import Iterable

import cffi
Expand Down Expand Up @@ -51,7 +51,7 @@ def load_hdr(ffi: cffi.FFI, hdr_path: str) -> None:
ffi.cdef(hdr)


def create_ffibuilder(**kwargs) -> cffi.FFI:
def create_ffibuilder(**kwargs: Any) -> cffi.FFI:
ffibuilder = cffi.FFI()
ffibuilder.cdef("typedef uint32_t dev_t;")

Expand Down
6 changes: 4 additions & 2 deletions contrib/bindings/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,18 @@

import setuptools

from typing import Any, Dict


# This is only needed for backwards compatibility with older versions.
def parse_pyproject():
def parse_pyproject() -> Dict[str, Any]:
try:
import tomllib

openmode = "rb"
except ImportError:
# TODO: Remove this once we only support Python >= 3.11.
import toml as tomllib
import toml as tomllib # type: ignore

openmode = "r"

Expand Down

0 comments on commit a4cdb57

Please sign in to comment.