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]: ...