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

Use a TypeVar for the return types of TarFile classmethods. #5602

Merged
merged 2 commits into from
Jun 12, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 17 additions & 15 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 @@ -129,7 +131,7 @@ class TarFile:
def __iter__(self) -> Iterator[TarInfo]: ...
@classmethod
def open(
cls,
cls: Type[_TF],
Copy link
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
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
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
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