Skip to content

Commit

Permalink
tarfile improvements (#5492)
Browse files Browse the repository at this point in the history
* Remove explicit Iterable super class
* Use Literals for mode parameters
* Import from collections.abc
* Use protocols for TarFile(fileobj=) and Tarfile.gzopen()
* Use object instead of Any as protocol return types
  • Loading branch information
srittau authored May 19, 2021
1 parent 620cf38 commit 1ea3d0f
Showing 1 changed file with 46 additions and 14 deletions.
60 changes: 46 additions & 14 deletions stdlib/tarfile.pyi
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import io
import sys
from _typeshed import StrOrBytesPath, StrPath
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, Callable, Dict, Iterable, Iterator, List, Mapping, Optional, Set, Tuple, Type, Union
from typing import IO, Dict, List, Optional, Protocol, Set, Tuple, Type, Union, overload
from typing_extensions import Literal

class _Fileobj(Protocol):
def read(self, __size: int) -> bytes: ...
def write(self, __b: bytes) -> object: ...
def tell(self) -> int: ...
def seek(self, __pos: int) -> object: ...
def close(self) -> object: ...
# Optional fields:
# name: str | bytes
# mode: Literal["rb", "r+b", "wb", "xb"]

# tar constants
NUL: bytes
Expand Down Expand Up @@ -52,7 +65,7 @@ ENCODING: str
def open(
name: Optional[StrOrBytesPath] = ...,
mode: str = ...,
fileobj: Optional[IO[bytes]] = ...,
fileobj: Optional[IO[bytes]] = ..., # depends on mode
bufsize: int = ...,
*,
format: Optional[int] = ...,
Expand All @@ -70,11 +83,11 @@ def open(
class ExFileObject(io.BufferedReader):
def __init__(self, tarfile: TarFile, tarinfo: TarInfo) -> None: ...

class TarFile(Iterable[TarInfo]):
class TarFile:
OPEN_METH: Mapping[str, str]
name: Optional[StrOrBytesPath]
mode: str
fileobj: Optional[IO[bytes]]
mode: Literal["r", "a", "w", "x"]
fileobj: Optional[_Fileobj]
format: Optional[int]
tarinfo: Type[TarInfo]
dereference: Optional[bool]
Expand All @@ -89,8 +102,8 @@ class TarFile(Iterable[TarInfo]):
def __init__(
self,
name: Optional[StrOrBytesPath] = ...,
mode: str = ...,
fileobj: Optional[IO[bytes]] = ...,
mode: Literal["r", "a", "w", "x"] = ...,
fileobj: Optional[_Fileobj] = ...,
format: Optional[int] = ...,
tarinfo: Optional[Type[TarInfo]] = ...,
dereference: Optional[bool] = ...,
Expand All @@ -112,7 +125,7 @@ class TarFile(Iterable[TarInfo]):
cls,
name: Optional[StrOrBytesPath] = ...,
mode: str = ...,
fileobj: Optional[IO[bytes]] = ...,
fileobj: Optional[IO[bytes]] = ..., # depends on mode
bufsize: int = ...,
*,
format: Optional[int] = ...,
Expand All @@ -129,8 +142,8 @@ class TarFile(Iterable[TarInfo]):
def taropen(
cls,
name: Optional[StrOrBytesPath],
mode: str = ...,
fileobj: Optional[IO[bytes]] = ...,
mode: Literal["r", "a", "w", "x"] = ...,
fileobj: Optional[_Fileobj] = ...,
*,
compresslevel: int = ...,
format: Optional[int] = ...,
Expand All @@ -142,12 +155,31 @@ class TarFile(Iterable[TarInfo]):
debug: Optional[int] = ...,
errorlevel: Optional[int] = ...,
) -> TarFile: ...
@overload
@classmethod
def gzopen(
cls,
name: Optional[StrOrBytesPath],
mode: str = ...,
fileobj: Optional[IO[bytes]] = ...,
mode: Literal["r"] = ...,
fileobj: Optional[_GzipReadableFileobj] = ...,
compresslevel: int = ...,
*,
format: Optional[int] = ...,
tarinfo: Optional[Type[TarInfo]] = ...,
dereference: Optional[bool] = ...,
ignore_zeros: Optional[bool] = ...,
encoding: Optional[str] = ...,
pax_headers: Optional[Mapping[str, str]] = ...,
debug: Optional[int] = ...,
errorlevel: Optional[int] = ...,
) -> TarFile: ...
@overload
@classmethod
def gzopen(
cls,
name: Optional[StrOrBytesPath],
mode: Literal["w", "x"],
fileobj: Optional[_GzipWritableFileobj] = ...,
compresslevel: int = ...,
*,
format: Optional[int] = ...,
Expand All @@ -163,7 +195,7 @@ class TarFile(Iterable[TarInfo]):
def bz2open(
cls,
name: Optional[StrOrBytesPath],
mode: str = ...,
mode: Literal["r", "w", "x"] = ...,
fileobj: Optional[IO[bytes]] = ...,
compresslevel: int = ...,
*,
Expand All @@ -180,7 +212,7 @@ class TarFile(Iterable[TarInfo]):
def xzopen(
cls,
name: Optional[StrOrBytesPath],
mode: str = ...,
mode: Literal["r", "w", "x"] = ...,
fileobj: Optional[IO[bytes]] = ...,
preset: Optional[int] = ...,
*,
Expand Down

0 comments on commit 1ea3d0f

Please sign in to comment.