diff --git a/CHANGELOG.md b/CHANGELOG.md index e393731..739b4ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file. ## Unreleased - Fix issue with `max_length` not always being respected in `EndlessZstdDecompressor` +- Improve type support ## 0.20.0 (October 11, 2026) diff --git a/src/pyzstd/__init__.py b/src/pyzstd/__init__.py index 73455ad..e5b598f 100644 --- a/src/pyzstd/__init__.py +++ b/src/pyzstd/__init__.py @@ -10,7 +10,6 @@ NamedTuple, NoReturn, TypeAlias, - cast, overload, ) import warnings @@ -158,6 +157,8 @@ def _convert_level_or_option( CParameter.compressionLevel: level_or_option, } if level_or_option is not None: + if not isinstance(level_or_option, Mapping): + raise TypeError("options must be a mapping") invalid_class = CParameter if read_mode else DParameter for key in level_or_option: if isinstance(key, invalid_class): @@ -165,7 +166,8 @@ def _convert_level_or_option( "Key of compression option dict should " f"NOT be {invalid_class.__name__}." ) - return level_or_option + return dict(level_or_option) + return None class ZstdCompressor: @@ -215,9 +217,6 @@ def __init__( parameters. zstd_dict: A ZstdDict object, pre-trained zstd dictionary. """ - zstd_dict = cast( - "ZstdDict | None", zstd_dict - ) # https://github.com/python/typeshed/pull/15113 self._compressor = zstd.ZstdCompressor( options=_convert_level_or_option(level_or_option, "w"), zstd_dict=zstd_dict ) @@ -285,9 +284,6 @@ def __init__(self, zstd_dict: _ZstdDict = None, option: _Option = None) -> None: zstd_dict: A ZstdDict object, pre-trained zstd dictionary. option: A dict object that contains advanced decompression parameters. """ - zstd_dict = cast( - "ZstdDict | None", zstd_dict - ) # https://github.com/python/typeshed/pull/15113 self._decompressor = zstd.ZstdDecompressor( zstd_dict=zstd_dict, options=_convert_level_or_option(option, "r") ) @@ -341,9 +337,7 @@ def __init__(self, zstd_dict: _ZstdDict = None, option: _Option = None) -> None: zstd_dict: A ZstdDict object, pre-trained zstd dictionary. option: A dict object that contains advanced decompression parameters. """ - self._zstd_dict = cast( - "ZstdDict | None", zstd_dict - ) # https://github.com/python/typeshed/pull/15113 + self._zstd_dict = zstd_dict self._options = _convert_level_or_option(option, "r") self._reset() @@ -364,9 +358,9 @@ def decompress(self, data: Buffer, max_length: int = -1) -> bytes: output buffer is unlimited. When it is nonnegative, returns at most max_length bytes of decompressed data. """ - if not isinstance(data, bytes) or not isinstance(max_length, int): + if not isinstance(max_length, int): raise TypeError - self._buffer += data + self._buffer += memoryview(data).tobytes() self._at_frame_edge &= not self._buffer out_chunks: list[bytes] = [] while True: @@ -423,9 +417,6 @@ def compress( parameters. zstd_dict: A ZstdDict object, pre-trained dictionary for compression. """ - zstd_dict = cast( - "ZstdDict | None", zstd_dict - ) # https://github.com/python/typeshed/pull/15113 return zstd.compress( data, options=_convert_level_or_option(level_or_option, "w"), @@ -445,9 +436,6 @@ def decompress( zstd_dict: A ZstdDict object, pre-trained zstd dictionary. option: A dict object, contains advanced decompression parameters. """ - zstd_dict = cast( - "ZstdDict | None", zstd_dict - ) # https://github.com/python/typeshed/pull/15113 return zstd.decompress( data, options=_convert_level_or_option(option, "r"), zstd_dict=zstd_dict ) @@ -461,9 +449,7 @@ def __init__( self, level_or_option: _LevelOrOption = None, zstd_dict: _ZstdDict = None ) -> None: self._options = _convert_level_or_option(level_or_option, "w") - self._zstd_dict = cast( - "ZstdDict | None", zstd_dict - ) # https://github.com/python/typeshed/pull/15113 + self._zstd_dict = zstd_dict def compress(self, data: Buffer) -> bytes: return zstd.compress(data, options=self._options, zstd_dict=self._zstd_dict) @@ -529,9 +515,6 @@ def __init__( DeprecationWarning, stacklevel=2, ) - zstd_dict = cast( - "ZstdDict | None", zstd_dict - ) # https://github.com/python/typeshed/pull/15113 super().__init__( filename, mode, @@ -602,9 +585,6 @@ def open( # noqa: A001 io.TextIOWrapper instance with the specified encoding, error handling behavior, and line ending(s). """ - zstd_dict = cast( - "ZstdDict | None", zstd_dict - ) # https://github.com/python/typeshed/pull/15113 return zstd.open( filename, mode, diff --git a/src/pyzstd/__main__.py b/src/pyzstd/__main__.py index 0fff622..4e32a0b 100644 --- a/src/pyzstd/__main__.py +++ b/src/pyzstd/__main__.py @@ -19,22 +19,22 @@ class Args(Protocol): - dict: str + dict: BinaryIO | None f: bool - compress: str - tar_input_dir: str + compress: str | None + tar_input_dir: str | None level: int threads: int long: int checksum: bool write_dictID: bool # noqa: N815 - decompress: str - tar_output_dir: str + decompress: str | None + tar_output_dir: str | None test: str | None windowLogMax: int # noqa: N815 - train: str + train: str | None maxdict: int - dictID: int # noqa: N815 + dictID: int | None # noqa: N815 output: BinaryIO | None input: BinaryIO | None zd: ZstdDict | None @@ -193,6 +193,8 @@ def decompress(args: Args) -> None: def train(args: Args) -> None: from glob import glob + assert args.train is not None # noqa: S101 + # check output file if args.output is None: raise ValueError("need to specify output file using -o/--output option") @@ -264,6 +266,8 @@ def tarfile_create(args: Args) -> None: else: import tarfile + assert args.tar_input_dir is not None # noqa: S101 + # check input dir args.tar_input_dir = args.tar_input_dir.rstrip(os.sep) if not os.path.isdir(args.tar_input_dir): @@ -322,6 +326,8 @@ def tarfile_extract(args: Args) -> None: else: import tarfile + assert args.tar_output_dir is not None # noqa: S101 + # input file size if args.input is None: msg = "need to specify input file using -d/--decompress option." diff --git a/src/pyzstd/_seekable_zstdfile.py b/src/pyzstd/_seekable_zstdfile.py index 20d6f1e..358842a 100644 --- a/src/pyzstd/_seekable_zstdfile.py +++ b/src/pyzstd/_seekable_zstdfile.py @@ -1,5 +1,6 @@ from array import array from bisect import bisect_right +from collections.abc import Mapping import io from os import PathLike from os.path import isfile @@ -556,7 +557,7 @@ def __init__( # Read or write mode if mode == "r": - if not isinstance(level_or_option, (type(None), dict)): + if not isinstance(level_or_option, (type(None), Mapping)): raise TypeError( "In read mode (decompression), level_or_option argument " "should be a dict object, that represents decompression " @@ -576,7 +577,7 @@ def __init__( mode_code = _MODE_READ elif mode in {"w", "a", "x"}: - if not isinstance(level_or_option, (type(None), int, dict)): + if not isinstance(level_or_option, (type(None), int, Mapping)): raise TypeError( "level_or_option argument should be int or dict object." ) @@ -902,7 +903,7 @@ def fileno(self) -> int: return self._fp.fileno() # type: ignore[union-attr] @property - def name(self) -> str: + def name(self) -> object: """Return the file name for the underlying file.""" self._check_not_closed() return self._fp.name # type: ignore[union-attr]