Skip to content
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
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],
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nit: type[_TF]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

mypy doesn't like type[_TF] because of python/mypy#10303

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Sorry about that. I actually tested it before this comment, but it seems that it worked in my small test for some reason, but not here. 😕

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I have switched back to Type[_TF] for now.

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