[Python-checkins] bpo-32506: Change dataclasses from OrderedDict to plain dict. (gh-5131)

Eric V. Smith webhook-mailer at python.org
Sun Jan 7 14:30:19 EST 2018


https://github.com/python/cpython/commit/d13889214a4c81b78fa8683d35bdbd17ff22f4fe
commit: d13889214a4c81b78fa8683d35bdbd17ff22f4fe
branch: master
author: Eric V. Smith <ericvsmith at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018年01月07日T14:30:17-05:00
summary:
bpo-32506: Change dataclasses from OrderedDict to plain dict. (gh-5131)
files:
A Misc/NEWS.d/next/Library/2018-01-07-11-32-42.bpo-32506.MaT-zU.rst
M Lib/dataclasses.py
diff --git a/Lib/dataclasses.py b/Lib/dataclasses.py
index 9e186c3c7b7..d80054920ce 100644
--- a/Lib/dataclasses.py
+++ b/Lib/dataclasses.py
@@ -1,7 +1,6 @@
 import sys
 import types
 from copy import deepcopy
-import collections
 import inspect
 
 __all__ = ['dataclass',
@@ -448,11 +447,11 @@ def _set_attribute(cls, name, value):
 
 
 def _process_class(cls, repr, eq, order, hash, init, frozen):
- # Use an OrderedDict because:
- # - Order matters!
- # - Derived class fields overwrite base class fields, but the
- # order is defined by the base class, which is found first.
- fields = collections.OrderedDict()
+ # Now that dicts retain insertion order, there's no reason to use
+ # an ordered dict. I am leveraging that ordering here, because
+ # derived class fields overwrite base class fields, but the order
+ # is defined by the base class, which is found first.
+ fields = {}
 
 # Find our base classes in reverse MRO order, and exclude
 # ourselves. In reversed order so that more derived classes
@@ -612,7 +611,8 @@ def fields(class_or_instance):
 except AttributeError:
 raise TypeError('must be called with a dataclass type or instance')
 
- # Exclude pseudo-fields.
+ # Exclude pseudo-fields. Note that fields is sorted by insertion
+ # order, so the order of the tuple is as the fields were defined.
 return tuple(f for f in fields.values() if f._field_type is _FIELD)
 
 
@@ -735,7 +735,7 @@ class C(Base):
 # Copy namespace since we're going to mutate it.
 namespace = namespace.copy()
 
- anns = collections.OrderedDict()
+ anns = {}
 for item in fields:
 if isinstance(item, str):
 name = item
diff --git a/Misc/NEWS.d/next/Library/2018-01-07-11-32-42.bpo-32506.MaT-zU.rst b/Misc/NEWS.d/next/Library/2018-01-07-11-32-42.bpo-32506.MaT-zU.rst
new file mode 100644
index 00000000000..e524769c6df
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2018-01-07-11-32-42.bpo-32506.MaT-zU.rst
@@ -0,0 +1,2 @@
+Now that dict is defined as keeping insertion order, drop OrderedDict and
+just use plain dict.


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /