New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The result of calling dict.* methods on OrderedDict is undefined. #68909
Comments
(see bpo-24667) collections.OrderedDict subclasses dict so calling dict's methods on an OrderedDict works. However, neither the pure Python nor the C implementation of OrderedDict was written to support doing so. In fact, both of them currently enter an inconsistent state when this happens. For example: # Python 3.4 (pure Python implementation)
>>> from collections import OrderedDict
>>> od = OrderedDict([('spam', 1), ('eggs', 2)])
>>> dict.__delitem__(od, 'spam')
>>> str(od)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/reprlib.py", line 24, in wrapper
result = user_function(self)
File "/usr/lib/python3.4/collections/__init__.py", line 198, in __repr__
return '%s(%r)' % (self.__class__.__name__, list(self.items()))
File "/usr/lib/python3.4/_collections_abc.py", line 485, in __iter__
yield (key, self._mapping[key])
KeyError: 'spam'
# Python 3.5 (C implementation)
>>> from collections import OrderedDict
>>> od = OrderedDict([('spam', 1), ('eggs', 2)])
>>> dict.__delitem__(od, 'spam')
>>> str(od)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'spam' This is a consequence of subclassing a builtin type, which typically do not have good support for subclassing (e.g. bpo-10977). It probably isn't worth making any changes to the code of either OrderedDict implementations. At most I'd recommend a note in the OrderedDict documentation indicating that the results of passing an OrderedDict object to dict.* methods are undefined. |
Not really. This is how subclassing works in general. Any time you a user calls a parent class directly on an instance of subclass, they are bypassing whatever the subclass needs to do to maintain its invariants. class A:
def __init__(self):
self.data = []
def add(self, x):
self.data.append(x)
class B(A):
'Track the number of odds'
def __init__(self):
A.__init__(self)
self.odds = 0
def add(self, x):
A.add(self, x)
self.odds += (x % 2)
b = B()
b.add(1)
b.add(2)
b.add(3)
b.add(4)
A.add(b, 5)
assert b.odds == sum(x%1 for x in b.data), 'OMG, B is broken!' There is nothing special about OrderedDicts in this regard. Perhaps there should be a FAQ entry regarding the "facts of life" in the world of object oriented programming. |
Ah, you're right. I was hung up on bpo-10977. :) |
Feel free to close this, Raymond. |
I think this is a bug. This is not a normal case of subclassing as the interpreter calls the C API PyDict_XXX() in many cases where a dictionary subclass is passed in. For example: All access to the ordered dict is via the dict.__setitem__ method. I think this should be documented. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: