]> granicus.if.org Git - python/commitdiff
Issue #22609: Constructor of collections.UserDict now accepts the self keyword
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 29 Sep 2015 20:36:06 +0000 (23:36 +0300)
committerSerhiy Storchaka <storchaka@gmail.com>
Tue, 29 Sep 2015 20:36:06 +0000 (23:36 +0300)
argument.

Lib/collections/__init__.py
Lib/test/test_userdict.py
Misc/NEWS

index 339552ef088b26b43d81ea0358e3a37701424b1a..f94210efa21d31d9ab33fa60398f6beb27b3f2b2 100644 (file)
@@ -883,7 +883,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)
index 2ca99298d4bccdf07ed0abe85197210f9c539007..e7fee55ad873e723dcad891b16384307a7d1c75d 100644 (file)
@@ -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)
index a75b6e92cb4206512d6f4e692da24fb8b2335b7e..3a9ddda0552a9f7bf0acfcaf7db1f51abb40dbc3 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -78,6 +78,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #22609: Constructor of collections.UserDict now accepts the self keyword
+  argument.
+
 - Issue #25262. Added support for BINBYTES8 opcode in Python implementation of
   unpickler.  Highest 32 bits of 64-bit size for BINUNICODE8 and BINBYTES8
   opcodes no longer silently ignored on 32-bit platforms in C implementation.