Skip to content

Commit

Permalink
Add mypy strict typing (#140)
Browse files Browse the repository at this point in the history
* No errors with mypy --strict

* Apply ruff formatting

* Add py.typed file

* Make it more modern

* Add strict mode to mypy

* Use --with instead of --from

---------

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
  • Loading branch information
jhnstrk and Kludex authored Sep 29, 2024
1 parent 24d5f57 commit a169d93
Show file tree
Hide file tree
Showing 12 changed files with 412 additions and 251 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
run: uv sync --python ${{ matrix.python-version }} --frozen

- name: Run linters
run: scripts/lint
run: scripts/check

- name: Run tests
run: scripts/test
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ coverage.xml
*.py,cover
.hypothesis/
.pytest_cache/
.ruff_cache/
cover/

# Translations
Expand Down
23 changes: 18 additions & 5 deletions multipart/decoders.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import base64
import binascii
from io import BufferedWriter
from typing import TYPE_CHECKING

from .exceptions import DecodeError

if TYPE_CHECKING: # pragma: no cover
from typing import Protocol, TypeVar

_T_contra = TypeVar("_T_contra", contravariant=True)

class SupportsWrite(Protocol[_T_contra]):
def write(self, __b: _T_contra) -> object: ...

# No way to specify optional methods. See
# https://github.com/python/typing/issues/601
# close() [Optional]
# finalize() [Optional]


class Base64Decoder:
"""This object provides an interface to decode a stream of Base64 data. It
Expand Down Expand Up @@ -34,7 +47,7 @@ class Base64Decoder:
:param underlying: the underlying object to pass writes to
"""

def __init__(self, underlying: BufferedWriter):
def __init__(self, underlying: "SupportsWrite[bytes]") -> None:
self.cache = bytearray()
self.underlying = underlying

Expand Down Expand Up @@ -67,9 +80,9 @@ def write(self, data: bytes) -> int:
# Get the remaining bytes and save in our cache.
remaining_len = len(data) % 4
if remaining_len > 0:
self.cache = data[-remaining_len:]
self.cache[:] = data[-remaining_len:]
else:
self.cache = b""
self.cache[:] = b""

# Return the length of the data to indicate no error.
return len(data)
Expand Down Expand Up @@ -112,7 +125,7 @@ class QuotedPrintableDecoder:
:param underlying: the underlying object to pass writes to
"""

def __init__(self, underlying: BufferedWriter) -> None:
def __init__(self, underlying: "SupportsWrite[bytes]") -> None:
self.cache = b""
self.underlying = underlying

Expand Down
Loading

0 comments on commit a169d93

Please sign in to comment.