Skip to content

Commit a169d93

Browse files
jhnstrkKludex
andauthored
Add mypy strict typing (#140)
* 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>
1 parent 24d5f57 commit a169d93

File tree

12 files changed

+412
-251
lines changed

12 files changed

+412
-251
lines changed

.github/workflows/main.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
run: uv sync --python ${{ matrix.python-version }} --frozen
2929

3030
- name: Run linters
31-
run: scripts/lint
31+
run: scripts/check
3232

3333
- name: Run tests
3434
run: scripts/test

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ coverage.xml
8989
*.py,cover
9090
.hypothesis/
9191
.pytest_cache/
92+
.ruff_cache/
9293
cover/
9394

9495
# Translations

multipart/decoders.py

+18-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
import base64
22
import binascii
3-
from io import BufferedWriter
3+
from typing import TYPE_CHECKING
44

55
from .exceptions import DecodeError
66

7+
if TYPE_CHECKING: # pragma: no cover
8+
from typing import Protocol, TypeVar
9+
10+
_T_contra = TypeVar("_T_contra", contravariant=True)
11+
12+
class SupportsWrite(Protocol[_T_contra]):
13+
def write(self, __b: _T_contra) -> object: ...
14+
15+
# No way to specify optional methods. See
16+
# https://github.com/python/typing/issues/601
17+
# close() [Optional]
18+
# finalize() [Optional]
19+
720

821
class Base64Decoder:
922
"""This object provides an interface to decode a stream of Base64 data. It
@@ -34,7 +47,7 @@ class Base64Decoder:
3447
:param underlying: the underlying object to pass writes to
3548
"""
3649

37-
def __init__(self, underlying: BufferedWriter):
50+
def __init__(self, underlying: "SupportsWrite[bytes]") -> None:
3851
self.cache = bytearray()
3952
self.underlying = underlying
4053

@@ -67,9 +80,9 @@ def write(self, data: bytes) -> int:
6780
# Get the remaining bytes and save in our cache.
6881
remaining_len = len(data) % 4
6982
if remaining_len > 0:
70-
self.cache = data[-remaining_len:]
83+
self.cache[:] = data[-remaining_len:]
7184
else:
72-
self.cache = b""
85+
self.cache[:] = b""
7386

7487
# Return the length of the data to indicate no error.
7588
return len(data)
@@ -112,7 +125,7 @@ class QuotedPrintableDecoder:
112125
:param underlying: the underlying object to pass writes to
113126
"""
114127

115-
def __init__(self, underlying: BufferedWriter) -> None:
128+
def __init__(self, underlying: "SupportsWrite[bytes]") -> None:
116129
self.cache = b""
117130
self.underlying = underlying
118131

0 commit comments

Comments
 (0)