[issue34320] Creating dict from OrderedDict doesn't preserve order

Eric Snow report at bugs.python.org
Thu Sep 20 12:17:17 EDT 2018


Eric Snow <ericsnowcurrently at gmail.com> added the comment:

FWIW, the PEP 520 example isn't exactly right.  PEP 520 is about the class definition namespace, not the namespace object passed to type().  That always does the right thing, since the default class definition namespace is dict (which happens to be ordered in 3.6+).

That said, a test that explicitly calls type() with an OrderedDict, as you have shown, is still a good idea since type() always changes the namespace to a dict.

Perhaps another good test of the same thing would be with a metaclass that returns an OrderedDict from __prepare__():

  class Meta(type):
      @classmethod
      def __prepare__(meta, name, bases, **kwargs):
          return OrderedDict()

  class Spam(metaclass=Meta):
      a = 1
      b = 2
      locals().move_to_end('a')

  Spam.__dict__

Just keep in mind that neither of those is specific to PEP 520.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34320>
_______________________________________


More information about the Python-bugs-list mailing list