Add parameters to typing.IO.

This commit is contained in:
Andrey Rakhmatullin 2024-05-31 21:41:27 +05:00
parent 4164e63725
commit 70c56faf48
5 changed files with 11 additions and 8 deletions

View File

@ -315,7 +315,9 @@ class FilesystemCacheStorage:
self.expiration_secs: int = settings.getint("HTTPCACHE_EXPIRATION_SECS")
self.use_gzip: bool = settings.getbool("HTTPCACHE_GZIP")
# https://github.com/python/mypy/issues/10740
self._open: Callable[Concatenate[Union[str, os.PathLike], str, ...], IO] = (
self._open: Callable[
Concatenate[Union[str, os.PathLike], str, ...], IO[bytes]
] = (
gzip.open if self.use_gzip else open # type: ignore[assignment]
)
@ -368,11 +370,12 @@ class FilesystemCacheStorage:
with self._open(rpath / "pickled_meta", "wb") as f:
pickle.dump(metadata, f, protocol=4)
with self._open(rpath / "response_headers", "wb") as f:
f.write(headers_dict_to_raw(response.headers))
# headers_dict_to_raw() needs a better type hint
f.write(cast(bytes, headers_dict_to_raw(response.headers)))
with self._open(rpath / "response_body", "wb") as f:
f.write(response.body)
with self._open(rpath / "request_headers", "wb") as f:
f.write(headers_dict_to_raw(request.headers))
f.write(cast(bytes, headers_dict_to_raw(request.headers)))
with self._open(rpath / "request_body", "wb") as f:
f.write(request.body)

View File

@ -97,7 +97,7 @@ class MailSender:
subject: str,
body: str,
cc: Union[str, List[str], None] = None,
attachs: Sequence[Tuple[str, str, IO]] = (),
attachs: Sequence[Tuple[str, str, IO[Any]]] = (),
mimetype: str = "text/plain",
charset: Optional[str] = None,
_callback: Optional[Callable[..., None]] = None,
@ -214,7 +214,7 @@ class MailSender:
return d
def _create_sender_factory(
self, to_addrs: List[str], msg: IO, d: Deferred
self, to_addrs: List[str], msg: IO[bytes], d: Deferred
) -> ESMTPSenderFactory:
from twisted.mail.smtp import ESMTPSenderFactory

View File

@ -47,7 +47,7 @@ def _to_string(path: Union[str, PathLike]) -> str:
return str(path) # convert a Path object to string
def _md5sum(file: IO) -> str:
def _md5sum(file: IO[bytes]) -> str:
"""Calculate the md5 checksum of a file-like object without reading its
whole content in memory.

View File

@ -21,7 +21,7 @@ def ftp_makedirs_cwd(ftp: FTP, path: str, first_call: bool = True) -> None:
def ftp_store_file(
*,
path: str,
file: IO,
file: IO[bytes],
host: str,
port: int,
username: str,

View File

@ -111,7 +111,7 @@ def walk_modules(path: str) -> List[ModuleType]:
return mods
def md5sum(file: IO) -> str:
def md5sum(file: IO[bytes]) -> str:
"""Calculate the md5 checksum of a file-like object without reading its
whole content in memory.