Skip to content

Commit 187aaac

Browse files
srittauJelleZijlstra
authored andcommitted
Use protocols for shutils.copyfileobj() (#2291)
See python/typing#564 for background.
1 parent 7ebd609 commit 187aaac

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

stdlib/2and3/shutil.pyi

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import sys
77

88
from typing import (
99
List, Iterable, Callable, Any, Tuple, Sequence, NamedTuple, IO,
10-
AnyStr, Optional, Union, Set, TypeVar, overload, Type
10+
AnyStr, Optional, Union, Set, TypeVar, overload, Type, Protocol
1111
)
1212

1313
if sys.version_info >= (3, 6):
@@ -40,7 +40,16 @@ else:
4040
class SpecialFileError(EnvironmentError): ...
4141
class ExecError(EnvironmentError): ...
4242

43-
def copyfileobj(fsrc: IO[AnyStr], fdst: IO[AnyStr],
43+
_S_co = TypeVar("_S_co", covariant=True)
44+
_S_contra = TypeVar("_S_contra", contravariant=True)
45+
46+
class _Reader(Protocol[_S_co]):
47+
def read(self, length: int) -> _S_co: ...
48+
49+
class _Writer(Protocol[_S_contra]):
50+
def write(self, data: _S_contra) -> Any: ...
51+
52+
def copyfileobj(fsrc: _Reader[AnyStr], fdst: _Writer[AnyStr],
4453
length: int = ...) -> None: ...
4554

4655
if sys.version_info >= (3,):

0 commit comments

Comments
 (0)