diff -r 0bbf121c48c6 Lib/collections/__init__.py --- a/Lib/collections/__init__.py Wed Mar 25 19:16:54 2015 +0200 +++ b/Lib/collections/__init__.py Wed Mar 25 20:42:03 2015 +0200 @@ -924,7 +924,23 @@ 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] + else: + if 'dict' in kwargs: + dict = kwargs.pop('dict') + import warnings + warnings.warn("Passing 'dict' as keyword argument is " + "deprecated", DeprecationWarning, stacklevel=2) + else: + dict = None self.data = {} if dict is not None: self.update(dict) diff -r 0bbf121c48c6 Lib/test/test_userdict.py --- a/Lib/test/test_userdict.py Wed Mar 25 19:16:54 2015 +0200 +++ b/Lib/test/test_userdict.py Wed Mar 25 20:42:03 2015 +0200 @@ -2,6 +2,7 @@ from test import support, mapping_tests import collections +from collections import UserDict d0 = {} d1 = {"one": 1} @@ -29,7 +30,8 @@ class UserDictTest(mapping_tests.TestHas 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 support.check_warnings(('.*dict.*', DeprecationWarning)): + 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 +141,26 @@ class UserDictTest(mapping_tests.TestHas self.assertEqual(t.popitem(), ("x", 42)) self.assertRaises(KeyError, t.popitem) + def test_init(self): + for kw in 'self', 'other', 'iterable': + self.assertEqual(list(UserDict(**{kw: 42}).items()), [(kw, 42)]) + self.assertEqual(list(UserDict({}, dict=42).items()), [('dict', 42)]) + self.assertEqual(list(UserDict({}, dict=None).items()), [('dict', None)]) + with support.check_warnings(('.*dict.*', DeprecationWarning)): + self.assertEqual(list(UserDict(dict={'a': 42}).items()), [('a', 42)]) + self.assertRaises(TypeError, UserDict, 42) + self.assertRaises(TypeError, UserDict, (), ()) + self.assertRaises(TypeError, UserDict.__init__) + + def test_update(self): + for kw in 'self', 'dict', 'other', 'iterable': + d = UserDict() + d.update(**{kw: 42}) + self.assertEqual(list(d.items()), [(kw, 42)]) + self.assertRaises(TypeError, UserDict().update, 42) + self.assertRaises(TypeError, UserDict().update, {}, {}) + self.assertRaises(TypeError, UserDict.update) + def test_missing(self): # Make sure UserDict doesn't have a __missing__ method self.assertEqual(hasattr(collections.UserDict, "__missing__"), False)