Replace a few instances of IO with protocols (#4296)

This commit is contained in:
Sebastian Rittau
2020-07-01 03:05:30 +02:00
committed by GitHub
parent 83e955b52f
commit b8f6d5fc54
3 changed files with 98 additions and 17 deletions

View File

@@ -1,7 +1,6 @@
import sys
from _typeshed import AnyPath, StrPath
from _typeshed import AnyPath, StrPath, SupportsWrite
from typing import (
IO,
AbstractSet,
Any,
Callable,
@@ -122,7 +121,7 @@ class RawConfigParser(_parser):
@overload
def items(self, section: str, raw: bool = ..., vars: Optional[_section] = ...) -> List[Tuple[str, str]]: ...
def set(self, section: str, option: str, value: Optional[str] = ...) -> None: ...
def write(self, fp: IO[str], space_around_delimiters: bool = ...) -> None: ...
def write(self, fp: SupportsWrite[str], space_around_delimiters: bool = ...) -> None: ...
def remove_option(self, section: str, option: str) -> bool: ...
def remove_section(self, section: str) -> bool: ...
def optionxform(self, optionstr: str) -> str: ...

View File

@@ -2,7 +2,7 @@ import os
import sys
import types
from _typeshed import StrPath
from typing import IO, Any, List, Optional, Tuple, TypeVar, Union
from typing import IO, Any, List, Optional, Protocol, Tuple, TypeVar, Union
from _imp import (
acquire_lock as acquire_lock,
@@ -40,12 +40,23 @@ class NullImporter:
def __init__(self, path: StrPath) -> None: ...
def find_module(self, fullname: Any) -> None: ...
# PathLike doesn't work for the pathname argument here
def load_source(name: str, pathname: str, file: Optional[IO[Any]] = ...) -> types.ModuleType: ...
def load_compiled(name: str, pathname: str, file: Optional[IO[Any]] = ...) -> types.ModuleType: ...
def load_package(name: str, path: StrPath) -> types.ModuleType: ...
def load_module(name: str, file: IO[Any], filename: str, details: Tuple[str, str, int]) -> types.ModuleType: ...
# Technically, a text file has to support a slightly different set of operations than a binary file,
# but we ignore that here.
class _FileLike(Protocol):
closed: bool
mode: str
def read(self) -> Union[str, bytes]: ...
def close(self) -> Any: ...
def __enter__(self) -> Any: ...
def __exit__(self, *args: Any) -> Any: ...
# PathLike doesn't work for the pathname argument here
def load_source(name: str, pathname: str, file: Optional[_FileLike] = ...) -> types.ModuleType: ...
def load_compiled(name: str, pathname: str, file: Optional[_FileLike] = ...) -> types.ModuleType: ...
def load_package(name: str, path: StrPath) -> types.ModuleType: ...
def load_module(name: str, file: Optional[_FileLike], filename: str, details: Tuple[str, str, int]) -> types.ModuleType: ...
# IO[Any] is a TextIOWrapper if name is a .py file, and a FileIO otherwise.
if sys.version_info >= (3, 6):
def find_module(
name: str, path: Union[None, List[str], List[os.PathLike[str]], List[StrPath]] = ...
@@ -56,4 +67,4 @@ else:
def reload(module: types.ModuleType) -> types.ModuleType: ...
def init_builtin(name: str) -> Optional[types.ModuleType]: ...
def load_dynamic(name: str, path: str, file: Optional[IO[Any]] = ...) -> types.ModuleType: ...
def load_dynamic(name: str, path: str, file: Any = ...) -> types.ModuleType: ... # file argument is ignored

View File

@@ -1,11 +1,13 @@
import sys
from _typeshed import AnyPath
from _typeshed import AnyPath, OpenBinaryMode, OpenBinaryModeReading, OpenBinaryModeUpdating, OpenBinaryModeWriting, OpenTextMode
from builtins import OSError as error
from io import TextIOWrapper as _TextIOWrapper
from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper as _TextIOWrapper
from posix import listdir as listdir, times_result
from typing import (
IO,
Any,
AnyStr,
BinaryIO,
Callable,
ContextManager,
Dict,
@@ -25,6 +27,7 @@ from typing import (
Union,
overload,
)
from typing_extensions import Literal
from . import path as path
@@ -396,16 +399,84 @@ def putenv(__name: Union[bytes, str], __value: Union[bytes, str]) -> None: ...
if sys.platform != "win32":
def unsetenv(__name: Union[bytes, str]) -> None: ...
# Return IO or TextIO
_Opener = Callable[[str, int], int]
@overload
def fdopen(
fd: int,
mode: str = ...,
mode: OpenTextMode = ...,
buffering: int = ...,
encoding: Optional[str] = ...,
errors: str = ...,
newline: str = ...,
errors: Optional[str] = ...,
newline: Optional[str] = ...,
closefd: bool = ...,
) -> Any: ...
opener: Optional[_Opener] = ...,
) -> _TextIOWrapper: ...
@overload
def fdopen(
fd: int,
mode: OpenBinaryMode,
buffering: Literal[0],
encoding: None = ...,
errors: None = ...,
newline: None = ...,
closefd: bool = ...,
opener: Optional[_Opener] = ...,
) -> FileIO: ...
@overload
def fdopen(
fd: int,
mode: OpenBinaryModeUpdating,
buffering: Literal[-1, 1] = ...,
encoding: None = ...,
errors: None = ...,
newline: None = ...,
closefd: bool = ...,
opener: Optional[_Opener] = ...,
) -> BufferedRandom: ...
@overload
def fdopen(
fd: int,
mode: OpenBinaryModeWriting,
buffering: Literal[-1, 1] = ...,
encoding: None = ...,
errors: None = ...,
newline: None = ...,
closefd: bool = ...,
opener: Optional[_Opener] = ...,
) -> BufferedWriter: ...
@overload
def fdopen(
fd: int,
mode: OpenBinaryModeReading,
buffering: Literal[-1, 1] = ...,
encoding: None = ...,
errors: None = ...,
newline: None = ...,
closefd: bool = ...,
opener: Optional[_Opener] = ...,
) -> BufferedReader: ...
@overload
def fdopen(
fd: int,
mode: OpenBinaryMode,
buffering: int,
encoding: None = ...,
errors: None = ...,
newline: None = ...,
closefd: bool = ...,
opener: Optional[_Opener] = ...,
) -> BinaryIO: ...
@overload
def fdopen(
fd: int,
mode: str,
buffering: int = ...,
encoding: Optional[str] = ...,
errors: Optional[str] = ...,
newline: Optional[str] = ...,
closefd: bool = ...,
opener: Optional[_Opener] = ...,
) -> IO[Any]: ...
def close(fd: int) -> None: ...
def closerange(__fd_low: int, __fd_high: int) -> None: ...
def device_encoding(fd: int) -> Optional[str]: ...