Kaydet (Commit) f7b57df0 authored tarafından Raymond Hettinger's avatar Raymond Hettinger Kaydeden (comit) Miss Islington (bot)

bpo-36320: Switch typing.NamedTuple from OrderedDict to regular dict (GH-12396)



Also,  deprecate the *_field_types* attributes which duplicated the information in *\__annotations__*.


https://bugs.python.org/issue36320
üst 1be0d113
...@@ -838,10 +838,11 @@ The module defines the following classes, functions and decorators: ...@@ -838,10 +838,11 @@ The module defines the following classes, functions and decorators:
Fields with a default value must come after any fields without a default. Fields with a default value must come after any fields without a default.
The resulting class has two extra attributes: ``_field_types``, The resulting class has an extra attribute ``__annotations__`` giving a
giving a dict mapping field names to types, and ``_field_defaults``, a dict dict that maps the field names to the field types. (The field names are in
mapping field names to default values. (The field names are in the the ``_fields`` attribute and the default values are in the
``_fields`` attribute, which is part of the namedtuple API.) ``_field_defaults`` attribute both of which are part of the namedtuple
API.)
``NamedTuple`` subclasses can also have docstrings and methods:: ``NamedTuple`` subclasses can also have docstrings and methods::
...@@ -863,6 +864,15 @@ The module defines the following classes, functions and decorators: ...@@ -863,6 +864,15 @@ The module defines the following classes, functions and decorators:
.. versionchanged:: 3.6.1 .. versionchanged:: 3.6.1
Added support for default values, methods, and docstrings. Added support for default values, methods, and docstrings.
.. versionchanged:: 3.8
Deprecated the ``_field_types`` attribute in favor of the more
standard ``__annotations__`` attribute which has the same information.
.. versionchanged:: 3.8
The ``_field_types`` and ``__annotations__`` attributes are
now regular dictionaries instead of instances of ``OrderedDict``.
.. function:: NewType(typ) .. function:: NewType(typ)
A helper function to indicate a distinct types to a typechecker, A helper function to indicate a distinct types to a typechecker,
......
...@@ -510,6 +510,10 @@ Deprecated ...@@ -510,6 +510,10 @@ Deprecated
(Contributed by Berker Peksag in :issue:`9372`.) (Contributed by Berker Peksag in :issue:`9372`.)
* The :class:`typing.NamedTuple` class has deprecated the ``_field_types``
attribute in favor of the ``__annotations__`` attribute which has the same
information. (Contributed by Raymond Hettinger in :issue:`36320`.)
* :mod:`ast` classes ``Num``, ``Str``, ``Bytes``, ``NameConstant`` and * :mod:`ast` classes ``Num``, ``Str``, ``Bytes``, ``NameConstant`` and
``Ellipsis`` are considered deprecated and will be removed in future Python ``Ellipsis`` are considered deprecated and will be removed in future Python
versions. :class:`~ast.Constant` should be used instead. versions. :class:`~ast.Constant` should be used instead.
......
...@@ -1325,8 +1325,8 @@ def _make_nmtuple(name, types): ...@@ -1325,8 +1325,8 @@ def _make_nmtuple(name, types):
types = [(n, _type_check(t, msg)) for n, t in types] types = [(n, _type_check(t, msg)) for n, t in types]
nm_tpl = collections.namedtuple(name, [n for n, t in types]) nm_tpl = collections.namedtuple(name, [n for n, t in types])
# Prior to PEP 526, only _field_types attribute was assigned. # Prior to PEP 526, only _field_types attribute was assigned.
# Now, both __annotations__ and _field_types are used to maintain compatibility. # Now __annotations__ are used and _field_types is deprecated (remove in 3.9)
nm_tpl.__annotations__ = nm_tpl._field_types = collections.OrderedDict(types) nm_tpl.__annotations__ = nm_tpl._field_types = dict(types)
try: try:
nm_tpl.__module__ = sys._getframe(2).f_globals.get('__name__', '__main__') nm_tpl.__module__ = sys._getframe(2).f_globals.get('__name__', '__main__')
except (AttributeError, ValueError): except (AttributeError, ValueError):
...@@ -1361,7 +1361,7 @@ class NamedTupleMeta(type): ...@@ -1361,7 +1361,7 @@ class NamedTupleMeta(type):
"follow default field(s) {default_names}" "follow default field(s) {default_names}"
.format(field_name=field_name, .format(field_name=field_name,
default_names=', '.join(defaults_dict.keys()))) default_names=', '.join(defaults_dict.keys())))
nm_tpl.__new__.__annotations__ = collections.OrderedDict(types) nm_tpl.__new__.__annotations__ = dict(types)
nm_tpl.__new__.__defaults__ = tuple(defaults) nm_tpl.__new__.__defaults__ = tuple(defaults)
nm_tpl._field_defaults = defaults_dict nm_tpl._field_defaults = defaults_dict
# update from user namespace without overriding special namedtuple attributes # update from user namespace without overriding special namedtuple attributes
...@@ -1386,12 +1386,10 @@ class NamedTuple(metaclass=NamedTupleMeta): ...@@ -1386,12 +1386,10 @@ class NamedTuple(metaclass=NamedTupleMeta):
Employee = collections.namedtuple('Employee', ['name', 'id']) Employee = collections.namedtuple('Employee', ['name', 'id'])
The resulting class has extra __annotations__ and _field_types The resulting class has an extra __annotations__ attribute, giving a
attributes, giving an ordered dict mapping field names to types. dict that maps field names to types. (The field names are also in
__annotations__ should be preferred, while _field_types the _fields attribute, which is part of the namedtuple API.)
is kept to maintain pre PEP 526 compatibility. (The field names Alternative equivalent keyword syntax is also accepted::
are in the _fields attribute, which is part of the namedtuple
API.) Alternative equivalent keyword syntax is also accepted::
Employee = NamedTuple('Employee', name=str, id=int) Employee = NamedTuple('Employee', name=str, id=int)
......
The typing.NamedTuple() class has deprecated the _field_types attribute in
favor of the __annotations__ attribute which carried the same information.
Also, both attributes were converted from OrderedDict to a regular 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