From e0f73d322065023e7a386e4f2446ba2b37be9aea Mon Sep 17 00:00:00 2001 From: dgelessus Date: Sun, 29 Sep 2019 16:28:07 +0200 Subject: [PATCH] Fix more issues reported by mypy --- rsrcfork/__main__.py | 2 +- rsrcfork/api.py | 13 ++++++++++--- rsrcfork/compress/common.py | 4 ++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/rsrcfork/__main__.py b/rsrcfork/__main__.py index 011213f..7dc7892 100644 --- a/rsrcfork/__main__.py +++ b/rsrcfork/__main__.py @@ -65,7 +65,7 @@ def _bytes_unescape(string: str) -> bytes: return bytes(out) -def _bytes_escape(bs: bytes, *, quote: str=None) -> str: +def _bytes_escape(bs: bytes, *, quote: typing.Optional[str]=None) -> str: """Convert a bytestring to a string (using _TEXT_ENCODING), with non-printable characters hex-escaped. (We implement our own escaping mechanism here to not depend on Python's str or bytes repr.) diff --git a/rsrcfork/api.py b/rsrcfork/api.py index 120122a..b3964ae 100644 --- a/rsrcfork/api.py +++ b/rsrcfork/api.py @@ -4,6 +4,7 @@ import enum import io import os import struct +import types import typing import warnings @@ -197,7 +198,7 @@ class Resource(object): else: return self.data_raw -class ResourceFile(typing.Mapping[bytes, typing.Mapping[int, Resource]]): +class ResourceFile(typing.Mapping[bytes, typing.Mapping[int, Resource]], typing.ContextManager["ResourceFile"]): """A resource file reader operating on a byte stream.""" # noinspection PyProtectedMember @@ -274,7 +275,7 @@ class ResourceFile(typing.Mapping[bytes, typing.Mapping[int, Resource]]): _references: typing.MutableMapping[bytes, typing.MutableMapping[int, typing.Tuple[int, ResourceAttrs, int]]] @classmethod - def open(cls, filename: typing.Union[str, os.PathLike], *, fork: str="auto", **kwargs) -> "ResourceFile": + def open(cls, filename: typing.Union[str, os.PathLike], *, fork: str="auto", **kwargs: typing.Any) -> "ResourceFile": """Open the file at the given path as a ResourceFile. The fork parameter controls which fork of the file the resource data will be read from. It accepts the following values: @@ -458,8 +459,14 @@ class ResourceFile(typing.Mapping[bytes, typing.Mapping[int, Resource]]): def __enter__(self) -> "ResourceFile": return self - def __exit__(self, exc_type, exc_val, exc_tb) -> None: + def __exit__( + self, + exc_type: typing.Optional[typing.Type[BaseException]], + exc_val: typing.Optional[BaseException], + exc_tb: typing.Optional[types.TracebackType] + ) -> typing.Optional[bool]: self.close() + return None def __len__(self) -> int: """Get the number of resource types in this ResourceFile.""" diff --git a/rsrcfork/compress/common.py b/rsrcfork/compress/common.py index 23d9c18..6edf5e4 100644 --- a/rsrcfork/compress/common.py +++ b/rsrcfork/compress/common.py @@ -84,7 +84,7 @@ class CompressedApplicationHeaderInfo(CompressedHeaderInfo): self.working_buffer_fractional_size = working_buffer_fractional_size self.expansion_buffer_size = expansion_buffer_size - def __repr__(self): + def __repr__(self) -> str: return f"{type(self).__qualname__}(header_length={self.header_length}, compression_type=0x{self.compression_type:>04x}, decompressed_length={self.decompressed_length}, dcmp_id={self.dcmp_id}, working_buffer_fractional_size={self.working_buffer_fractional_size}, expansion_buffer_size={self.expansion_buffer_size})" @@ -96,7 +96,7 @@ class CompressedSystemHeaderInfo(CompressedHeaderInfo): self.parameters = parameters - def __repr__(self): + def __repr__(self) -> str: return f"{type(self).__qualname__}(header_length={self.header_length}, compression_type=0x{self.compression_type:>04x}, decompressed_length={self.decompressed_length}, dcmp_id={self.dcmp_id}, parameters={self.parameters!r})"