Skip to content

Add TarFile.zstopen for 3.14 #14238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion stdlib/@tests/stubtest_allowlists/py314.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ multiprocessing.managers._BaseDictProxy.__ror__
multiprocessing.managers._BaseDictProxy.fromkeys
multiprocessing.process.BaseProcess.interrupt
multiprocessing.synchronize.SemLock.locked
tarfile.TarFile.zstopen
tkinter.Event.__class_getitem__

# =========================
Expand Down
51 changes: 40 additions & 11 deletions stdlib/tarfile.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ from builtins import list as _list # aliases to avoid name clashes with fields
from collections.abc import Callable, Iterable, Iterator, Mapping
from gzip import _ReadableFileobj as _GzipReadableFileobj, _WritableFileobj as _GzipWritableFileobj
from types import TracebackType
from typing import IO, ClassVar, Literal, Protocol, overload
from typing import IO, ClassVar, Literal, Protocol, TypedDict, overload
from typing_extensions import Self, TypeAlias, deprecated

__all__ = [
Expand Down Expand Up @@ -105,6 +105,17 @@ PAX_NAME_FIELDS: set[str]

ENCODING: str

class _TarOpenOptions(TypedDict, total=False):
compresslevel: int
format: int | None
tarinfo: type[TarInfo] | None
dereference: bool | None
ignore_zeros: bool | None
encoding: str | None
pax_headers: Mapping[str, str] | None
debug: int | None
errorlevel: int | None

class ExFileObject(io.BufferedReader):
def __init__(self, tarfile: TarFile, tarinfo: TarInfo) -> None: ...

Expand Down Expand Up @@ -426,16 +437,7 @@ class TarFile:
name: StrOrBytesPath | None,
mode: Literal["r", "a", "w", "x"] = "r",
fileobj: _Fileobj | None = None,
*,
compresslevel: int = ...,
format: int | None = ...,
tarinfo: type[TarInfo] | None = ...,
dereference: bool | None = ...,
ignore_zeros: bool | None = ...,
encoding: str | None = ...,
pax_headers: Mapping[str, str] | None = ...,
debug: int | None = ...,
errorlevel: int | None = ...,
**kwargs: _TarOpenOptions,
) -> Self: ...
@overload
@classmethod
Expand Down Expand Up @@ -587,6 +589,33 @@ class TarFile:
self, name: StrOrBytesPath | None = None, arcname: str | None = None, fileobj: IO[bytes] | None = None
) -> TarInfo: ...
def close(self) -> None: ...
if sys.version_info >= (3, 14):
from compression.zstd import ZstdDict as _ZstdDict

@classmethod
@overload
def zstopen(
cls,
name: StrOrBytesPath | None,
mode: Literal["r", "rb"] = "r",
fileobj: _Fileobj | None = None,
level: None = None,
options: Mapping[int, int] | None = None,
zstd_dict: _ZstdDict | None = None,
**kwargs: _TarOpenOptions,
) -> Self: ...
@classmethod
@overload
def zstopen(
cls,
name: StrOrBytesPath | None,
mode: Literal["w", "x"],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Runtime validation around supported read/write modes:

  if mode not in ("r", "w", "x"):
      raise ValueError("mode must be 'r', 'w' or 'x'")

fileobj: _Fileobj | None = None,
level: int | None = None,
options: Mapping[int, int] | None = None,
zstd_dict: _ZstdDict | None = None,
**kwargs: _TarOpenOptions,
) -> Self: ...

open = TarFile.open

Expand Down
Loading