Skip to content

Commit

Permalink
Update mmap stubs for newer Python versions (python#4244)
Browse files Browse the repository at this point in the history
* 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 python#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.
  • Loading branch information
bmerry authored and Vishal Kuo committed Jun 26, 2020
1 parent c5dae1f commit 8cb9764
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions stdlib/2and3/mmap.pyi
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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: ...

Expand All @@ -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
Expand All @@ -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: ...
Expand Down

0 comments on commit 8cb9764

Please sign in to comment.