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

Skip to content

Commit d138892

Browse files
authored
bpo-32506: Change dataclasses from OrderedDict to plain dict. (gh-5131)
1 parent b216a25 commit d138892

2 files changed

Lines changed: 10 additions & 8 deletions

File tree

Lib/dataclasses.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import sys
22
import types
33
from copy import deepcopy
4-
import collections
54
import inspect
65

76
__all__ = ['dataclass',
@@ -448,11 +447,11 @@ def _set_attribute(cls, name, value):
448447

449448

450449
def _process_class(cls, repr, eq, order, hash, init, frozen):
451-
# Use an OrderedDict because:
452-
# - Order matters!
453-
# - Derived class fields overwrite base class fields, but the
454-
# order is defined by the base class, which is found first.
455-
fields = collections.OrderedDict()
450+
# Now that dicts retain insertion order, there's no reason to use
451+
# an ordered dict. I am leveraging that ordering here, because
452+
# derived class fields overwrite base class fields, but the order
453+
# is defined by the base class, which is found first.
454+
fields = {}
456455

457456
# Find our base classes in reverse MRO order, and exclude
458457
# ourselves. In reversed order so that more derived classes
@@ -612,7 +611,8 @@ def fields(class_or_instance):
612611
except AttributeError:
613612
raise TypeError('must be called with a dataclass type or instance')
614613

615-
# Exclude pseudo-fields.
614+
# Exclude pseudo-fields. Note that fields is sorted by insertion
615+
# order, so the order of the tuple is as the fields were defined.
616616
return tuple(f for f in fields.values() if f._field_type is _FIELD)
617617

618618

@@ -735,7 +735,7 @@ class C(Base):
735735
# Copy namespace since we're going to mutate it.
736736
namespace = namespace.copy()
737737

738-
anns = collections.OrderedDict()
738+
anns = {}
739739
for item in fields:
740740
if isinstance(item, str):
741741
name = item
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Now that dict is defined as keeping insertion order, drop OrderedDict and
2+
just use plain dict.

0 commit comments

Comments
 (0)