From 187aaaced93ee936a861471644bae1a00d6dbea9 Mon Sep 17 00:00:00 2001 From: Sebastian Rittau Date: Thu, 28 Jun 2018 19:28:24 +0200 Subject: [PATCH] Use protocols for shutils.copyfileobj() (#2291) See https://github.com/python/typing/issues/564 for background. --- stdlib/2and3/shutil.pyi | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/stdlib/2and3/shutil.pyi b/stdlib/2and3/shutil.pyi index 34f2c6c350f9..07f9f4bf4c89 100644 --- a/stdlib/2and3/shutil.pyi +++ b/stdlib/2and3/shutil.pyi @@ -7,7 +7,7 @@ import sys from typing import ( List, Iterable, Callable, Any, Tuple, Sequence, NamedTuple, IO, - AnyStr, Optional, Union, Set, TypeVar, overload, Type + AnyStr, Optional, Union, Set, TypeVar, overload, Type, Protocol ) if sys.version_info >= (3, 6): @@ -40,7 +40,16 @@ else: class SpecialFileError(EnvironmentError): ... class ExecError(EnvironmentError): ... -def copyfileobj(fsrc: IO[AnyStr], fdst: IO[AnyStr], +_S_co = TypeVar("_S_co", covariant=True) +_S_contra = TypeVar("_S_contra", contravariant=True) + +class _Reader(Protocol[_S_co]): + def read(self, length: int) -> _S_co: ... + +class _Writer(Protocol[_S_contra]): + def write(self, data: _S_contra) -> Any: ... + +def copyfileobj(fsrc: _Reader[AnyStr], fdst: _Writer[AnyStr], length: int = ...) -> None: ... if sys.version_info >= (3,):