From f105c7921951d19e0a9a3a9ae1726bea25e98e4d Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sat, 4 Dec 2021 17:50:47 +0000 Subject: [PATCH] Harmonise `UserDict.__init__` with `dict.__init__` (#6490) Co-authored-by: Akuli --- stdlib/builtins.pyi | 1 + stdlib/collections/__init__.pyi | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index b8ee4ff38..fb43eefcc 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -816,6 +816,7 @@ class list(MutableSequence[_T], Generic[_T]): def __class_getitem__(cls, __item: Any) -> GenericAlias: ... class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]): + # __init__ should be kept roughly in line with `collections.UserDict.__init__`, which has similar semantics @overload def __init__(self: dict[_KT, _VT]) -> None: ... @overload diff --git a/stdlib/collections/__init__.pyi b/stdlib/collections/__init__.pyi index 8a5c40cd7..5d52641e5 100644 --- a/stdlib/collections/__init__.pyi +++ b/stdlib/collections/__init__.pyi @@ -1,6 +1,6 @@ import sys from _collections_abc import dict_items, dict_keys, dict_values -from _typeshed import Self, SupportsLessThan, SupportsLessThanT +from _typeshed import Self, SupportsKeysAndGetItem, SupportsLessThan, SupportsLessThanT from typing import Any, Dict, Generic, NoReturn, Tuple, Type, TypeVar, overload from typing_extensions import SupportsIndex, final @@ -35,9 +35,19 @@ else: typename: str, field_names: str | Iterable[str], *, verbose: bool = ..., rename: bool = ..., module: str | None = ... ) -> Type[Tuple[Any, ...]]: ... -class UserDict(MutableMapping[_KT, _VT]): +class UserDict(MutableMapping[_KT, _VT], Generic[_KT, _VT]): data: dict[_KT, _VT] - def __init__(self, __dict: Mapping[_KT, _VT] | None = ..., **kwargs: _VT) -> None: ... + # __init__ should be kept roughly in line with `dict.__init__`, which has the same semantics + @overload + def __init__(self: UserDict[_KT, _VT], __dict: None = ...) -> None: ... + @overload + def __init__(self: UserDict[str, _VT], __dict: None = ..., **kwargs: _VT) -> None: ... + @overload + def __init__(self, __dict: SupportsKeysAndGetItem[_KT, _VT], **kwargs: _VT) -> None: ... + @overload + def __init__(self, __iterable: Iterable[tuple[_KT, _VT]], **kwargs: _VT) -> None: ... + @overload + def __init__(self: UserDict[str, str], __iterable: Iterable[list[str]]) -> None: ... def __len__(self) -> int: ... def __getitem__(self, key: _KT) -> _VT: ... def __setitem__(self, key: _KT, item: _VT) -> None: ...