From 983a9d014d728a5c980cf4129f9f46e5ca848048 Mon Sep 17 00:00:00 2001 From: AlexWaygood Date: Thu, 18 Aug 2022 08:27:25 +0100 Subject: [PATCH 1/5] Improve classes returned from `OrderedDict.{keys(), items(), values()}` --- stdlib/collections/__init__.pyi | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/stdlib/collections/__init__.pyi b/stdlib/collections/__init__.pyi index 40cf999dfae1..95522b651ff0 100644 --- a/stdlib/collections/__init__.pyi +++ b/stdlib/collections/__init__.pyi @@ -301,16 +301,29 @@ class Counter(dict[_T, int], Generic[_T]): def __ge__(self, other: Counter[Any]) -> bool: ... def __gt__(self, other: Counter[Any]) -> bool: ... +###### +# These three classes (`_odict_keys`, `_odict_items`, `_odict_values`) +# represent the types returned by `OrderedDict.keys()`, `OrderedDict.items()` and `OrderedDict.values()`. +# At runtime these classes are `odict_keys`, `odict_items` and `odict_values`, +# but they are not exposed anywhere (they are implemented in C). +# +# These classes are similar to the pure-Python equivalents exposed at runtime +# (`collections._OrderedDictKeysView`, `collections._OrderedDictItemsView`, `collections._OrderedDictValuesView`), +# but they have two differences: +# - They inherit from `dict_keys`, `dict_items` and `dict_values`. +# - They cannot be subclassed. +###### + @final -class _OrderedDictKeysView(dict_keys[_KT_co, _VT_co], Reversible[_KT_co]): # type: ignore[misc] +class _odict_keys(dict_keys[_KT_co, _VT_co], Reversible[_KT_co]): # type: ignore[misc] def __reversed__(self) -> Iterator[_KT_co]: ... @final -class _OrderedDictItemsView(dict_items[_KT_co, _VT_co], Reversible[tuple[_KT_co, _VT_co]]): # type: ignore[misc] +class _odict_items(dict_items[_KT_co, _VT_co], Reversible[tuple[_KT_co, _VT_co]]): # type: ignore[misc] def __reversed__(self) -> Iterator[tuple[_KT_co, _VT_co]]: ... @final -class _OrderedDictValuesView(dict_values[_KT_co, _VT_co], Reversible[_VT_co], Generic[_KT_co, _VT_co]): # type: ignore[misc] +class _odict_values(dict_values[_KT_co, _VT_co], Reversible[_VT_co], Generic[_KT_co, _VT_co]): # type: ignore[misc] def __reversed__(self) -> Iterator[_VT_co]: ... class OrderedDict(dict[_KT, _VT], Reversible[_KT], Generic[_KT, _VT]): @@ -318,9 +331,9 @@ class OrderedDict(dict[_KT, _VT], Reversible[_KT], Generic[_KT, _VT]): def move_to_end(self, key: _KT, last: bool = ...) -> None: ... def copy(self: Self) -> Self: ... def __reversed__(self) -> Iterator[_KT]: ... - def keys(self) -> _OrderedDictKeysView[_KT, _VT]: ... - def items(self) -> _OrderedDictItemsView[_KT, _VT]: ... - def values(self) -> _OrderedDictValuesView[_KT, _VT]: ... + def keys(self) -> _odict_keys[_KT, _VT]: ... + def items(self) -> _odict_items[_KT, _VT]: ... + def values(self) -> _odict_values[_KT, _VT]: ... # The signature of OrderedDict.fromkeys should be kept in line with `dict.fromkeys`, modulo positional-only differences. # Like dict.fromkeys, its true signature is not expressible in the current type system. # See #3800 & https://github.com/python/typing/issues/548#issuecomment-683336963. From 65831270725250c0b848e39bdb9393ddc04449bc Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sat, 20 Aug 2022 22:00:24 +0100 Subject: [PATCH 2/5] Update __init__.pyi --- stdlib/collections/__init__.pyi | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/stdlib/collections/__init__.pyi b/stdlib/collections/__init__.pyi index 95522b651ff0..5ee99cebb451 100644 --- a/stdlib/collections/__init__.pyi +++ b/stdlib/collections/__init__.pyi @@ -1,6 +1,7 @@ import sys from _collections_abc import dict_items, dict_keys, dict_values from _typeshed import Self, SupportsKeysAndGetItem, SupportsRichComparison, SupportsRichComparisonT +from collections.abc import KeysView, ItemsView, ValuesView from typing import Any, Generic, NoReturn, TypeVar, overload from typing_extensions import SupportsIndex, final @@ -301,19 +302,20 @@ class Counter(dict[_T, int], Generic[_T]): def __ge__(self, other: Counter[Any]) -> bool: ... def __gt__(self, other: Counter[Any]) -> bool: ... -###### -# These three classes (`_odict_keys`, `_odict_items`, `_odict_values`) -# represent the types returned by `OrderedDict.keys()`, `OrderedDict.items()` and `OrderedDict.values()`. -# At runtime these classes are `odict_keys`, `odict_items` and `odict_values`, -# but they are not exposed anywhere (they are implemented in C). -# -# These classes are similar to the pure-Python equivalents exposed at runtime -# (`collections._OrderedDictKeysView`, `collections._OrderedDictItemsView`, `collections._OrderedDictValuesView`), -# but they have two differences: -# - They inherit from `dict_keys`, `dict_items` and `dict_values`. -# - They cannot be subclassed. -###### +# The pure-Python implementations of the "views" classes +# These are exposed at runtime in `collections/__init__.py` +class _OrderedDictKeysView(KeysView[_KT_co], Reversible[_KT_co]): + def __reversed__(self) -> Iterator[_KT_co]: ... + +class _OrderedDictItemsView(ItemsView[_KT_co, _VT_co], Reversible[tuple[_KT_co, _VT_co]]): + def __reversed__(self) -> Iterator[tuple[_KT_co, _VT_co]]: ... + +class _OrderedDictValuesView(ValuesView[_VT_co], Reversible[_VT_co]): + def __reversed__(self) -> Iterator[_VT_co]: ... +# The C implementations of the "views" classes +# (At runtime, these are called `odict_keys`, `odict_items` and `odict_values`, +# but they are not exposed anywhere) @final class _odict_keys(dict_keys[_KT_co, _VT_co], Reversible[_KT_co]): # type: ignore[misc] def __reversed__(self) -> Iterator[_KT_co]: ... From 4dc7de71e806827efd7873e69a6dbcdacf41d0ae Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 20 Aug 2022 21:01:31 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/collections/__init__.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/collections/__init__.pyi b/stdlib/collections/__init__.pyi index 5ee99cebb451..b90c24aee800 100644 --- a/stdlib/collections/__init__.pyi +++ b/stdlib/collections/__init__.pyi @@ -1,7 +1,7 @@ import sys from _collections_abc import dict_items, dict_keys, dict_values from _typeshed import Self, SupportsKeysAndGetItem, SupportsRichComparison, SupportsRichComparisonT -from collections.abc import KeysView, ItemsView, ValuesView +from collections.abc import ItemsView, KeysView, ValuesView from typing import Any, Generic, NoReturn, TypeVar, overload from typing_extensions import SupportsIndex, final From f5bef54a3b77d0120e63c054137b55badf8efc18 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Sat, 20 Aug 2022 22:04:10 +0100 Subject: [PATCH 4/5] Update __init__.pyi --- stdlib/collections/__init__.pyi | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/stdlib/collections/__init__.pyi b/stdlib/collections/__init__.pyi index b90c24aee800..e7be83d3e35c 100644 --- a/stdlib/collections/__init__.pyi +++ b/stdlib/collections/__init__.pyi @@ -1,7 +1,6 @@ import sys from _collections_abc import dict_items, dict_keys, dict_values from _typeshed import Self, SupportsKeysAndGetItem, SupportsRichComparison, SupportsRichComparisonT -from collections.abc import ItemsView, KeysView, ValuesView from typing import Any, Generic, NoReturn, TypeVar, overload from typing_extensions import SupportsIndex, final @@ -9,7 +8,7 @@ if sys.version_info >= (3, 9): from types import GenericAlias if sys.version_info >= (3, 10): - from collections.abc import Callable, Iterable, Iterator, Mapping, MutableMapping, MutableSequence, Reversible, Sequence + from collections.abc import Callable, ItemsView, Iterable, Iterator, KeysView, Mapping, MutableMapping, MutableSequence, Reversible, Sequence, ValuesView else: from _collections_abc import * From 79073b43082c6ceeeacb66d377d75cb121afbbf9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 20 Aug 2022 21:05:18 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks --- stdlib/collections/__init__.pyi | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/stdlib/collections/__init__.pyi b/stdlib/collections/__init__.pyi index e7be83d3e35c..37505c256d9c 100644 --- a/stdlib/collections/__init__.pyi +++ b/stdlib/collections/__init__.pyi @@ -8,7 +8,19 @@ if sys.version_info >= (3, 9): from types import GenericAlias if sys.version_info >= (3, 10): - from collections.abc import Callable, ItemsView, Iterable, Iterator, KeysView, Mapping, MutableMapping, MutableSequence, Reversible, Sequence, ValuesView + from collections.abc import ( + Callable, + ItemsView, + Iterable, + Iterator, + KeysView, + Mapping, + MutableMapping, + MutableSequence, + Reversible, + Sequence, + ValuesView, + ) else: from _collections_abc import *