Skip to content

Commit e556f52

Browse files
aborgna-qdoug-q
andauthored
chore(hugr-py): Swap zstd dependency for pyzstd (#2038)
Both libraries offer good support for encoding/decoding zstd, but `zstd` fails to build under PEP517. Some poetry error hints: ``` Note: This error originates from the build backend, and is likely not a problem with poetry but with zstd (1.5.6.6) not supporting PEP 517 builds. You can verify this by running 'pip wheel --no-cache-dir --use-pep517 "zstd (==1.5.6.6)" ``` As the libraries are almost interchangeable, let's try using the other one. --------- Co-authored-by: Douglas Wilson <141026920+doug-q@users.noreply.github.com>
1 parent ae8f4ba commit e556f52

4 files changed

Lines changed: 109 additions & 53 deletions

File tree

hugr-py/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ dependencies = [
3232
"pydantic-extra-types>=2.9.0",
3333
"semver>=3.0.2",
3434
"typing-extensions~=4.12",
35-
"zstd>=1.5.6.6",
35+
"pyzstd~=0.16.2",
3636
]
3737

3838
[project.optional-dependencies]

hugr-py/src/hugr/envelope.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
from enum import Enum
3737
from typing import TYPE_CHECKING, ClassVar
3838

39-
import zstd
39+
import pyzstd
4040

4141
if TYPE_CHECKING:
4242
from hugr.package import Package
@@ -62,7 +62,7 @@ def make_envelope(package: Package, config: EnvelopeConfig) -> bytes:
6262
raise ValueError(msg)
6363

6464
if config.zstd is not None:
65-
payload = zstd.compress(payload, config.zstd)
65+
payload = pyzstd.compress(payload, config.zstd)
6666

6767
envelope += payload
6868
return bytes(envelope)
@@ -85,7 +85,7 @@ def read_envelope(envelope: bytes) -> Package:
8585
payload = envelope[10:]
8686

8787
if header.zstd:
88-
payload = zstd.uncompress(payload)
88+
payload = pyzstd.decompress(payload)
8989

9090
match header.format:
9191
case EnvelopeFormat.JSON:

hugr-py/tests/test_envelope.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from hugr import tys
2+
from hugr.build.function import Module
3+
from hugr.envelope import EnvelopeConfig, EnvelopeFormat
4+
from hugr.package import Package
5+
6+
7+
def test_envelope():
8+
mod = Module()
9+
f_id = mod.define_function("id", [tys.Qubit])
10+
f_id.set_outputs(f_id.input_node[0])
11+
12+
mod2 = Module()
13+
f_id_decl = mod2.declare_function(
14+
"id", tys.PolyFuncType([], tys.FunctionType([tys.Qubit], [tys.Qubit]))
15+
)
16+
f_main = mod2.define_main([tys.Qubit])
17+
q = f_main.input_node[0]
18+
call = f_main.call(f_id_decl, q)
19+
f_main.set_outputs(call)
20+
package = Package([mod.hugr, mod2.hugr])
21+
22+
# Binary compression roundtrip
23+
for format in [EnvelopeFormat.JSON]:
24+
for compression in [None, 0]:
25+
encoded = package.to_bytes(EnvelopeConfig(format=format, zstd=compression))
26+
decoded = Package.from_bytes(encoded)
27+
assert decoded == package
28+
29+
# String roundtrip
30+
encoded = package.to_str(EnvelopeConfig.TEXT)
31+
decoded = Package.from_str(encoded)
32+
assert decoded == package

0 commit comments

Comments
 (0)