Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 68f5ef2

Browse files
Issue #22609: Constructor of collections.UserDict now accepts the self keyword
argument.
1 parent e060619 commit 68f5ef2

3 files changed

Lines changed: 45 additions & 2 deletions

File tree

Lib/collections/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,22 @@ def clear(self):
883883
class UserDict(MutableMapping):
884884

885885
# Start by filling-out the abstract methods
886-
def __init__(self, dict=None, **kwargs):
886+
def __init__(*args, **kwargs):
887+
if not args:
888+
raise TypeError("descriptor '__init__' of 'UserDict' object "
889+
"needs an argument")
890+
self, *args = args
891+
if len(args) > 1:
892+
raise TypeError('expected at most 1 arguments, got %d' % len(args))
893+
if args:
894+
dict = args[0]
895+
elif 'dict' in kwargs:
896+
dict = kwargs.pop('dict')
897+
import warnings
898+
warnings.warn("Passing 'dict' as keyword argument is deprecated",
899+
PendingDeprecationWarning, stacklevel=2)
900+
else:
901+
dict = None
887902
self.data = {}
888903
if dict is not None:
889904
self.update(dict)

Lib/test/test_userdict.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ def test_all(self):
2929
self.assertEqual(collections.UserDict(one=1, two=2), d2)
3030
# item sequence constructor
3131
self.assertEqual(collections.UserDict([('one',1), ('two',2)]), d2)
32-
self.assertEqual(collections.UserDict(dict=[('one',1), ('two',2)]), d2)
32+
with self.assertWarnsRegex(PendingDeprecationWarning, "'dict'"):
33+
self.assertEqual(collections.UserDict(dict=[('one',1), ('two',2)]), d2)
3334
# both together
3435
self.assertEqual(collections.UserDict([('one',1), ('two',2)], two=3, three=5), d3)
3536

@@ -139,6 +140,30 @@ def display(self): print(self)
139140
self.assertEqual(t.popitem(), ("x", 42))
140141
self.assertRaises(KeyError, t.popitem)
141142

143+
def test_init(self):
144+
for kw in 'self', 'other', 'iterable':
145+
self.assertEqual(list(collections.UserDict(**{kw: 42}).items()),
146+
[(kw, 42)])
147+
self.assertEqual(list(collections.UserDict({}, dict=42).items()),
148+
[('dict', 42)])
149+
self.assertEqual(list(collections.UserDict({}, dict=None).items()),
150+
[('dict', None)])
151+
with self.assertWarnsRegex(PendingDeprecationWarning, "'dict'"):
152+
self.assertEqual(list(collections.UserDict(dict={'a': 42}).items()),
153+
[('a', 42)])
154+
self.assertRaises(TypeError, collections.UserDict, 42)
155+
self.assertRaises(TypeError, collections.UserDict, (), ())
156+
self.assertRaises(TypeError, collections.UserDict.__init__)
157+
158+
def test_update(self):
159+
for kw in 'self', 'dict', 'other', 'iterable':
160+
d = collections.UserDict()
161+
d.update(**{kw: 42})
162+
self.assertEqual(list(d.items()), [(kw, 42)])
163+
self.assertRaises(TypeError, collections.UserDict().update, 42)
164+
self.assertRaises(TypeError, collections.UserDict().update, {}, {})
165+
self.assertRaises(TypeError, collections.UserDict.update)
166+
142167
def test_missing(self):
143168
# Make sure UserDict doesn't have a __missing__ method
144169
self.assertEqual(hasattr(collections.UserDict, "__missing__"), False)

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@ Core and Builtins
7878
Library
7979
-------
8080

81+
- Issue #22609: Constructor of collections.UserDict now accepts the self keyword
82+
argument.
83+
8184
- Issue #25262. Added support for BINBYTES8 opcode in Python implementation of
8285
unpickler. Highest 32 bits of 64-bit size for BINUNICODE8 and BINBYTES8
8386
opcodes no longer silently ignored on 32-bit platforms in C implementation.

0 commit comments

Comments
 (0)