diff --git a/Lib/_abcoll.py b/Lib/_abcoll.py index 03856277f07..b643692e391 100644 --- a/Lib/_abcoll.py +++ b/Lib/_abcoll.py @@ -453,6 +453,7 @@ class KeysView(MappingView, Set): for key in self._mapping: yield key +KeysView.register(type({}.viewkeys())) class ItemsView(MappingView, Set): @@ -473,6 +474,7 @@ class ItemsView(MappingView, Set): for key in self._mapping: yield (key, self._mapping[key]) +ItemsView.register(type({}.viewitems())) class ValuesView(MappingView): @@ -486,6 +488,7 @@ class ValuesView(MappingView): for key in self._mapping: yield self._mapping[key] +ValuesView.register(type({}.viewvalues())) class MutableMapping(Mapping): diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index cd272278676..09b5c544fe1 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -1286,6 +1286,10 @@ class TestOrderedDict(unittest.TestCase): self.assertEqual(list(od.viewvalues()), [None for k in s]) self.assertEqual(list(od.viewitems()), [(k, None) for k in s]) + # See http://bugs.python.org/issue24286 + self.assertEqual(od.viewkeys(), dict(od).viewkeys()) + self.assertEqual(od.viewitems(), dict(od).viewitems()) + def test_override_update(self): # Verify that subclasses can override update() without breaking __init__() class MyOD(OrderedDict): diff --git a/Lib/test/test_dictviews.py b/Lib/test/test_dictviews.py index 30cfb93be7d..c5d391793a2 100644 --- a/Lib/test/test_dictviews.py +++ b/Lib/test/test_dictviews.py @@ -1,4 +1,5 @@ import unittest +import collections from test import test_support class DictSetTest(unittest.TestCase): @@ -164,7 +165,26 @@ class DictSetTest(unittest.TestCase): d[42] = d.viewvalues() self.assertRaises(RuntimeError, repr, d) + def test_abc_registry(self): + d = dict(a=1) + self.assertIsInstance(d.viewkeys(), collections.KeysView) + self.assertIsInstance(d.viewkeys(), collections.MappingView) + self.assertIsInstance(d.viewkeys(), collections.Set) + self.assertIsInstance(d.viewkeys(), collections.Sized) + self.assertIsInstance(d.viewkeys(), collections.Iterable) + self.assertIsInstance(d.viewkeys(), collections.Container) + + self.assertIsInstance(d.viewvalues(), collections.ValuesView) + self.assertIsInstance(d.viewvalues(), collections.MappingView) + self.assertIsInstance(d.viewvalues(), collections.Sized) + + self.assertIsInstance(d.viewitems(), collections.ItemsView) + self.assertIsInstance(d.viewitems(), collections.MappingView) + self.assertIsInstance(d.viewitems(), collections.Set) + self.assertIsInstance(d.viewitems(), collections.Sized) + self.assertIsInstance(d.viewitems(), collections.Iterable) + self.assertIsInstance(d.viewitems(), collections.Container) def test_main(): diff --git a/Misc/NEWS b/Misc/NEWS index 41e926a0c60..01fd7639b6d 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -21,6 +21,10 @@ Library - Issue #24257: Fixed segmentation fault in sqlite3.Row constructor with faked cursor type. +- Issue #24286: Dict view were not registered with the MappingView abstract + base classes. This caused key and item views in OrderedDict to not be equal + to their regular dict counterparts. + - Issue #22107: tempfile.gettempdir() and tempfile.mkdtemp() now try again when a directory with the chosen name already exists on Windows as well as on Unix. tempfile.mkstemp() now fails early if parent directory is not