From a293f1e73fb78b6bba1c013b2613978b260548a8 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Fri, 3 Dec 2021 20:06:51 +0000 Subject: [PATCH] Add `OrderedDict.fromkeys` (#6485) --- stdlib/builtins.pyi | 5 +++++ stdlib/collections/__init__.pyi | 12 ++++++++++++ 2 files changed, 17 insertions(+) diff --git a/stdlib/builtins.pyi b/stdlib/builtins.pyi index bb18586ca..298192c7a 100644 --- a/stdlib/builtins.pyi +++ b/stdlib/builtins.pyi @@ -777,11 +777,13 @@ class list(MutableSequence[_T], Generic[_T]): def append(self, __object: _T) -> None: ... def extend(self, __iterable: Iterable[_T]) -> None: ... def pop(self, __index: SupportsIndex = ...) -> _T: ... + # Signature of `list.index` should be kept in line with `collections.UserList.index()` def index(self, __value: _T, __start: SupportsIndex = ..., __stop: SupportsIndex = ...) -> int: ... def count(self, __value: _T) -> int: ... def insert(self, __index: SupportsIndex, __object: _T) -> None: ... def remove(self, __value: _T) -> None: ... def reverse(self) -> None: ... + # Signature of `list.sort` should be kept inline with `collections.UserList.sort()` @overload def sort(self: list[SupportsLessThanT], *, key: None = ..., reverse: bool = ...) -> None: ... @overload @@ -840,6 +842,9 @@ 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` + # 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 @overload def fromkeys(cls, __iterable: Iterable[_T], __value: None = ...) -> dict[_T, Any | None]: ... diff --git a/stdlib/collections/__init__.pyi b/stdlib/collections/__init__.pyi index 5f8200d5c..17094bfb3 100644 --- a/stdlib/collections/__init__.pyi +++ b/stdlib/collections/__init__.pyi @@ -77,8 +77,10 @@ class UserList(MutableSequence[_T]): def clear(self) -> None: ... def copy(self: _S) -> _S: ... def count(self, item: _T) -> int: ... + # All arguments are passed to `list.index` at runtime, so the signature should be kept in line with `list.index`. def index(self, item: _T, __start: SupportsIndex = ..., __stop: SupportsIndex = ...) -> int: ... def reverse(self) -> None: ... + # All arguments are passed to `list.sort` at runtime, so the signature should be kept in line with `list.sort`. @overload def sort(self: UserList[SupportsLessThanT], *, key: None = ..., reverse: bool = ...) -> None: ... @overload @@ -273,6 +275,15 @@ class OrderedDict(Dict[_KT, _VT], Reversible[_KT], Generic[_KT, _VT]): def keys(self) -> _OrderedDictKeysView[_KT, _VT]: ... def items(self) -> _OrderedDictItemsView[_KT, _VT]: ... def values(self) -> _OrderedDictValuesView[_KT, _VT]: ... + # `fromkeys` is actually inherited from `dict` at runtime, so the signature should be kept in line with `dict.fromkeys`. + # Ideally we would not redefine it here, but 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 + @overload + def fromkeys(cls, __iterable: Iterable[_T], __value: None = ...) -> OrderedDict[_T, Any | None]: ... + @classmethod + @overload + def fromkeys(cls, __iterable: Iterable[_T], __value: _S) -> OrderedDict[_T, _S]: ... class defaultdict(Dict[_KT, _VT], Generic[_KT, _VT]): default_factory: Callable[[], _VT] | None @@ -314,6 +325,7 @@ class ChainMap(MutableMapping[_KT, _VT], Generic[_KT, _VT]): @overload def pop(self, key: _KT, default: _VT | _T = ...) -> _VT | _T: ... def copy(self: Self) -> Self: ... + # All arguments to `fromkeys` are passed to `dict.fromkeys` at runtime, so the signature should be kept in line with `dict.fromkeys`. @classmethod @overload def fromkeys(cls, iterable: Iterable[_T], __value: None = ...) -> ChainMap[_T, Any | None]: ...