Fokko commented on code in PR #4498:
URL: https://github.com/apache/iceberg-python/pull/4498#discussion_r4738064190
##########
tests/table/test_puffin.py:
##########
@@ -79,3 +85,64 @@ def test_read_two_blobs_uncompressed() -> None:
assert pf.get_blob_payload(blob2) == (
b"some blob \x00 binary data \xf0\x9f\xa4\xaf that is not very very
very very very very long, is it?"
)
+
+
+def test_zstd_without_backend_raises_on_use(monkeypatch: pytest.MonkeyPatch)
-> None:
+ monkeypatch.setattr(zstd, "_compress", None)
+ monkeypatch.setattr(zstd, "_decompress", None)
+
+ puffin_bytes = _open_file("v1/sample-metric-data-compressed-zstd.bin")
+ pf = PuffinFile(puffin_bytes)
+ with pytest.raises(ImportError, match="Zstandard support not installed"):
+ pf.get_blob_payload(pf.footer.blobs[0])
+
+
+def test_imports_without_zstd_backend() -> None:
+ script = (
+ "import sys\n"
+ "from importlib.abc import MetaPathFinder\n"
+ "class Blocker(MetaPathFinder):\n"
+ " def find_spec(self, name, path=None, target=None):\n"
+ " if name == 'zstandard' or name.startswith('zstandard.') or
name in ('compression', 'compression.zstd'):\n"
+ " raise ImportError(f'No module named {name}')\n"
+ " return None\n"
+ "sys.meta_path.insert(0, Blocker())\n"
+ "import pyiceberg.manifest\n"
+ "from pyiceberg.avro.codecs import ZStandardCodec\n"
+ "from pyiceberg.table.puffin import PuffinFile\n"
+ "try:\n"
+ " ZStandardCodec.compress(b'hi')\n"
+ " raise SystemExit('expected ImportError')\n"
+ "except ImportError:\n"
+ " print('import-ok-raise-on-use')\n"
+ )
+ result = subprocess.run([sys.executable, "-c", script],
capture_output=True, text=True, check=False)
+
+ assert result.returncode == 0, result.stderr
+ assert "import-ok-raise-on-use" in result.stdout
+
+
+def test_zstd_fallback_when_stdlib_missing() -> None:
Review Comment:
The selection happens at import time, so I see why a fresh interpreter is
needed here. However, the script-as-a-string isn't linted or type-checked,
coverage doesn't track the subprocess (so the fallback lines this test exists
for don't count), and each test pays for an interpreter start plus importing
`pyiceberg.manifest`. Patching `sys.modules` and reloading the module does the
same in-process. We already block imports with `patch.dict("sys.modules", {...:
None})` in `tests/catalog/test_rest_auth.py` and `tests/test_transforms.py`,
and reload a module in `tests/expressions/test_pyparsing_warning.py`. A `None`
entry in `sys.modules` makes the import raise `ImportError`, so the custom
`MetaPathFinder` isn't needed:
```python
def test_zstd_fallback_when_stdlib_missing() -> None:
zstandard = pytest.importorskip("zstandard")
try:
with patch.dict("sys.modules", {"compression": None,
"compression.zstd": None}):
importlib.reload(_zstd)
assert _zstd.ZstdError is zstandard.ZstdError
data = b"hello iceberg" * 100
assert _zstd.decompress(_zstd.compress(data)) == data
finally:
importlib.reload(_zstd)
```
The `finally` reload restores the real backend for the following tests.
`ZStandardCodec` and Puffin look up `_zstd.decompress` on the module at call
time, so they pick up the restored state. Since this isn't Puffin-specific,
`tests/avro/test_file.py` would be a better home.
Side note: on 3.11–3.13 the stdlib branch is skipped anyway, and the 3.14 CI
doesn't have `zstandard` installed, so this only really exercises the fallback
on 3.14 with `zstandard` installed. That's fine for now.
##########
pyiceberg/utils/zstd.py:
##########
@@ -0,0 +1,89 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+"""Zstandard compression backed by the stdlib ``compression.zstd`` or the
``zstandard`` package.
+
Review Comment:
Could we rename this to `pyiceberg/utils/_zstd.py`? As
`pyiceberg.utils.zstd`, `compress`, `decompress` and `ZstdError` become public
API that we'd have to keep around and deprecate before changing. It's only an
internal helper for the Avro codec and Puffin. The imports would become `from
pyiceberg.utils import _zstd`, which also matches the alias `puffin.py` already
uses.
##########
tests/avro/test_file.py:
##########
@@ -691,3 +693,41 @@ def test_avro_output_file_tell_includes_buffered_records()
-> None:
out.write_block([manifest_entry(0)])
assert out.tell() > after_header
+
+
+def test_zstd_without_backend_raises_on_use(monkeypatch: pytest.MonkeyPatch)
-> None:
Review Comment:
Nit: this only checks `compress`. Could we assert `decompress` raises as
well?
##########
tests/table/test_puffin.py:
##########
@@ -79,3 +85,64 @@ def test_read_two_blobs_uncompressed() -> None:
assert pf.get_blob_payload(blob2) == (
b"some blob \x00 binary data \xf0\x9f\xa4\xaf that is not very very
very very very very long, is it?"
)
+
+
+def test_zstd_without_backend_raises_on_use(monkeypatch: pytest.MonkeyPatch)
-> None:
+ monkeypatch.setattr(zstd, "_compress", None)
+ monkeypatch.setattr(zstd, "_decompress", None)
+
+ puffin_bytes = _open_file("v1/sample-metric-data-compressed-zstd.bin")
+ pf = PuffinFile(puffin_bytes)
+ with pytest.raises(ImportError, match="Zstandard support not installed"):
+ pf.get_blob_payload(pf.footer.blobs[0])
+
+
+def test_imports_without_zstd_backend() -> None:
Review Comment:
Same here: blocking `zstandard` as well in the `patch.dict` above, then
calling `importlib.reload(_zstd)` and asserting that `compress`/`decompress`
raise `ImportError`, would cover this in-process. If the module failed at
import time, the `reload` would raise. With that in place, the
`monkeypatch.setattr(zstd, "_compress", None)` tests could probably go too,
since this covers the same thing more faithfully.
##########
uv.lock:
##########
@@ -506,8 +506,8 @@ resolution-markers = [
"python_full_version >= '3.11.5' and python_full_version < '3.12' and
sys_platform != 'emscripten' and sys_platform != 'win32'",
]
dependencies = [
- { name = "soupsieve", marker = "python_full_version >= '3.11.5'" },
- { name = "typing-extensions", marker = "python_full_version >= '3.11.5'" },
+ { name = "soupsieve" },
Review Comment:
This drops the `python_full_version >= '3.11.5'` markers from
beautifulsoup4's dependencies, which is unrelated to this PR and probably comes
from a different `uv` version. Could you revert these two lines so that only
the `zstandard` specifier changes?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]