bpo-36320: Switch typing.NamedTuple from OrderedDict to regular dict … · python/cpython@f7b57df (original) (raw)

`@@ -1325,8 +1325,8 @@ def _make_nmtuple(name, types):

`

1325

1325

`types = [(n, _type_check(t, msg)) for n, t in types]

`

1326

1326

`nm_tpl = collections.namedtuple(name, [n for n, t in types])

`

1327

1327

`# Prior to PEP 526, only _field_types attribute was assigned.

`

1328

``

`-

Now, both annotations and _field_types are used to maintain compatibility.

`

1329

``

`-

nm_tpl.annotations = nm_tpl._field_types = collections.OrderedDict(types)

`

``

1328

`+

Now annotations are used and _field_types is deprecated (remove in 3.9)

`

``

1329

`+

nm_tpl.annotations = nm_tpl._field_types = dict(types)

`

1330

1330

`try:

`

1331

1331

`nm_tpl.module = sys._getframe(2).f_globals.get('name', 'main')

`

1332

1332

`except (AttributeError, ValueError):

`

`@@ -1361,7 +1361,7 @@ def new(cls, typename, bases, ns):

`

1361

1361

`"follow default field(s) {default_names}"

`

1362

1362

` .format(field_name=field_name,

`

1363

1363

`default_names=', '.join(defaults_dict.keys())))

`

1364

``

`-

nm_tpl.new.annotations = collections.OrderedDict(types)

`

``

1364

`+

nm_tpl.new.annotations = dict(types)

`

1365

1365

`nm_tpl.new.defaults = tuple(defaults)

`

1366

1366

`nm_tpl._field_defaults = defaults_dict

`

1367

1367

`# update from user namespace without overriding special namedtuple attributes

`

`@@ -1386,12 +1386,10 @@ class Employee(NamedTuple):

`

1386

1386

``

1387

1387

` Employee = collections.namedtuple('Employee', ['name', 'id'])

`

1388

1388

``

1389

``

`-

The resulting class has extra annotations and _field_types

`

1390

``

`-

attributes, giving an ordered dict mapping field names to types.

`

1391

``

`-

annotations should be preferred, while _field_types

`

1392

``

`-

is kept to maintain pre PEP 526 compatibility. (The field names

`

1393

``

`-

are in the _fields attribute, which is part of the namedtuple

`

1394

``

`-

API.) Alternative equivalent keyword syntax is also accepted::

`

``

1389

`+

The resulting class has an extra annotations attribute, giving a

`

``

1390

`+

dict that maps field names to types. (The field names are also in

`

``

1391

`+

the _fields attribute, which is part of the namedtuple API.)

`

``

1392

`+

Alternative equivalent keyword syntax is also accepted::

`

1395

1393

``

1396

1394

` Employee = NamedTuple('Employee', name=str, id=int)

`

1397

1395

``