Skip to content

Commit

Permalink
Rework tarfile.open/TarFile.open (#13177)
Browse files Browse the repository at this point in the history
* Copy overloads of `open()` to `TarFile.open()`.
* Replace remaining instances of `IO` with `_Fileobj`.
* Replace `open()` with alias to `TarFile.open()` to match implementation.
  • Loading branch information
srittau authored Mar 6, 2025
1 parent 6cecf19 commit 5f1fd23
Showing 1 changed file with 181 additions and 178 deletions.
359 changes: 181 additions & 178 deletions stdlib/tarfile.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -103,182 +103,6 @@ PAX_NAME_FIELDS: set[str]

ENCODING: str

@overload
def open(
name: StrOrBytesPath | None = None,
mode: Literal["r", "r:*", "r:", "r:gz", "r:bz2", "r:xz"] = "r",
fileobj: IO[bytes] | 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", "x:", "a", "a:", "w", "w:", "w:tar"],
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:", "w:tar"],
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: 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 | ReadableBuffer | None = None,
*,
mode: Literal["r|*", "r|", "r|gz", "r|bz2", "r|xz"],
fileobj: IO[bytes] | 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 | WriteableBuffer | None = None,
*,
mode: Literal["w|", "w|xz"],
fileobj: IO[bytes] | 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 | WriteableBuffer | None = None,
*,
mode: Literal["w|gz", "w|bz2"],
fileobj: IO[bytes] | 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: ...

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

Expand Down Expand Up @@ -341,14 +165,152 @@ class TarFile:
self, type: type[BaseException] | None, value: BaseException | None, traceback: TracebackType | None
) -> None: ...
def __iter__(self) -> Iterator[TarInfo]: ...
@overload
@classmethod
def open(
cls,
name: StrOrBytesPath | None = None,
mode: Literal["r", "r:*", "r:", "r:gz", "r:bz2", "r:xz"] = "r",
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 = ...,
) -> Self: ...
@overload
@classmethod
def open(
cls,
name: StrOrBytesPath | None,
mode: Literal["x", "x:", "a", "a:", "w", "w:", "w:tar"],
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 = ...,
) -> Self: ...
@overload
@classmethod
def open(
cls,
name: StrOrBytesPath | None = None,
*,
mode: Literal["x", "x:", "a", "a:", "w", "w:", "w:tar"],
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 = ...,
) -> Self: ...
@overload
@classmethod
def open(
cls,
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,
) -> Self: ...
@overload
@classmethod
def open(
cls,
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,
) -> Self: ...
@overload
@classmethod
def open(
cls,
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 = ...,
) -> Self: ...
@overload
@classmethod
def open(
cls,
name: StrOrBytesPath | None = None,
mode: str = "r",
fileobj: IO[bytes] | None = None, # depends on mode
*,
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 = ...,
) -> Self: ...
@overload
@classmethod
def open(
cls,
name: StrOrBytesPath | ReadableBuffer | None = None,
*,
mode: Literal["r|*", "r|", "r|gz", "r|bz2", "r|xz"],
fileobj: _Fileobj | None = None,
bufsize: int = 10240,
format: int | None = ...,
tarinfo: type[TarInfo] | None = ...,
dereference: bool | None = ...,
Expand All @@ -359,6 +321,45 @@ class TarFile:
debug: int | None = ...,
errorlevel: int | None = ...,
) -> Self: ...
@overload
@classmethod
def open(
cls,
name: StrOrBytesPath | WriteableBuffer | None = None,
*,
mode: Literal["w|", "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 = ...,
) -> Self: ...
@overload
@classmethod
def open(
cls,
name: StrOrBytesPath | WriteableBuffer | None = None,
*,
mode: Literal["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,
) -> Self: ...
@classmethod
def taropen(
cls,
Expand Down Expand Up @@ -517,6 +518,8 @@ class TarFile:
) -> TarInfo: ...
def close(self) -> None: ...

open = TarFile.open

if sys.version_info >= (3, 9):
def is_tarfile(name: StrOrBytesPath | IO[bytes]) -> bool: ...

Expand Down

0 comments on commit 5f1fd23

Please sign in to comment.