Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
36 changes: 8 additions & 28 deletions src/pyzstd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
NamedTuple,
NoReturn,
TypeAlias,
cast,
overload,
)
import warnings
Expand Down Expand Up @@ -158,14 +157,17 @@ 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):
raise TypeError(
"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:
Expand Down Expand Up @@ -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
)
Expand Down Expand Up @@ -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")
)
Expand Down Expand Up @@ -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()

Expand All @@ -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:
Expand Down Expand Up @@ -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"),
Expand All @@ -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
)
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
20 changes: 13 additions & 7 deletions src/pyzstd/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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."
Expand Down
7 changes: 4 additions & 3 deletions src/pyzstd/_seekable_zstdfile.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 "
Expand All @@ -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."
)
Expand Down Expand Up @@ -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]
Expand Down