mirror of
https://github.com/davidhalter/typeshed.git
synced 2025-12-06 20:24:30 +08:00
This pull request is a follow-up to https://github.com/python/mypy/issues/7214. In short, within that mypy issue, we found it would be helpful to determine between contextmanagers that can "swallow" exceptions vs ones that can't. This helps prevent some false positive when using flags that analyze control flow such as `--warn-unreachable`. To do this, Jelle proposed assuming that only contextmanagers where the `__exit__` returns `bool` are assumed to swallow exceptions. This unfortunately required the following typeshed changes: 1. The typing.IO, threading.Lock, and concurrent.futures.Executor were all modified so `__exit__` returns `Optional[None]` instead of None -- along with all of their subclasses. I believe these three types are meant to be subclassed, so I felt picking the more general type was correct. 2. There were also a few concrete types (e.g. see socketserver, subprocess, ftplib...) that I modified to return `None` -- I checked the source code, and these all seem to return None (and don't appear to be meant to be subclassable). 3. contextlib.suppress was changed to return bool. I also double-checked the unittest modules and modified a subset of those contextmanagers, leaving ones like `_AssertRaisesContext` alone.
187 lines
6.7 KiB
Python
187 lines
6.7 KiB
Python
# Stubs for tarfile
|
|
|
|
from typing import (
|
|
Callable, IO, Iterable, Iterator, List, Mapping, Optional, Type,
|
|
Union,
|
|
)
|
|
import os
|
|
import sys
|
|
from types import TracebackType
|
|
|
|
if sys.version_info >= (3, 6):
|
|
_Path = Union[bytes, str, os.PathLike]
|
|
elif sys.version_info >= (3,):
|
|
_Path = Union[bytes, str]
|
|
else:
|
|
_Path = Union[str, unicode]
|
|
|
|
ENCODING: str
|
|
|
|
USTAR_FORMAT: int
|
|
GNU_FORMAT: int
|
|
PAX_FORMAT: int
|
|
DEFAULT_FORMAT: int
|
|
|
|
REGTYPE: bytes
|
|
AREGTYPE: bytes
|
|
LNKTYPE: bytes
|
|
SYMTYPE: bytes
|
|
DIRTYPE: bytes
|
|
FIFOTYPE: bytes
|
|
CONTTYPE: bytes
|
|
CHRTYPE: bytes
|
|
BLKTYPE: bytes
|
|
GNUTYPE_SPARSE: bytes
|
|
|
|
if sys.version_info < (3,):
|
|
TAR_PLAIN: int
|
|
TAR_GZIPPED: int
|
|
|
|
def open(name: Optional[_Path] = ..., mode: str = ...,
|
|
fileobj: Optional[IO[bytes]] = ..., bufsize: int = ...,
|
|
*, format: Optional[int] = ..., tarinfo: Optional[TarInfo] = ...,
|
|
dereference: Optional[bool] = ...,
|
|
ignore_zeros: Optional[bool] = ...,
|
|
encoding: Optional[str] = ..., errors: str = ...,
|
|
pax_headers: Optional[Mapping[str, str]] = ...,
|
|
debug: Optional[int] = ...,
|
|
errorlevel: Optional[int] = ...,
|
|
compresslevel: Optional[int] = ...) -> TarFile: ...
|
|
|
|
class TarFile(Iterable[TarInfo]):
|
|
name: Optional[_Path]
|
|
mode: str
|
|
fileobj: Optional[IO[bytes]]
|
|
format: Optional[int]
|
|
tarinfo: Optional[TarInfo]
|
|
dereference: Optional[bool]
|
|
ignore_zeros: Optional[bool]
|
|
encoding: Optional[str]
|
|
errors: str
|
|
pax_headers: Optional[Mapping[str, str]]
|
|
debug: Optional[int]
|
|
errorlevel: Optional[int]
|
|
if sys.version_info < (3,):
|
|
posix: bool
|
|
def __init__(self, name: Optional[_Path] = ..., mode: str = ...,
|
|
fileobj: Optional[IO[bytes]] = ...,
|
|
format: Optional[int] = ..., tarinfo: Optional[TarInfo] = ...,
|
|
dereference: Optional[bool] = ...,
|
|
ignore_zeros: Optional[bool] = ...,
|
|
encoding: Optional[str] = ..., errors: str = ...,
|
|
pax_headers: Optional[Mapping[str, str]] = ...,
|
|
debug: Optional[int] = ...,
|
|
errorlevel: Optional[int] = ...,
|
|
compresslevel: Optional[int] = ...) -> None: ...
|
|
def __enter__(self) -> TarFile: ...
|
|
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, name: Optional[_Path] = ..., mode: str = ...,
|
|
fileobj: Optional[IO[bytes]] = ..., bufsize: int = ...,
|
|
*, format: Optional[int] = ..., tarinfo: Optional[TarInfo] = ...,
|
|
dereference: Optional[bool] = ...,
|
|
ignore_zeros: Optional[bool] = ...,
|
|
encoding: Optional[str] = ..., errors: str = ...,
|
|
pax_headers: Optional[Mapping[str, str]] = ...,
|
|
debug: Optional[int] = ...,
|
|
errorlevel: Optional[int] = ...) -> TarFile: ...
|
|
def getmember(self, name: str) -> TarInfo: ...
|
|
def getmembers(self) -> List[TarInfo]: ...
|
|
def getnames(self) -> List[str]: ...
|
|
if sys.version_info >= (3, 5):
|
|
def list(self, verbose: bool = ...,
|
|
*, members: Optional[List[TarInfo]] = ...) -> None: ...
|
|
else:
|
|
def list(self, verbose: bool = ...) -> None: ...
|
|
def next(self) -> Optional[TarInfo]: ...
|
|
if sys.version_info >= (3, 5):
|
|
def extractall(self, path: _Path = ...,
|
|
members: Optional[List[TarInfo]] = ...,
|
|
*, numeric_owner: bool = ...) -> None: ...
|
|
else:
|
|
def extractall(self, path: _Path = ...,
|
|
members: Optional[List[TarInfo]] = ...) -> None: ...
|
|
if sys.version_info >= (3, 5):
|
|
def extract(self, member: Union[str, TarInfo], path: _Path = ...,
|
|
set_attrs: bool = ...,
|
|
*, numeric_owner: bool = ...) -> None: ...
|
|
else:
|
|
def extract(self, member: Union[str, TarInfo],
|
|
path: _Path = ...) -> None: ...
|
|
def extractfile(self,
|
|
member: Union[str, TarInfo]) -> Optional[IO[bytes]]: ...
|
|
if sys.version_info >= (3, 7):
|
|
def add(self, name: str, arcname: Optional[str] = ...,
|
|
recursive: bool = ..., *,
|
|
filter: Optional[Callable[[TarInfo], Optional[TarInfo]]] = ...) -> None: ...
|
|
elif sys.version_info >= (3,):
|
|
def add(self, name: str, arcname: Optional[str] = ...,
|
|
recursive: bool = ...,
|
|
exclude: Optional[Callable[[str], bool]] = ..., *,
|
|
filter: Optional[Callable[[TarInfo], Optional[TarInfo]]] = ...) -> None: ...
|
|
else:
|
|
def add(self, name: str, arcname: Optional[str] = ...,
|
|
recursive: bool = ...,
|
|
exclude: Optional[Callable[[str], bool]] = ...,
|
|
filter: Optional[Callable[[TarInfo], Optional[TarInfo]]] = ...) -> None: ...
|
|
def addfile(self, tarinfo: TarInfo,
|
|
fileobj: Optional[IO[bytes]] = ...) -> None: ...
|
|
def gettarinfo(self, name: Optional[str] = ...,
|
|
arcname: Optional[str] = ...,
|
|
fileobj: Optional[IO[bytes]] = ...) -> TarInfo: ...
|
|
def close(self) -> None: ...
|
|
|
|
def is_tarfile(name: str) -> bool: ...
|
|
|
|
if sys.version_info < (3, 8):
|
|
def filemode(mode: int) -> str: ... # undocumented
|
|
|
|
if sys.version_info < (3,):
|
|
class TarFileCompat:
|
|
def __init__(self, filename: str, mode: str = ...,
|
|
compression: int = ...) -> None: ...
|
|
|
|
class TarError(Exception): ...
|
|
class ReadError(TarError): ...
|
|
class CompressionError(TarError): ...
|
|
class StreamError(TarError): ...
|
|
class ExtractError(TarError): ...
|
|
class HeaderError(TarError): ...
|
|
|
|
class TarInfo:
|
|
name: str
|
|
size: int
|
|
mtime: int
|
|
mode: int
|
|
type: bytes
|
|
linkname: str
|
|
uid: int
|
|
gid: int
|
|
uname: str
|
|
gname: str
|
|
pax_headers: Mapping[str, str]
|
|
def __init__(self, name: str = ...) -> None: ...
|
|
if sys.version_info >= (3,):
|
|
@classmethod
|
|
def frombuf(cls, buf: bytes, encoding: str, errors: str) -> TarInfo: ...
|
|
else:
|
|
@classmethod
|
|
def frombuf(cls, buf: bytes) -> TarInfo: ...
|
|
@classmethod
|
|
def fromtarfile(cls, tarfile: TarFile) -> TarInfo: ...
|
|
def tobuf(self, format: Optional[int] = ...,
|
|
encoding: Optional[str] = ..., errors: str = ...) -> bytes: ...
|
|
def isfile(self) -> bool: ...
|
|
def isreg(self) -> bool: ...
|
|
def isdir(self) -> bool: ...
|
|
def issym(self) -> bool: ...
|
|
def islnk(self) -> bool: ...
|
|
def ischr(self) -> bool: ...
|
|
def isblk(self) -> bool: ...
|
|
def isfifo(self) -> bool: ...
|
|
def isdev(self) -> bool: ...
|