Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mypy strict typing #140

Merged
merged 6 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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