Skip to content

Commit

Permalink
Use a TypeVar for the return types of TarFile classmethods. (#5602)
Browse files Browse the repository at this point in the history
* Use a TypeVar for the return types of TarFile classmethods.

* Add TypeVar annotation to TarFile.__enter__
  • Loading branch information
domdfcoding committed Jun 12, 2021
1 parent f28100a commit 48a3469
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions stdlib/tarfile.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ 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, Dict, List, Optional, Protocol, Set, Tuple, Type, Union, overload
from typing import IO, Dict, List, Optional, Protocol, Set, Tuple, Type, TypeVar, Union, overload
from typing_extensions import Literal

_TF = TypeVar("_TF", bound=TarFile)

class _Fileobj(Protocol):
def read(self, __size: int) -> bytes: ...
def write(self, __b: bytes) -> object: ...
Expand Down Expand Up @@ -122,14 +124,14 @@ class TarFile:
errorlevel: Optional[int] = ...,
copybufsize: Optional[int] = ..., # undocumented
) -> None: ...
def __enter__(self) -> TarFile: ...
def __enter__(self: _TF) -> _TF: ...
def __exit__(
self, exc_type: Optional[Type[BaseException]], exc_val: Optional[BaseException], exc_tb: Optional[TracebackType]
) -> None: ...
def __iter__(self) -> Iterator[TarInfo]: ...
@classmethod
def open(
cls,
cls: Type[_TF],
name: Optional[StrOrBytesPath] = ...,
mode: str = ...,
fileobj: Optional[IO[bytes]] = ..., # depends on mode
Expand All @@ -144,10 +146,10 @@ class TarFile:
pax_headers: Optional[Mapping[str, str]] = ...,
debug: Optional[int] = ...,
errorlevel: Optional[int] = ...,
) -> TarFile: ...
) -> _TF: ...
@classmethod
def taropen(
cls,
cls: Type[_TF],
name: Optional[StrOrBytesPath],
mode: Literal["r", "a", "w", "x"] = ...,
fileobj: Optional[_Fileobj] = ...,
Expand All @@ -161,11 +163,11 @@ class TarFile:
pax_headers: Optional[Mapping[str, str]] = ...,
debug: Optional[int] = ...,
errorlevel: Optional[int] = ...,
) -> TarFile: ...
) -> _TF: ...
@overload
@classmethod
def gzopen(
cls,
cls: Type[_TF],
name: Optional[StrOrBytesPath],
mode: Literal["r"] = ...,
fileobj: Optional[_GzipReadableFileobj] = ...,
Expand All @@ -179,11 +181,11 @@ class TarFile:
pax_headers: Optional[Mapping[str, str]] = ...,
debug: Optional[int] = ...,
errorlevel: Optional[int] = ...,
) -> TarFile: ...
) -> _TF: ...
@overload
@classmethod
def gzopen(
cls,
cls: Type[_TF],
name: Optional[StrOrBytesPath],
mode: Literal["w", "x"],
fileobj: Optional[_GzipWritableFileobj] = ...,
Expand All @@ -197,11 +199,11 @@ class TarFile:
pax_headers: Optional[Mapping[str, str]] = ...,
debug: Optional[int] = ...,
errorlevel: Optional[int] = ...,
) -> TarFile: ...
) -> _TF: ...
@overload
@classmethod
def bz2open(
cls,
cls: Type[_TF],
name: Optional[StrOrBytesPath],
mode: Literal["w", "x"],
fileobj: Optional[_Bz2WritableFileobj] = ...,
Expand All @@ -215,11 +217,11 @@ class TarFile:
pax_headers: Optional[Mapping[str, str]] = ...,
debug: Optional[int] = ...,
errorlevel: Optional[int] = ...,
) -> TarFile: ...
) -> _TF: ...
@overload
@classmethod
def bz2open(
cls,
cls: Type[_TF],
name: Optional[StrOrBytesPath],
mode: Literal["r"] = ...,
fileobj: Optional[_Bz2ReadableFileobj] = ...,
Expand All @@ -233,10 +235,10 @@ class TarFile:
pax_headers: Optional[Mapping[str, str]] = ...,
debug: Optional[int] = ...,
errorlevel: Optional[int] = ...,
) -> TarFile: ...
) -> _TF: ...
@classmethod
def xzopen(
cls,
cls: Type[_TF],
name: Optional[StrOrBytesPath],
mode: Literal["r", "w", "x"] = ...,
fileobj: Optional[IO[bytes]] = ...,
Expand All @@ -250,7 +252,7 @@ class TarFile:
pax_headers: Optional[Mapping[str, str]] = ...,
debug: Optional[int] = ...,
errorlevel: Optional[int] = ...,
) -> TarFile: ...
) -> _TF: ...
def getmember(self, name: str) -> TarInfo: ...
def getmembers(self) -> List[TarInfo]: ...
def getnames(self) -> List[str]: ...
Expand Down

0 comments on commit 48a3469

Please sign in to comment.