fileinput: Fix TypeVar usage (#7934)

* fileinput: Fix TypeVar usage

Fixes #7922, part of #7928.
This commit is contained in:
Jelle Zijlstra
2022-05-24 09:55:35 -07:00
committed by GitHub
parent c1aba8059f
commit d950ec37ca

View File

@@ -2,7 +2,11 @@ import sys
from _typeshed import Self, StrOrBytesPath
from collections.abc import Callable, Iterable, Iterator
from types import TracebackType
from typing import IO, Any, AnyStr, Generic
from typing import IO, Any, AnyStr, Generic, Protocol, TypeVar, overload
from typing_extensions import Literal, TypeAlias
if sys.version_info >= (3, 9):
from types import GenericAlias
__all__ = [
"input",
@@ -19,40 +23,133 @@ __all__ = [
"hook_encoded",
]
if sys.version_info >= (3, 9):
from types import GenericAlias
if sys.version_info >= (3, 11):
_TextMode: TypeAlias = Literal["r"]
else:
_TextMode: TypeAlias = Literal["r", "rU", "U"]
_AnyStr_co = TypeVar("_AnyStr_co", str, bytes, covariant=True)
class _HasReadlineAndFileno(Protocol[_AnyStr_co]):
def readline(self) -> _AnyStr_co: ...
def fileno(self) -> int: ...
if sys.version_info >= (3, 10):
# encoding and errors are added
@overload
def input(
files: StrOrBytesPath | Iterable[StrOrBytesPath] | None = ...,
inplace: bool = ...,
backup: str = ...,
*,
mode: str = ...,
openhook: Callable[[StrOrBytesPath, str], IO[AnyStr]] = ...,
mode: _TextMode = ...,
openhook: Callable[[StrOrBytesPath, str], _HasReadlineAndFileno[str]] | None = ...,
encoding: str | None = ...,
errors: str | None = ...,
) -> FileInput[AnyStr]: ...
elif sys.version_info >= (3, 8):
) -> FileInput[str]: ...
@overload
def input(
files: StrOrBytesPath | Iterable[StrOrBytesPath] | None = ...,
inplace: bool = ...,
backup: str = ...,
*,
mode: str = ...,
openhook: Callable[[StrOrBytesPath, str], IO[AnyStr]] = ...,
) -> FileInput[AnyStr]: ...
mode: Literal["rb"],
openhook: Callable[[StrOrBytesPath, str], _HasReadlineAndFileno[bytes]] | None = ...,
encoding: None = ...,
errors: None = ...,
) -> FileInput[bytes]: ...
@overload
def input(
files: StrOrBytesPath | Iterable[StrOrBytesPath] | None = ...,
inplace: bool = ...,
backup: str = ...,
*,
mode: str,
openhook: Callable[[StrOrBytesPath, str], _HasReadlineAndFileno[Any]] | None = ...,
encoding: str | None = ...,
errors: str | None = ...,
) -> FileInput[Any]: ...
elif sys.version_info >= (3, 8):
# bufsize is dropped and mode and openhook become keyword-only
@overload
def input(
files: StrOrBytesPath | Iterable[StrOrBytesPath] | None = ...,
inplace: bool = ...,
backup: str = ...,
*,
mode: _TextMode = ...,
openhook: Callable[[StrOrBytesPath, str], _HasReadlineAndFileno[str]] | None = ...,
) -> FileInput[str]: ...
@overload
def input(
files: StrOrBytesPath | Iterable[StrOrBytesPath] | None = ...,
inplace: bool = ...,
backup: str = ...,
*,
mode: Literal["rb"],
openhook: Callable[[StrOrBytesPath, str], _HasReadlineAndFileno[bytes]] | None = ...,
) -> FileInput[bytes]: ...
@overload
def input(
files: StrOrBytesPath | Iterable[StrOrBytesPath] | None = ...,
inplace: bool = ...,
backup: str = ...,
*,
mode: str,
openhook: Callable[[StrOrBytesPath, str], _HasReadlineAndFileno[Any]] | None = ...,
) -> FileInput[Any]: ...
else:
@overload
def input(
files: StrOrBytesPath | Iterable[StrOrBytesPath] | None = ...,
inplace: bool = ...,
backup: str = ...,
bufsize: int = ...,
mode: str = ...,
openhook: Callable[[StrOrBytesPath, str], IO[AnyStr]] = ...,
) -> FileInput[AnyStr]: ...
mode: _TextMode = ...,
openhook: Callable[[StrOrBytesPath, str], _HasReadlineAndFileno[str]] | None = ...,
) -> FileInput[str]: ...
# Because mode isn't keyword-only here yet, we need two overloads each for
# the bytes case and the fallback case.
@overload
def input(
files: StrOrBytesPath | Iterable[StrOrBytesPath] | None = ...,
inplace: bool = ...,
backup: str = ...,
bufsize: int = ...,
*,
mode: Literal["rb"],
openhook: Callable[[StrOrBytesPath, str], _HasReadlineAndFileno[bytes]] | None = ...,
) -> FileInput[bytes]: ...
@overload
def input(
files: StrOrBytesPath | Iterable[StrOrBytesPath] | None,
inplace: bool,
backup: str,
bufsize: int,
mode: Literal["rb"],
openhook: Callable[[StrOrBytesPath, str], _HasReadlineAndFileno[bytes]] | None = ...,
) -> FileInput[bytes]: ...
@overload
def input(
files: StrOrBytesPath | Iterable[StrOrBytesPath] | None = ...,
inplace: bool = ...,
backup: str = ...,
bufsize: int = ...,
*,
mode: str,
openhook: Callable[[StrOrBytesPath, str], _HasReadlineAndFileno[Any]] | None = ...,
) -> FileInput[Any]: ...
@overload
def input(
files: StrOrBytesPath | Iterable[StrOrBytesPath] | None,
inplace: bool,
backup: str,
bufsize: int,
mode: str,
openhook: Callable[[StrOrBytesPath, str], _HasReadlineAndFileno[Any]] | None = ...,
) -> FileInput[Any]: ...
def close() -> None: ...
def nextfile() -> None: ...
@@ -65,36 +162,131 @@ def isstdin() -> bool: ...
class FileInput(Iterator[AnyStr], Generic[AnyStr]):
if sys.version_info >= (3, 10):
# encoding and errors are added
@overload
def __init__(
self,
files: None | StrOrBytesPath | Iterable[StrOrBytesPath] = ...,
self: FileInput[str],
files: StrOrBytesPath | Iterable[StrOrBytesPath] | None = ...,
inplace: bool = ...,
backup: str = ...,
*,
mode: str = ...,
openhook: Callable[[StrOrBytesPath, str], IO[AnyStr]] = ...,
mode: _TextMode = ...,
openhook: Callable[[StrOrBytesPath, str], _HasReadlineAndFileno[str]] | None = ...,
encoding: str | None = ...,
errors: str | None = ...,
) -> None: ...
elif sys.version_info >= (3, 8):
@overload
def __init__(
self,
files: None | StrOrBytesPath | Iterable[StrOrBytesPath] = ...,
self: FileInput[bytes],
files: StrOrBytesPath | Iterable[StrOrBytesPath] | None = ...,
inplace: bool = ...,
backup: str = ...,
*,
mode: str = ...,
openhook: Callable[[StrOrBytesPath, str], IO[AnyStr]] = ...,
mode: Literal["rb"],
openhook: Callable[[StrOrBytesPath, str], _HasReadlineAndFileno[bytes]] | None = ...,
encoding: None = ...,
errors: None = ...,
) -> None: ...
else:
@overload
def __init__(
self,
files: None | StrOrBytesPath | Iterable[StrOrBytesPath] = ...,
self: FileInput[Any],
files: StrOrBytesPath | Iterable[StrOrBytesPath] | None = ...,
inplace: bool = ...,
backup: str = ...,
*,
mode: str,
openhook: Callable[[StrOrBytesPath, str], _HasReadlineAndFileno[Any]] | None = ...,
encoding: str | None = ...,
errors: str | None = ...,
) -> None: ...
elif sys.version_info >= (3, 8):
# bufsize is dropped and mode and openhook become keyword-only
@overload
def __init__(
self: FileInput[str],
files: StrOrBytesPath | Iterable[StrOrBytesPath] | None = ...,
inplace: bool = ...,
backup: str = ...,
*,
mode: _TextMode = ...,
openhook: Callable[[StrOrBytesPath, str], _HasReadlineAndFileno[str]] | None = ...,
) -> None: ...
@overload
def __init__(
self: FileInput[bytes],
files: StrOrBytesPath | Iterable[StrOrBytesPath] | None = ...,
inplace: bool = ...,
backup: str = ...,
*,
mode: Literal["rb"],
openhook: Callable[[StrOrBytesPath, str], _HasReadlineAndFileno[bytes]] | None = ...,
) -> None: ...
@overload
def __init__(
self: FileInput[Any],
files: StrOrBytesPath | Iterable[StrOrBytesPath] | None = ...,
inplace: bool = ...,
backup: str = ...,
*,
mode: str,
openhook: Callable[[StrOrBytesPath, str], _HasReadlineAndFileno[Any]] | None = ...,
) -> None: ...
else:
@overload
def __init__(
self: FileInput[str],
files: StrOrBytesPath | Iterable[StrOrBytesPath] | None = ...,
inplace: bool = ...,
backup: str = ...,
bufsize: int = ...,
mode: str = ...,
openhook: Callable[[StrOrBytesPath, str], IO[AnyStr]] = ...,
mode: _TextMode = ...,
openhook: Callable[[StrOrBytesPath, str], _HasReadlineAndFileno[str]] | None = ...,
) -> None: ...
# Because mode isn't keyword-only here yet, we need two overloads each for
# the bytes case and the fallback case.
@overload
def __init__(
self: FileInput[bytes],
files: StrOrBytesPath | Iterable[StrOrBytesPath] | None = ...,
inplace: bool = ...,
backup: str = ...,
bufsize: int = ...,
*,
mode: Literal["rb"],
openhook: Callable[[StrOrBytesPath, str], _HasReadlineAndFileno[bytes]] | None = ...,
) -> None: ...
@overload
def __init__(
self: FileInput[bytes],
files: StrOrBytesPath | Iterable[StrOrBytesPath] | None,
inplace: bool,
backup: str,
bufsize: int,
mode: Literal["rb"],
openhook: Callable[[StrOrBytesPath, str], _HasReadlineAndFileno[bytes]] | None = ...,
) -> None: ...
@overload
def __init__(
self: FileInput[Any],
files: StrOrBytesPath | Iterable[StrOrBytesPath] | None = ...,
inplace: bool = ...,
backup: str = ...,
bufsize: int = ...,
*,
mode: str,
openhook: Callable[[StrOrBytesPath, str], _HasReadlineAndFileno[Any]] | None = ...,
) -> None: ...
@overload
def __init__(
self: FileInput[Any],
files: StrOrBytesPath | Iterable[StrOrBytesPath] | None,
inplace: bool,
backup: str,
bufsize: int,
mode: str,
openhook: Callable[[StrOrBytesPath, str], _HasReadlineAndFileno[Any]] | None = ...,
) -> None: ...
def __del__(self) -> None: ...