Skip to content
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

tarfile.open(): Handle all modes #12181

Merged
merged 6 commits into from
Oct 3, 2024
Merged
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
117 changes: 111 additions & 6 deletions stdlib/tarfile.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,10 @@ PAX_NAME_FIELDS: set[str]

ENCODING: str

_FileCreationModes: TypeAlias = Literal["a", "w", "x"]

@overload
def open(
name: StrOrBytesPath | None = None,
mode: str = "r",
mode: Literal["r", "r:*", "r:", "r:gz", "r:bz2", "r:xz"] = "r",
fileobj: IO[bytes] | None = None,
bufsize: int = 10240,
*,
Expand All @@ -121,16 +119,124 @@ def open(
pax_headers: Mapping[str, str] | None = ...,
debug: int | None = ...,
errorlevel: int | None = ...,
compresslevel: int | None = ...,
Copy link
Member

Choose a reason for hiding this comment

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

I see this argument being used in the implementation, why did you remove it?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Due to this comment from the docs:

For modes 'w:gz', 'x:gz', 'w|gz', 'w:bz2', 'x:bz2', 'w|bz2', tarfile.open() accepts the keyword argument compresslevel (default 9) to specify the compression level of the file.

) -> TarFile: ...
@overload
def open(
name: StrOrBytesPath | None,
mode: Literal["x", "x:", "a", "a:", "w", "w:"],
fileobj: _Fileobj | None = None,
bufsize: int = 10240,
*,
format: int | None = ...,
tarinfo: type[TarInfo] | None = ...,
dereference: bool | None = ...,
ignore_zeros: bool | None = ...,
encoding: str | None = ...,
errors: str = ...,
pax_headers: Mapping[str, str] | None = ...,
debug: int | None = ...,
errorlevel: int | None = ...,
) -> TarFile: ...
@overload
def open(
name: StrOrBytesPath | None = None,
*,
mode: Literal["x", "x:", "a", "a:", "w", "w:"],
fileobj: _Fileobj | None = None,
bufsize: int = 10240,
format: int | None = ...,
tarinfo: type[TarInfo] | None = ...,
dereference: bool | None = ...,
ignore_zeros: bool | None = ...,
encoding: str | None = ...,
errors: str = ...,
pax_headers: Mapping[str, str] | None = ...,
debug: int | None = ...,
errorlevel: int | None = ...,
) -> TarFile: ...
@overload
def open(
name: StrOrBytesPath | None,
mode: Literal["x:gz", "x:bz2", "w:gz", "w:bz2"],
fileobj: _Fileobj | None = None,
bufsize: int = 10240,
*,
format: int | None = ...,
tarinfo: type[TarInfo] | None = ...,
dereference: bool | None = ...,
ignore_zeros: bool | None = ...,
encoding: str | None = ...,
errors: str = ...,
pax_headers: Mapping[str, str] | None = ...,
debug: int | None = ...,
errorlevel: int | None = ...,
compresslevel: int = 9,
) -> TarFile: ...
@overload
def open(
name: StrOrBytesPath | None = None,
*,
mode: Literal["x:gz", "x:bz2", "w:gz", "w:bz2"],
fileobj: _Fileobj | None = None,
bufsize: int = 10240,
format: int | None = ...,
tarinfo: type[TarInfo] | None = ...,
dereference: bool | None = ...,
ignore_zeros: bool | None = ...,
encoding: str | None = ...,
errors: str = ...,
pax_headers: Mapping[str, str] | None = ...,
debug: int | None = ...,
errorlevel: int | None = ...,
compresslevel: int = 9,
) -> TarFile: ...
@overload
def open(
name: StrOrBytesPath | None,
mode: Literal["x:xz", "w:xz"],
fileobj: _Fileobj | None = None,
bufsize: int = 10240,
*,
format: int | None = ...,
tarinfo: type[TarInfo] | None = ...,
dereference: bool | None = ...,
ignore_zeros: bool | None = ...,
encoding: str | None = ...,
errors: str = ...,
pax_headers: Mapping[str, str] | None = ...,
debug: int | None = ...,
errorlevel: int | None = ...,
preset: Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | None = ...,
) -> TarFile: ...
@overload
def open(
name: StrOrBytesPath | None = None,
mode: _FileCreationModes = ...,
*,
mode: Literal["x:xz", "w:xz"],
fileobj: _Fileobj | None = None,
bufsize: int = 10240,
format: int | None = ...,
tarinfo: type[TarInfo] | None = ...,
dereference: bool | None = ...,
ignore_zeros: bool | None = ...,
encoding: str | None = ...,
errors: str = ...,
pax_headers: Mapping[str, str] | None = ...,
debug: int | None = ...,
errorlevel: int | None = ...,
preset: Literal[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] | None = ...,
) -> TarFile: ...

# TODO: Temporary fallback for modes containing pipe characters. These don't
# work with mypy 1.10, but this should be fixed with mypy 1.11.
# https://github.com/python/typeshed/issues/12182
@overload
def open(
name: StrOrBytesPath | None = None,
*,
mode: str,
fileobj: IO[bytes] | None = None,
bufsize: int = 10240,
format: int | None = ...,
tarinfo: type[TarInfo] | None = ...,
dereference: bool | None = ...,
Expand All @@ -140,7 +246,6 @@ def open(
pax_headers: Mapping[str, str] | None = ...,
debug: int | None = ...,
errorlevel: int | None = ...,
compresslevel: int | None = ...,
preset: int | None = ...,
) -> TarFile: ...

Expand Down