summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2015-09-29 20:37:09 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2015-09-29 20:37:09 (GMT)
commitf4ee1c23e8c2981c01bca7a9bcb3904833726c18 (patch)
tree827d7e2f7ba4fc652c80239d18ff0c2ee4423efa /Lib
parent3066fc41d1a054e7734e8334af9758173ee0aa9d (diff)
parent68f5ef226e9b62b1755ee98ae3d35a4aedc56684 (diff)
downloadcpython-f4ee1c23e8c2981c01bca7a9bcb3904833726c18.zip
cpython-f4ee1c23e8c2981c01bca7a9bcb3904833726c18.tar.gz
cpython-f4ee1c23e8c2981c01bca7a9bcb3904833726c18.tar.bz2
Issue #22609: Constructor of collections.UserDict now accepts the self keyword
argument.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/collections/__init__.py17
-rw-r--r--Lib/test/test_userdict.py27
2 files changed, 42 insertions, 2 deletions
diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py
index 943cbca..e8312a9 100644
--- a/Lib/collections/__init__.py
+++ b/Lib/collections/__init__.py
@@ -939,7 +939,22 @@ class ChainMap(MutableMapping):
class UserDict(MutableMapping):
# Start by filling-out the abstract methods
- def __init__(self, dict=None, **kwargs):
+ def __init__(*args, **kwargs):
+ if not args:
+ raise TypeError("descriptor '__init__' of 'UserDict' object "
+ "needs an argument")
+ self, *args = args
+ if len(args) > 1:
+ raise TypeError('expected at most 1 arguments, got %d' % len(args))
+ if args:
+ dict = args[0]
+ elif 'dict' in kwargs:
+ dict = kwargs.pop('dict')
+ import warnings
+ warnings.warn("Passing 'dict' as keyword argument is deprecated",
+ PendingDeprecationWarning, stacklevel=2)
+ else:
+ dict = None
self.data = {}
if dict is not None:
self.update(dict)
diff --git a/Lib/test/test_userdict.py b/Lib/test/test_userdict.py
index c83cc55..68a582c 100644
--- a/Lib/test/test_userdict.py
+++ b/Lib/test/test_userdict.py
@@ -29,7 +29,8 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
self.assertEqual(collections.UserDict(one=1, two=2), d2)
# item sequence constructor
self.assertEqual(collections.UserDict([('one',1), ('two',2)]), d2)
- self.assertEqual(collections.UserDict(dict=[('one',1), ('two',2)]), d2)
+ with self.assertWarnsRegex(PendingDeprecationWarning, "'dict'"):
+ self.assertEqual(collections.UserDict(dict=[('one',1), ('two',2)]), d2)
# both together
self.assertEqual(collections.UserDict([('one',1), ('two',2)], two=3, three=5), d3)
@@ -139,6 +140,30 @@ class UserDictTest(mapping_tests.TestHashMappingProtocol):
self.assertEqual(t.popitem(), ("x", 42))
self.assertRaises(KeyError, t.popitem)
+ def test_init(self):
+ for kw in 'self', 'other', 'iterable':
+ self.assertEqual(list(collections.UserDict(**{kw: 42}).items()),
+ [(kw, 42)])
+ self.assertEqual(list(collections.UserDict({}, dict=42).items()),
+ [('dict', 42)])
+ self.assertEqual(list(collections.UserDict({}, dict=None).items()),
+ [('dict', None)])
+ with self.assertWarnsRegex(PendingDeprecationWarning, "'dict'"):
+ self.assertEqual(list(collections.UserDict(dict={'a': 42}).items()),
+ [('a', 42)])
+ self.assertRaises(TypeError, collections.UserDict, 42)
+ self.assertRaises(TypeError, collections.UserDict, (), ())
+ self.assertRaises(TypeError, collections.UserDict.__init__)
+
+ def test_update(self):
+ for kw in 'self', 'dict', 'other', 'iterable':
+ d = collections.UserDict()
+ d.update(**{kw: 42})
+ self.assertEqual(list(d.items()), [(kw, 42)])
+ self.assertRaises(TypeError, collections.UserDict().update, 42)
+ self.assertRaises(TypeError, collections.UserDict().update, {}, {})
+ self.assertRaises(TypeError, collections.UserDict.update)
+
def test_missing(self):
# Make sure UserDict doesn't have a __missing__ method
self.assertEqual(hasattr(collections.UserDict, "__missing__"), False)