cpython: acd9a465b373 (original) (raw)

Mercurial > cpython

changeset 102866:acd9a465b373

Issue 27598: Add Collections to collections.abc. Patch by Ivan Levkivskyi, docs by Neil Girdhar.

Guido van Rossum guido@dropbox.com
date Tue, 23 Aug 2016 10:47:07 -0700
parents d43a77b6f865
children 013bb690c24a
files Doc/library/collections.abc.rst Lib/_collections_abc.py Lib/test/test_collections.py Lib/test/test_functools.py Misc/NEWS
diffstat 5 files changed, 132 insertions(+), 21 deletions(-)[+] [-] Doc/library/collections.abc.rst 26 Lib/_collections_abc.py 17 Lib/test/test_collections.py 90 Lib/test/test_functools.py 17 Misc/NEWS 3

line wrap: on

line diff

--- a/Doc/library/collections.abc.rst +++ b/Doc/library/collections.abc.rst @@ -45,10 +45,12 @@ ABC Inherits from :class:Generator :class:Iterator send, throw close, __iter__, __next__ :class:Sized __len__ :class:Callable __call__ +:class:Collection :class:Sized, __contains__,

-:class:Sequence :class:Sized, __getitem__, __contains__, __iter__, __reversed__,

+:class:Sequence :class:Reversible, __getitem__, __contains__, __iter__, __reversed__,

:class:MutableSequence :class:Sequence __getitem__, Inherited :class:Sequence methods and __setitem__, append, reverse, extend, pop, @@ -59,9 +61,9 @@ ABC Inherits from :class:ByteString :class:Sequence __getitem__, Inherited :class:Sequence methods __len__ -:class:Set :class:Sized, __contains__, __le__, __lt__, __eq__, __ne__,

+:class:Set :class:Collection __contains__, __le__, __lt__, __eq__, __ne__,

:class:MutableSet :class:Set __contains__, Inherited :class:Set methods and __iter__, clear, pop, remove, __ior__, @@ -69,9 +71,9 @@ ABC Inherits from add, discard -:class:Mapping :class:Sized, __getitem__, __contains__, keys, items, values,

+:class:Mapping :class:Collection __getitem__, __contains__, keys, items, values,

:class:MutableMapping :class:Mapping __getitem__, Inherited :class:Mapping methods and __setitem__, pop, popitem, clear, update, @@ -106,6 +108,12 @@ ABC Inherits from ABC for classes that provide the :meth:__iter__ method. See also the definition of :term:iterable. +.. class:: Collection +

.. class:: Iterator ABC for classes that provide the :meth:~iterator.__iter__ and

--- a/Lib/_collections_abc.py +++ b/Lib/_collections_abc.py @@ -11,7 +11,7 @@ import sys all = ["Awaitable", "Coroutine", "AsyncIterable", "AsyncIterator", "Hashable", "Iterable", "Iterator", "Generator", "Reversible",

@@ -326,6 +326,15 @@ class Container(metaclass=ABCMeta): return _check_methods(C, "contains") return NotImplemented +class Collection(Sized, Iterable, Container): +

+

class Callable(metaclass=ABCMeta): @@ -345,7 +354,7 @@ class Callable(metaclass=ABCMeta):

SETS ###

-class Set(Sized, Iterable, Container): +class Set(Collection): """A set is a finite, iterable container. @@ -570,7 +579,7 @@ MutableSet.register(set)

MAPPINGS ###

-class Mapping(Sized, Iterable, Container): +class Mapping(Collection): slots = () @@ -794,7 +803,7 @@ MutableMapping.register(dict)

SEQUENCES ###

-class Sequence(Sized, Reversible, Container): +class Sequence(Reversible, Collection): """All the operations on a read-only sequence.

--- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -21,7 +21,7 @@ from collections import ChainMap from collections import deque from collections.abc import Awaitable, Coroutine, AsyncIterator, AsyncIterable from collections.abc import Hashable, Iterable, Iterator, Generator, Reversible -from collections.abc import Sized, Container, Callable +from collections.abc import Sized, Container, Callable, Collection from collections.abc import Set, MutableSet from collections.abc import Mapping, MutableMapping, KeysView, ItemsView, ValuesView from collections.abc import Sequence, MutableSequence @@ -771,6 +771,94 @@ class TestOneTrickPonyABCs(ABCTestCase): self.assertFalse(issubclass(RevRevBlocked, Reversible)) self.assertFalse(isinstance(RevRevBlocked(), Reversible))

+ + def test_Iterator(self): non_samples = [None, 42, 3.14, 1j, b"", "", (), [], {}, set()] for x in non_samples:

--- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -1548,13 +1548,15 @@ class TestSingleDispatch(unittest.TestCa bases = [c.Sequence, c.MutableMapping, c.Mapping, c.Set] for haystack in permutations(bases): m = mro(dict, haystack)

# If there's a generic function with implementations registered for # both Sized and Container, passing a defaultdict to it results in an @@ -1575,9 +1577,9 @@ class TestSingleDispatch(unittest.TestCa bases = [c.MutableSequence, c.MutableMapping] for haystack in permutations(bases): m = mro(D, bases)

# Container and Callable are registered on different base classes and @@ -1590,7 +1592,8 @@ class TestSingleDispatch(unittest.TestCa for haystack in permutations(bases): m = mro(C, haystack) self.assertEqual(m, [C, c.Callable, c.defaultdict, dict, c.Mapping,

def test_register_abc(self): c = collections

--- a/Misc/NEWS +++ b/Misc/NEWS @@ -126,6 +126,9 @@ Core and Builtins Library ------- +- Issue 27598: Add Collections to collections.abc.