From 089dd43eb7b20d64eca41a7ced294678ef4b1c6b Mon Sep 17 00:00:00 2001 From: MapleCCC Date: Fri, 30 Jul 2021 21:50:27 +0800 Subject: [PATCH] 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`. --- stdlib/shelve.pyi | 46 +++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/stdlib/shelve.pyi b/stdlib/shelve.pyi index 2e36338db..40f08e6c3 100644 --- a/stdlib/shelve.pyi +++ b/stdlib/shelve.pyi @@ -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]: ...