From 49290944c0d098a85dd867a9e331cfb33a802199 Mon Sep 17 00:00:00 2001 From: Jacob Henner Date: Wed, 8 Oct 2025 13:04:31 -0400 Subject: [PATCH] Fix support for Python 3.14 built without zstd (#11611) --- CHANGES/11603.bugfix.rst | 1 + CONTRIBUTORS.txt | 1 + aiohttp/compression_utils.py | 18 +++++++++--------- 3 files changed, 11 insertions(+), 9 deletions(-) create mode 100644 CHANGES/11603.bugfix.rst diff --git a/CHANGES/11603.bugfix.rst b/CHANGES/11603.bugfix.rst new file mode 100644 index 00000000000..1475698f17a --- /dev/null +++ b/CHANGES/11603.bugfix.rst @@ -0,0 +1 @@ +Fixed Python 3.14 support when built without ``zstd`` support -- by :user:`JacobHenner`. diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index f123d1543fe..b64d99fb632 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -177,6 +177,7 @@ Ivan Lakovic Ivan Larin J. Nick Koston Jacob Champion +Jacob Henner Jaesung Lee Jake Davis Jakob Ackermann diff --git a/aiohttp/compression_utils.py b/aiohttp/compression_utils.py index 67aab49f91b..fcaee591fe5 100644 --- a/aiohttp/compression_utils.py +++ b/aiohttp/compression_utils.py @@ -21,18 +21,21 @@ except ImportError: HAS_BROTLI = False -if sys.version_info >= (3, 14): - import compression.zstd # noqa: I900 +try: + from compression.zstd import ( # type: ignore[import-not-found] # noqa: I900 + ZstdDecompressor, + ) HAS_ZSTD = True -else: +except ImportError: try: - import zstandard + from zstandard import ZstdDecompressor HAS_ZSTD = True except ImportError: HAS_ZSTD = False + MAX_SYNC_CHUNK_SIZE = 1024 @@ -297,13 +300,10 @@ def __init__(self) -> None: "The zstd decompression is not available. " "Please install `zstandard` module" ) - if sys.version_info >= (3, 14): - self._obj = compression.zstd.ZstdDecompressor() - else: - self._obj = zstandard.ZstdDecompressor() + self._obj = ZstdDecompressor() def decompress_sync(self, data: bytes) -> bytes: - return self._obj.decompress(data) + return self._obj.decompress(data) # type: ignore[no-any-return] def flush(self) -> bytes: return b""