8000 Bring `_collections_abc` closer to runtime definition (#6312) · python/typeshed@fd48026 · GitHub
[go: up one dir, main page]

Skip to content

Commit fd48026

Browse files
authored
Bring _collections_abc closer to runtime definition (#6312)
1 parent 9c2be95 commit fd48026

File tree

3 files changed

+33
-25
lines changed

3 files changed

+33
-25
lines changed

stdlib/_collections_abc.pyi

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sys
2+
from types import MappingProxyType
13
from typing import (
24
AbstractSet as Set,
35
AsyncGenerator as AsyncGenerator,
@@ -10,6 +12,7 @@ from typing import (
1012
Container as Container,
1113
Coroutine as Coroutine,
1214
Generator as Generator,
15+
Generic,
1316
Hashable as Hashable,
1417
ItemsView as ItemsView,
1518
Iterable as Iterable,
@@ -23,8 +26,10 @@ from typing import (
2326
Reversible as Reversible,
2427
Sequence as Sequence,
2528
Sized as Sized,
29+
TypeVar,
2630
ValuesView as ValuesView,
2731
)
32+
from typing_extensions import final
2833

2934
__all__ = [
3035
"Awaitable",
@@ -53,3 +58,21 @@ __all__ = [
5358
"MutableSequence",
5459
"ByteString",
5560
]
61+
62+
_KT_co = TypeVar("_KT_co", covariant=True) # Key type covariant containers.
63+
_VT_co = TypeVar("_VT_co", covariant=True) # Value type covariant containers.
64+
65+
@final
66+
class dict_keys(KeysView[_KT_co], Generic[_KT_co, _VT_co]): # undocumented
67+
if sys.version_info >= (3, 10):
68+
mapping: MappingProxyType[_KT_co, _VT_co]
69+
70+
@final
71+
class dict_values(ValuesView[_VT_co], Generic[_KT_co, _VT_co]): # undocumented
72+
if sys.version_info >= (3, 10):
73+
mapping: MappingProxyType[_KT_co, _VT_co]
74+
75+
@final
76+
class dict_items(ItemsView[_KT_co, _VT_co], Generic[_KT_co, _VT_co]): # undocumented
77+
if sys.version_info >= (3, 10):
78+
mapping: MappingProxyType[_KT_co, _VT_co]

stdlib/builtins.pyi

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ from _typeshed import (
2020
SupportsWrite,
2121
)
2222
from io import BufferedRandom, BufferedReader, BufferedWriter, FileIO, TextIOWrapper
23-
from types import CodeType, MappingProxyType, TracebackType
23+
from types import CodeType, TracebackType
2424
from typing import (
2525
IO,
2626
AbstractSet,
@@ -32,10 +32,8 @@ from typing import (
3232
Callable,
3333
FrozenSet,
3434
Generic,
35-
ItemsView,
3635
Iterable,
3736
Iterator,
38-
KeysView,
3937
Mapping,
4038
MutableMapping,
4139
MutableSequence,
@@ -56,12 +54,12 @@ from typing import (
5654
Type,
5755
TypeVar,
5856
Union,
59-
ValuesView,
6057
overload,
6158
)
6259
from typing_extensions import Literal, SupportsIndex, TypeGuard, final
6360

6461
from _ast import AST
62+
from _collections_abc import dict_items, dict_keys, dict_values
6563

6664
if sys.version_info >= (3, 9):
6765
from types import GenericAlias
@@ -74,8 +72,6 @@ _T_co = TypeVar("_T_co", covariant=True)
7472
_T_contra = TypeVar("_T_contra", contravariant=True)
7573
_KT = TypeVar("_KT")
7674
_VT = TypeVar("_VT")
77-
_KT_co = TypeVar("_KT_co", covariant=True) # Key type covariant containers.
78-
_VT_co = TypeVar("_VT_co", covariant=True) # Value type covariant containers.
7975
_S = TypeVar("_S")
8076
_T1 = TypeVar("_T1")
8177
_T2 = TypeVar("_T2")
@@ -809,18 +805,6 @@ class list(MutableSequence[_T], Generic[_T]):
809805
if sys.version_info >= (3, 9):
810806
def __class_getitem__(cls, __item: Any) -> GenericAlias: ...
811807

812-
class _dict_keys(KeysView[_KT_co], Generic[_KT_co, _VT_co]):
813-
if sys.version_info >= (3, 10):
814-
mapping: MappingProxyType[_KT_co, _VT_co]
815-
816-
class _dict_values(ValuesView[_VT_co], Generic[_KT_co, _VT_co]):
817-
if sys.version_info >= (3, 10):
818-
mapping: MappingProxyType[_KT_co, _VT_co]
819-
820-
class _dict_items(ItemsView[_KT_co, _VT_co], Generic[_KT_co, _VT_co]):
821-
if sys.version_info >= (3, 10):
822-
mapping: MappingProxyType[_KT_co, _VT_co]
823-
824808
class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
825809
@overload
826810
def __init__(self: dict[_KT, _VT]) -> None: ...
@@ -845,9 +829,9 @@ class dict(MutableMapping[_KT, _VT], Generic[_KT, _VT]):
845829
def update(self, __m: Iterable[tuple[_KT, _VT]], **kwargs: _VT) -> None: ...
846830
@overload
847831
def update(self, **kwargs: _VT) -> None: ...
848-
def keys(self) -> _dict_keys[_KT, _VT]: ...
849-
def values(self) -> _dict_values[_KT, _VT]: ...
850-
def items(self) -> _dict_items[_KT, _VT]: ...
832+
def keys(self) -> dict_keys[_KT, _VT]: ...
833+
def values(self) -> dict_values[_KT, _VT]: ...
834+
def items(self) -> dict_items[_KT, _VT]: ...
851835
@classmethod
852836
@overload
853837
def fromkeys(cls, __iterable: Iterable[_T], __value: None = ...) -> dict[_T, Any | None]: ...

stdlib/collections/__init__.pyi

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import sys
22
from _typeshed import Self
3-
from builtins import _dict_items, _dict_keys, _dict_values
43
from typing import Any, Dict, Generic, NoReturn, Tuple, Type, TypeVar, overload
54
from typing_extensions import final
65

6+
from _collections_abc import dict_items, dict_keys, dict_values
7+
78
if sys.version_info >= (3, 10):
89
from typing import Callable, Iterable, Iterator, Mapping, MutableMapping, MutableSequence, Reversible, Sequence
910
else:
@@ -242,15 +243,15 @@ class Counter(Dict[_T, int], Generic[_T]):
242243
def __ior__(self, other: Counter[_T]) -> Counter[_T]: ... # type: ignore
243244

244245
@final
245-
class _OrderedDictKeysView(_dict_keys[_KT_co, _VT_co], Reversible[_KT_co]):
246+
class _OrderedDictKeysView(dict_keys[_KT_co, _VT_co], Reversible[_KT_co]): # type: ignore[misc]
246247
def __reversed__(self) -> Iterator[_KT_co]: ...
247248

248249
@final
249-
class _OrderedDictItemsView(_dict_items[_KT_co, _VT_co], Reversible[Tuple[_KT_co, _VT_co]]):
250+
class _OrderedDictItemsView(dict_items[_KT_co, _VT_co], Reversible[Tuple[_KT_co, _VT_co]]): # type: ignore[misc]
250251
def __reversed__(self) -> Iterator[tuple[_KT_co, _VT_co]]: ...
251252

252253
@final
253-
class _OrderedDictValuesView(_dict_values[_KT_co, _VT_co], Reversible[_VT_co], Generic[_KT_co, _VT_co]):
254+
class _OrderedDictValuesView(dict_values[_KT_co, _VT_co], Reversible[_VT_co], Generic[_KT_co, _VT_co]): # type: ignore[misc]
254255
def __reversed__(self) -> Iterator[_VT_co]: ...
255256

256257
class OrderedDict(Dict[_KT, _VT], Reversible[_KT], Generic[_KT, _VT]):

0 commit comments

Comments
 (0)
0