Update stdblib/shelve.pyi (#5816)

Several things done:

1. Replace all occurrences of `Any` by respective concrete types.
2. Make `Shelf` and its subclassses generic. (Fixes #5815)
3. `shelve.open` should return a general `Shelf` object, not `DbfilenameShelf`. The documentation does not expose such implementation detail.
4. The argument `dict` is annotated with an abstract type `MutableMapping` rather than concrete type `Dict`.
5. Remove unnecessary methods. Some of them are inherited from `MutableMapping`, so no need to repeat them here.
6. Use builtin-in generics instead of importing from `typing`.
This commit is contained in:
MapleCCC
2021-07-30 21:50:27 +08:00
committed by GitHub
parent 1e39a5899a
commit 089dd43eb7

View File

@@ -1,35 +1,39 @@
import collections.abc
from _typeshed import Self
from typing import Any, Dict, Iterator, Optional, Tuple
from collections.abc import Iterator, MutableMapping
from types import TracebackType
from typing import Optional, Type, TypeVar, Union, overload
class Shelf(collections.abc.MutableMapping[Any, Any]):
_T = TypeVar("_T")
_VT = TypeVar("_VT")
class Shelf(MutableMapping[str, _VT]):
def __init__(
self, dict: Dict[bytes, Any], protocol: Optional[int] = ..., writeback: bool = ..., keyencoding: str = ...
self, dict: MutableMapping[bytes, bytes], protocol: Optional[int] = ..., writeback: bool = ..., keyencoding: str = ...
) -> None: ...
def __iter__(self) -> Iterator[str]: ...
def __len__(self) -> int: ...
def __contains__(self, key: Any) -> bool: ... # key should be str, but it would conflict with superclass's type signature
def get(self, key: str, default: Any = ...) -> Any: ...
def __getitem__(self, key: str) -> Any: ...
def __setitem__(self, key: str, value: Any) -> None: ...
@overload
def get(self, key: str) -> Optional[_VT]: ...
@overload
def get(self, key: str, default: _T) -> Union[_VT, _T]: ...
def __getitem__(self, key: str) -> _VT: ...
def __setitem__(self, key: str, value: _VT) -> None: ...
def __delitem__(self, key: str) -> None: ...
def __enter__(self: Self) -> Self: ...
def __exit__(self, type: Any, value: Any, traceback: Any) -> None: ...
def __exit__(
self, type: Optional[Type[BaseException]], value: Optional[BaseException], traceback: Optional[TracebackType]
) -> None: ...
def close(self) -> None: ...
def __del__(self) -> None: ...
def sync(self) -> None: ...
class BsdDbShelf(Shelf):
def __init__(
self, dict: Dict[bytes, Any], protocol: Optional[int] = ..., writeback: bool = ..., keyencoding: str = ...
) -> None: ...
def set_location(self, key: Any) -> Tuple[str, Any]: ...
def next(self) -> Tuple[str, Any]: ...
def previous(self) -> Tuple[str, Any]: ...
def first(self) -> Tuple[str, Any]: ...
def last(self) -> Tuple[str, Any]: ...
class BsdDbShelf(Shelf[_VT]):
def set_location(self, key: str) -> tuple[str, _VT]: ...
def next(self) -> tuple[str, _VT]: ...
def previous(self) -> tuple[str, _VT]: ...
def first(self) -> tuple[str, _VT]: ...
def last(self) -> tuple[str, _VT]: ...
class DbfilenameShelf(Shelf):
class DbfilenameShelf(Shelf[_VT]):
def __init__(self, filename: str, flag: str = ..., protocol: Optional[int] = ..., writeback: bool = ...) -> None: ...
def open(filename: str, flag: str = ..., protocol: Optional[int] = ..., writeback: bool = ...) -> DbfilenameShelf: ...
def open(filename: str, flag: str = ..., protocol: Optional[int] = ..., writeback: bool = ...) -> Shelf[object]: ...