fix the stub files for the stdlib mmap module (#5705)

In Python 3:
- The `mmap` type is `Iterable[int]`, not `Iterable[bytes]`.
- The `read_byte` method returns an `int`, and the `write_byte` method only accepts an `int` as its first and only argument.
- The `__setitem__` method accepts any `ReadableBuffer` object, not just `bytes`.

In both Python 2 and 3:
- The `__delitem__` method always raises a `TypeError`, so the proper return type is `NoReturn`.
- The `mmap` type isn't generic, so I've simplified the stubs by removing the unnecessary `_mmap` class.
This commit is contained in:
Charly C
2021-06-29 17:01:32 +02:00
committed by GitHub
parent 14add7520b
commit 389b92cb04
2 changed files with 14 additions and 18 deletions

View File

@@ -1,5 +1,5 @@
import sys
from typing import AnyStr, Generic, Optional, Sequence, Union
from typing import NoReturn, Optional, Sequence, Union
ACCESS_DEFAULT: int
ACCESS_READ: int
@@ -23,7 +23,7 @@ if sys.platform != "win32":
PAGESIZE: int
class _mmap(Generic[AnyStr]):
class mmap(Sequence[bytes]):
if sys.platform == "win32":
def __init__(
self, fileno: int, length: int, tagname: Optional[str] = ..., access: int = ..., offset: int = ...
@@ -35,21 +35,19 @@ class _mmap(Generic[AnyStr]):
def close(self) -> None: ...
def flush(self, offset: int = ..., size: int = ...) -> int: ...
def move(self, dest: int, src: int, count: int) -> None: ...
def read_byte(self) -> AnyStr: ...
def readline(self) -> AnyStr: ...
def read_byte(self) -> bytes: ...
def readline(self) -> bytes: ...
def resize(self, newsize: int) -> None: ...
def seek(self, pos: int, whence: int = ...) -> None: ...
def size(self) -> int: ...
def tell(self) -> int: ...
def write_byte(self, byte: AnyStr) -> None: ...
def write_byte(self, byte: bytes) -> None: ...
def __len__(self) -> int: ...
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: ...
def __delitem__(self, index: Union[int, slice]) -> NoReturn: ...
def __setitem__(self, index: Union[int, slice], object: bytes) -> None: ...

View File

@@ -1,6 +1,6 @@
import sys
from _typeshed import ReadableBuffer
from typing import AnyStr, ContextManager, Generic, Iterable, Iterator, Optional, Sized, Union, overload
from typing import ContextManager, Iterable, Iterator, NoReturn, Optional, Sized, Union, overload
ACCESS_DEFAULT: int
ACCESS_READ: int
@@ -24,7 +24,7 @@ if sys.platform != "win32":
PAGESIZE: int
class _mmap(Generic[AnyStr]):
class mmap(ContextManager[mmap], Iterable[int], Sized):
if sys.platform == "win32":
def __init__(
self, fileno: int, length: int, tagname: Optional[str] = ..., access: int = ..., offset: int = ...
@@ -39,16 +39,14 @@ class _mmap(Generic[AnyStr]):
else:
def flush(self, offset: int = ..., size: int = ...) -> int: ...
def move(self, dest: int, src: int, count: int) -> None: ...
def read_byte(self) -> AnyStr: ...
def readline(self) -> AnyStr: ...
def read_byte(self) -> int: ...
def readline(self) -> bytes: ...
def resize(self, newsize: int) -> None: ...
def seek(self, pos: int, whence: int = ...) -> None: ...
def size(self) -> int: ...
def tell(self) -> int: ...
def write_byte(self, byte: AnyStr) -> None: ...
def write_byte(self, byte: int) -> None: ...
def __len__(self) -> int: ...
class mmap(_mmap[bytes], ContextManager[mmap], Iterable[bytes], Sized):
closed: bool
if sys.version_info >= (3, 8) and sys.platform != "win32":
def madvise(self, option: int, start: int = ..., length: int = ...) -> None: ...
@@ -60,14 +58,14 @@ class mmap(_mmap[bytes], ContextManager[mmap], Iterable[bytes], Sized):
def __getitem__(self, index: int) -> int: ...
@overload
def __getitem__(self, index: slice) -> bytes: ...
def __delitem__(self, index: Union[int, slice]) -> None: ...
def __delitem__(self, index: Union[int, slice]) -> NoReturn: ...
@overload
def __setitem__(self, index: int, object: int) -> None: ...
@overload
def __setitem__(self, index: slice, object: bytes) -> None: ...
def __setitem__(self, index: slice, object: ReadableBuffer) -> None: ...
# Doesn't actually exist, but the object is actually iterable because it has __getitem__ and
# __len__, so we claim that there is also an __iter__ to help type checkers.
def __iter__(self) -> Iterator[bytes]: ...
def __iter__(self) -> Iterator[int]: ...
if sys.version_info >= (3, 8) and sys.platform != "win32":
MADV_NORMAL: int