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 type hints (part 1) #72

Closed
wants to merge 14 commits into from
Closed
9 changes: 9 additions & 0 deletions multipart/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,12 @@
create_form_parser,
parse_form,
)

__all__ = [
"FormParser",
"MultipartParser",
"OctetStreamParser",
"QuerystringParser",
"create_form_parser",
"parse_form",
]
26 changes: 20 additions & 6 deletions multipart/decoders.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import base64
import binascii
from io import IOBase
from typing import overload

from .exceptions import DecodeError

Expand Down Expand Up @@ -33,10 +35,16 @@ class Base64Decoder:
:param underlying: the underlying object to pass writes to
"""

def __init__(self, underlying):
def __init__(self, underlying: IOBase):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is underlying a IOBase?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a stream or file-like object with a read/write interface. From Base64Decoder's docstring:

"""
...
        from multipart.decoders import Base64Decoder
        fd = open("notb64.txt", "wb")
        decoder = Base64Decoder(fd)
...

I would appreciate more feedback on this from you though.

self.cache = bytearray()
self.underlying = underlying

@overload
def write(self, data: str) -> int: ...

@overload
def write(self, data: bytes) -> int: ...

def write(self, data):
"""Takes any input data provided, decodes it as base64, and passes it
on to the underlying object. If the data provided is invalid base64
Expand Down Expand Up @@ -95,8 +103,8 @@ def finalize(self):
'Base64Decoder cache when finalize() is called'
% len(self.cache))

if hasattr(self.underlying, 'finalize'):
self.underlying.finalize()
if hasattr(self.underlying, 'finalize') and callable(getattr(self.underlying, 'finalize')):
self.underlying.finalize() # type:ignore [reportGeneralTypeIssues]

def __repr__(self):
return f"{self.__class__.__name__}(underlying={self.underlying!r})"
Expand All @@ -111,10 +119,16 @@ class QuotedPrintableDecoder:

:param underlying: the underlying object to pass writes to
"""
def __init__(self, underlying):
def __init__(self, underlying: IOBase):
self.cache = b''
self.underlying = underlying

@overload
def write(self, data: str) -> int: ...

@overload
def write(self, data: bytes) -> int: ...

def write(self, data):
"""Takes any input data provided, decodes it as quoted-printable, and
passes it on to the underlying object.
Expand Down Expand Up @@ -164,8 +178,8 @@ def finalize(self):
self.cache = b''

# Finalize our underlying stream.
if hasattr(self.underlying, 'finalize'):
self.underlying.finalize()
if hasattr(self.underlying, 'finalize') and callable(getattr(self.underlying, 'finalize')):
self.underlying.finalize() # type:ignore [reportGeneralTypeIssues]

def __repr__(self):
return f"{self.__class__.__name__}(underlying={self.underlying!r})"
Loading