This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author eric.snow
Recipients docs@python, eric.snow, rhettinger
Date 2015-07-25.17:10:57
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1437844258.42.0.543403557619.issue24721@psf.upfronthosting.co.za>
In-reply-to
Content
(see issue24667)

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. issue10977).

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.
History
Date User Action Args
2015-07-25 17:10:58eric.snowsetrecipients: + eric.snow, rhettinger, docs@python
2015-07-25 17:10:58eric.snowsetmessageid: <1437844258.42.0.543403557619.issue24721@psf.upfronthosting.co.za>
2015-07-25 17:10:58eric.snowlinkissue24721 messages
2015-07-25 17:10:57eric.snowcreate