Typing Stubs for zstd
The `types-zstd` library provides static type annotations for the `python-zstandard` compression library, enabling type checkers like MyPy to validate the correct usage of `zstd` in Python projects. It is part of the `typeshed` project, which regularly updates stubs to reflect changes in popular Python libraries and new Python versions. The current version is 1.5.7.3.20260408.
Common errors
-
ModuleNotFoundError: No module named 'zstd'
cause The `python-zstandard` runtime library, which provides the `zstd` module, is not installed. `types-zstd` only provides type hints.fixInstall the runtime library: `pip install python-zstandard` -
error: Library "zstd" has no attribute "compress" [attr-defined]
cause This MyPy error indicates that the type checker cannot find the `compress` function on the `zstd` module. This could mean either `python-zstandard` is too old/new for the stubs, or MyPy isn't properly picking up the `types-zstd` stubs.fixVerify that `python-zstandard` is installed and its version is compatible with `types-zstd`. Ensure MyPy is running correctly and has access to the installed stubs (e.g., by running in the same virtual environment). -
error: Argument "level" to "compress" has incompatible type "str"; expected "int" [arg-type]
cause This is a typical type checker error, indicating that you're passing an argument of the wrong type (e.g., a string) to a function that expects a different type (e.g., an integer) according to `types-zstd`.fixCorrect the type of the argument being passed to match the expected type. For example, pass an `int` for the compression `level` instead of a `str`.
Warnings
- gotcha `types-zstd` only provides type hints; it does not install the actual `zstd` compression library. For runtime functionality, you must install `python-zstandard` separately.
- gotcha To benefit from `types-zstd`, you need a static type checker (e.g., MyPy) configured to analyze your Python code. Without a type checker, the stubs have no effect.
- breaking If the underlying `python-zstandard` library introduces significant API changes, `types-zstd` will eventually be updated to reflect these changes. This might cause type errors in your existing code if it relies on an older `zstd` API but you've updated `types-zstd`.
Install
-
pip install types-zstd
Imports
- zstd
import types_zstd
import zstd
- compress
from zstd import compress
- ZstdCompressor
from zstd import ZstdCompressor
Quickstart
import zstd
from typing import Optional
# Ensure python-zstandard is installed: pip install python-zstandard
def compress_data(data: bytes, level: Optional[int] = None) -> bytes:
"""Compresses bytes using zstd."""
if level is not None:
return zstd.compress(data, level)
return zstd.compress(data)
def decompress_data(compressed_data: bytes) -> bytes:
"""Decompresses bytes using zstd."""
return zstd.decompress(compressed_data)
if __name__ == '__main__':
original_data = b"This is some data to be compressed by zstd! " * 100
# Use type-checked functions
compressed = compress_data(original_data, level=3)
decompressed = decompress_data(compressed)
print(f"Original size: {len(original_data)} bytes")
print(f"Compressed size: {len(compressed)} bytes")
print(f"Decompressed matches original: {original_data == decompressed}")
# Example of using the streaming API (also type-checked)
compressor = zstd.ZstdCompressor(level=1)
decompressor = zstd.ZstdDecompressor()
with compressor.stream_writer() as writer:
writer.write(b"Hello ")
writer.write(b"World!")
compressed_stream = writer.read()
with decompressor.stream_reader(compressed_stream) as reader:
decompressed_stream = reader.read()
print(f"Stream compression works: {decompressed_stream == b'Hello World!'}")