Skip to content

Commit 3b1677c

Browse files
committed
Use zstandard implementation from stdlib (PEP-784)
1 parent 4ebce0e commit 3b1677c

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ dependencies = [
5555
"userpath~=1.7",
5656
"uv>=0.5.23",
5757
"virtualenv>=20.26.6",
58-
"zstandard<1",
58+
"backports.zstd>=0.4.0 ; python_version<'3.14'",
5959
]
6060
dynamic = ["version"]
6161

src/hatch/python/resolve.py

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sys
66
from abc import ABC, abstractmethod
77
from functools import cached_property
8-
from typing import TYPE_CHECKING
8+
from typing import TYPE_CHECKING, Literal
99

1010
from hatch.config.constants import PythonEnvVars
1111
from hatch.errors import PythonDistributionResolutionError, PythonDistributionUnknownError
@@ -55,43 +55,36 @@ def archive_name(self) -> str:
5555
return self.source.rsplit('/', 1)[-1]
5656

5757
def unpack(self, archive: Path, directory: Path) -> None:
58+
# zip
5859
if self.source.endswith('.zip'):
5960
import zipfile
6061

6162
with zipfile.ZipFile(archive, 'r') as zf:
6263
zf.extractall(directory)
63-
elif self.source.endswith(('.tar.gz', '.tgz')):
64-
import tarfile
64+
return
6565

66-
with tarfile.open(archive, 'r:gz') as tf:
67-
if sys.version_info[:2] >= (3, 12):
68-
tf.extractall(directory, filter='data')
69-
else:
70-
tf.extractall(directory) # noqa: S202
71-
elif self.source.endswith(('.tar.bz2', '.bz2')):
66+
# tar
67+
if sys.version_info >= (3, 14):
7268
import tarfile
69+
else:
70+
# for zstd support (introduced in Python 3.14)
71+
# and filter kwarg (introduced in Python 3.12)
72+
from backports.zstd import tarfile
7373

74-
with tarfile.open(archive, 'r:bz2') as tf:
75-
if sys.version_info[:2] >= (3, 12):
76-
tf.extractall(directory, filter='data')
77-
else:
78-
tf.extractall(directory) # noqa: S202
74+
mode: Literal['r:gz', 'r:bz2', 'r:zst']
75+
if self.source.endswith(('.tar.gz', '.tgz')):
76+
mode = 'r:gz'
77+
elif self.source.endswith(('.tar.bz2', '.bz2')):
78+
mode = 'r:bz2'
7979
elif self.source.endswith(('.tar.zst', '.tar.zstd')):
80-
import tarfile
81-
82-
import zstandard
83-
84-
with open(archive, 'rb') as ifh:
85-
dctx = zstandard.ZstdDecompressor()
86-
with dctx.stream_reader(ifh) as reader, tarfile.open(mode='r|', fileobj=reader) as tf:
87-
if sys.version_info[:2] >= (3, 12):
88-
tf.extractall(directory, filter='data')
89-
else:
90-
tf.extractall(directory) # noqa: S202
80+
mode = 'r:zst'
9181
else:
9282
message = f'Unknown archive type: {archive}'
9383
raise ValueError(message)
9484

85+
with tarfile.open(archive, mode) as tf:
86+
tf.extractall(directory, filter='data')
87+
9588
@property
9689
@abstractmethod
9790
def version(self) -> Version:

0 commit comments

Comments
 (0)