From 9e0ee447c67487de16695e7d33ed30b219b0263f Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sat, 4 Dec 2021 12:56:30 +0000 Subject: [PATCH] Harmonise UserDict.fromkeys with dict.fromkeys (#6488) --- stdlib/builtins.pyi | 2 +- stdlib/collections/__init__.pyi | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index 298192c7a..b8ee4ff38 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -842,7 +842,7 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]): def keys(self) -> dict_keys[_KT, _VT]: ... def values(self) -> dict_values[_KT, _VT]: ... def items(self) -> dict_items[_KT, _VT]: ... - # Signature of `dict.fromkeys` should be kept identical to `fromkeys` methods in `collections.OrderedDict`/`collections.ChainMap` + # Signature of `dict.fromkeys` should be kept identical to `fromkeys` methods of `OrderedDict`/`ChainMap`/`UserDict` in `collections` # TODO: the true signature of `dict.fromkeys` is not expressable in the current type system. # See #3800 & https://github.com/python/typing/issues/548#issuecomment-683336963. @classmethod diff --git a/stdlib/collections/__init__.pyi b/stdlib/collections/__init__.pyi index 17094bfb3..8a5c40cd7 100644 --- a/stdlib/collections/__init__.pyi +++ b/stdlib/collections/__init__.pyi @@ -45,8 +45,15 @@ class UserDict(MutableMapping[_KT, _VT]): def __iter__(self) -> Iterator[_KT]: ... def __contains__(self, key: object) -> bool: ... def copy(self: Self) -> Self: ... + # `UserDict.fromkeys` has the same semantics as `dict.fromkeys`, so should be kept in line with `dict.fromkeys`. + # TODO: Much like `dict.fromkeys`, the true signature of `UserDict.fromkeys` is inexpressable in the current type system. + # See #3800 & https://github.com/python/typing/issues/548#issuecomment-683336963. @classmethod - def fromkeys(cls: Type[Self], iterable: Iterable[_KT], value: _VT | None = ...) -> Self: ... + @overload + def fromkeys(cls, iterable: Iterable[_T], value: None = ...) -> UserDict[_T, Any | None]: ... + @classmethod + @overload + def fromkeys(cls, iterable: Iterable[_T], value: _S) -> UserDict[_T, _S]: ... class UserList(MutableSequence[_T]): data: list[_T]