summaryrefslogtreecommitdiffstats
path: root/Lib/_collections_abc.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2020-04-07 16:50:06 (GMT)
committerGitHub <noreply@github.com>2020-04-07 16:50:06 (GMT)
commit48b069a003ba6c684a9ba78493fbbec5e89f10b8 (patch)
treee67e71abd17516cea5fe1a1ec487bf929ab0b9fd /Lib/_collections_abc.py
parent9cc3ebd7e04cb645ac7b2f372eaafa7464e16b9c (diff)
downloadcpython-48b069a003ba6c684a9ba78493fbbec5e89f10b8.zip
cpython-48b069a003ba6c684a9ba78493fbbec5e89f10b8.tar.gz
cpython-48b069a003ba6c684a9ba78493fbbec5e89f10b8.tar.bz2
bpo-39481: Implementation for PEP 585 (#18239)
This implements things like `list[int]`, which returns an object of type `types.GenericAlias`. This object mostly acts as a proxy for `list`, but has attributes `__origin__` and `__args__` that allow recovering the parts (with values `list` and `(int,)`. There is also an approximate notion of type variables; e.g. `list[T]` has a `__parameters__` attribute equal to `(T,)`. Type variables are objects of type `typing.TypeVar`.
Diffstat (limited to 'Lib/_collections_abc.py')
-rw-r--r--Lib/_collections_abc.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/Lib/_collections_abc.py b/Lib/_collections_abc.py
index 2b2ddba..36cd993 100644
--- a/Lib/_collections_abc.py
+++ b/Lib/_collections_abc.py
@@ -9,6 +9,8 @@ Unit tests are in test_collections.
from abc import ABCMeta, abstractmethod
import sys
+GenericAlias = type(list[int])
+
__all__ = ["Awaitable", "Coroutine",
"AsyncIterable", "AsyncIterator", "AsyncGenerator",
"Hashable", "Iterable", "Iterator", "Generator", "Reversible",
@@ -110,6 +112,8 @@ class Awaitable(metaclass=ABCMeta):
return _check_methods(C, "__await__")
return NotImplemented
+ __class_getitem__ = classmethod(GenericAlias)
+
class Coroutine(Awaitable):
@@ -169,6 +173,8 @@ class AsyncIterable(metaclass=ABCMeta):
return _check_methods(C, "__aiter__")
return NotImplemented
+ __class_getitem__ = classmethod(GenericAlias)
+
class AsyncIterator(AsyncIterable):
@@ -255,6 +261,8 @@ class Iterable(metaclass=ABCMeta):
return _check_methods(C, "__iter__")
return NotImplemented
+ __class_getitem__ = classmethod(GenericAlias)
+
class Iterator(Iterable):
@@ -274,6 +282,7 @@ class Iterator(Iterable):
return _check_methods(C, '__iter__', '__next__')
return NotImplemented
+
Iterator.register(bytes_iterator)
Iterator.register(bytearray_iterator)
#Iterator.register(callable_iterator)
@@ -353,6 +362,7 @@ class Generator(Iterator):
'send', 'throw', 'close')
return NotImplemented
+
Generator.register(generator)
@@ -385,6 +395,9 @@ class Container(metaclass=ABCMeta):
return _check_methods(C, "__contains__")
return NotImplemented
+ __class_getitem__ = classmethod(GenericAlias)
+
+
class Collection(Sized, Iterable, Container):
__slots__ = ()
@@ -395,6 +408,7 @@ class Collection(Sized, Iterable, Container):
return _check_methods(C, "__len__", "__iter__", "__contains__")
return NotImplemented
+
class Callable(metaclass=ABCMeta):
__slots__ = ()
@@ -409,6 +423,8 @@ class Callable(metaclass=ABCMeta):
return _check_methods(C, "__call__")
return NotImplemented
+ __class_getitem__ = classmethod(GenericAlias)
+
### SETS ###
@@ -550,6 +566,7 @@ class Set(Collection):
h = 590923713
return h
+
Set.register(frozenset)
@@ -632,6 +649,7 @@ class MutableSet(Set):
self.discard(value)
return self
+
MutableSet.register(set)
@@ -688,6 +706,7 @@ class Mapping(Collection):
__reversed__ = None
+
Mapping.register(mappingproxy)
@@ -704,6 +723,8 @@ class MappingView(Sized):
def __repr__(self):
return '{0.__class__.__name__}({0._mapping!r})'.format(self)
+ __class_getitem__ = classmethod(GenericAlias)
+
class KeysView(MappingView, Set):
@@ -719,6 +740,7 @@ class KeysView(MappingView, Set):
def __iter__(self):
yield from self._mapping
+
KeysView.register(dict_keys)
@@ -743,6 +765,7 @@ class ItemsView(MappingView, Set):
for key in self._mapping:
yield (key, self._mapping[key])
+
ItemsView.register(dict_items)
@@ -761,6 +784,7 @@ class ValuesView(MappingView, Collection):
for key in self._mapping:
yield self._mapping[key]
+
ValuesView.register(dict_values)
@@ -847,6 +871,7 @@ class MutableMapping(Mapping):
self[key] = default
return default
+
MutableMapping.register(dict)
@@ -914,6 +939,7 @@ class Sequence(Reversible, Collection):
'S.count(value) -> integer -- return number of occurrences of value'
return sum(1 for v in self if v is value or v == value)
+
Sequence.register(tuple)
Sequence.register(str)
Sequence.register(range)
@@ -1000,5 +1026,6 @@ class MutableSequence(Sequence):
self.extend(values)
return self
+
MutableSequence.register(list)
MutableSequence.register(bytearray) # Multiply inheriting, see ByteString