From ec6116dfa4197782543e32ea107cbcd4dcd91bfe Mon Sep 17 00:00:00 2001 From: Bruce Merry Date: Sun, 21 Jun 2020 22:37:19 +0200 Subject: [PATCH] Update mmap stubs for newer Python versions (#4244) * Update mmap stubs for newer Python versions Based on the Python stdlib documentation: - Since Python 3.5, mmap.{find,rfind,write} all accept any bytes-like. I've used the _typeshed.ReadableBuffer alias defined in #4232. - Since Python 3.6, mmap.write returns the number of bytes written. - Since Python 3.3, mmap.read allows None as the parameter; while in Python 2 the argument cannot be omitted. * Further clean up mmap.pyi Use the fact that Python 3.0-3.4 are no longer supported to clean up the version-dependent logic. Functions that always have different signatures in Python 2/3 are moved from the base _mmap[bytes] to the mmap subclass. --- stdlib/2and3/mmap.pyi | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/stdlib/2and3/mmap.pyi b/stdlib/2and3/mmap.pyi index f977d82a4..fd6090712 100644 --- a/stdlib/2and3/mmap.pyi +++ b/stdlib/2and3/mmap.pyi @@ -1,6 +1,7 @@ import sys from typing import (Optional, Sequence, Union, Generic, overload, Iterable, Iterator, Sized, ContextManager, AnyStr) +from _typeshed import ReadableBuffer ACCESS_DEFAULT: int ACCESS_READ: int @@ -33,21 +34,17 @@ class _mmap(Generic[AnyStr]): prot: int = ..., access: int = ..., offset: int = ...) -> None: ... def close(self) -> None: ... - def find(self, sub: AnyStr, - start: int = ..., end: int = ...) -> int: ... if sys.version_info >= (3, 8): def flush(self, offset: int = ..., size: int = ...) -> None: ... else: def flush(self, offset: int = ..., size: int = ...) -> int: ... def move(self, dest: int, src: int, count: int) -> None: ... - def read(self, n: int = ...) -> AnyStr: ... def read_byte(self) -> AnyStr: ... def readline(self) -> AnyStr: ... def resize(self, newsize: int) -> None: ... def seek(self, pos: int, whence: int = ...) -> None: ... def size(self) -> int: ... def tell(self) -> int: ... - def write(self, bytes: AnyStr) -> None: ... def write_byte(self, byte: AnyStr) -> None: ... def __len__(self) -> int: ... @@ -56,7 +53,13 @@ if sys.version_info >= (3,): closed: bool if sys.version_info >= (3, 8) and sys.platform != "win32": def madvise(self, option: int, start: int = ..., length: int = ...) -> None: ... - def rfind(self, sub: bytes, start: int = ..., stop: int = ...) -> int: ... + def find(self, sub: ReadableBuffer, start: int = ..., stop: int = ...) -> int: ... + def rfind(self, sub: ReadableBuffer, start: int = ..., stop: int = ...) -> int: ... + def read(self, n: Optional[int] = ...) -> bytes: ... + if sys.version_info >= (3, 6): + def write(self, bytes: ReadableBuffer) -> int: ... + else: + def write(self, bytes: ReadableBuffer) -> None: ... @overload def __getitem__(self, index: int) -> int: ... @overload @@ -71,7 +74,10 @@ if sys.version_info >= (3,): def __iter__(self) -> Iterator[bytes]: ... else: class mmap(_mmap[bytes], Sequence[bytes]): + def find(self, string: bytes, start: int = ..., end: int = ...) -> int: ... def rfind(self, string: bytes, start: int = ..., stop: int = ...) -> int: ... + def read(self, num: int) -> bytes: ... + def write(self, string: bytes) -> None: ... def __getitem__(self, index: Union[int, slice]) -> bytes: ... def __getslice__(self, i: Optional[int], j: Optional[int]) -> bytes: ... def __delitem__(self, index: Union[int, slice]) -> None: ...