Commit d1388921 authored by Eric V. Smith's avatar Eric V. Smith Committed by GitHub

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

parent b216a253
import sys import sys
import types import types
from copy import deepcopy from copy import deepcopy
import collections
import inspect import inspect
__all__ = ['dataclass', __all__ = ['dataclass',
...@@ -448,11 +447,11 @@ def _set_attribute(cls, name, value): ...@@ -448,11 +447,11 @@ def _set_attribute(cls, name, value):
def _process_class(cls, repr, eq, order, hash, init, frozen): def _process_class(cls, repr, eq, order, hash, init, frozen):
# Use an OrderedDict because: # Now that dicts retain insertion order, there's no reason to use
# - Order matters! # an ordered dict. I am leveraging that ordering here, because
# - Derived class fields overwrite base class fields, but the # derived class fields overwrite base class fields, but the order
# order is defined by the base class, which is found first. # is defined by the base class, which is found first.
fields = collections.OrderedDict() fields = {}
# Find our base classes in reverse MRO order, and exclude # Find our base classes in reverse MRO order, and exclude
# ourselves. In reversed order so that more derived classes # ourselves. In reversed order so that more derived classes
...@@ -612,7 +611,8 @@ def fields(class_or_instance): ...@@ -612,7 +611,8 @@ def fields(class_or_instance):
except AttributeError: except AttributeError:
raise TypeError('must be called with a dataclass type or instance') 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) return tuple(f for f in fields.values() if f._field_type is _FIELD)
...@@ -735,7 +735,7 @@ def make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True, ...@@ -735,7 +735,7 @@ def make_dataclass(cls_name, fields, *, bases=(), namespace=None, init=True,
# Copy namespace since we're going to mutate it. # Copy namespace since we're going to mutate it.
namespace = namespace.copy() namespace = namespace.copy()
anns = collections.OrderedDict() anns = {}
for item in fields: for item in fields:
if isinstance(item, str): if isinstance(item, str):
name = item name = item
......
Now that dict is defined as keeping insertion order, drop OrderedDict and
just use plain dict.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment