Skip to content

Commit 6fed3c8

Browse files
bpo-40182: Remove the _field_types attribute of the NamedTuple class (GH-19368)
1 parent 1b21573 commit 6fed3c8

File tree

5 files changed

+16
-12
lines changed

5 files changed

+16
-12
lines changed

Doc/library/typing.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -959,14 +959,15 @@ The module defines the following classes, functions and decorators:
959959
.. versionchanged:: 3.6.1
960960
Added support for default values, methods, and docstrings.
961961

962-
.. versionchanged:: 3.8
963-
Deprecated the ``_field_types`` attribute in favor of the more
964-
standard ``__annotations__`` attribute which has the same information.
965-
966962
.. versionchanged:: 3.8
967963
The ``_field_types`` and ``__annotations__`` attributes are
968964
now regular dictionaries instead of instances of ``OrderedDict``.
969965

966+
.. versionchanged:: 3.9
967+
Removed the ``_field_types`` attribute in favor of the more
968+
standard ``__annotations__`` attribute which has the same information.
969+
970+
970971
.. class:: TypedDict(dict)
971972

972973
A simple typed namespace. At runtime it is equivalent to

Doc/whatsnew/3.9.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,11 @@ Removed
734734
defining ``COUNT_ALLOCS`` macro.
735735
(Contributed by Victor Stinner in :issue:`39489`.)
736736

737+
* The ``_field_types`` attribute of the :class:`typing.NamedTuple` class
738+
has been removed. It was deprecated deprecated since Python 3.8. Use
739+
the ``__annotations__`` attribute instead.
740+
(Contributed by Serhiy Storchaka in :issue:`40182`.)
741+
737742

738743
Porting to Python 3.9
739744
=====================

Lib/test/test_typing.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3561,7 +3561,6 @@ def test_basics(self):
35613561
self.assertEqual(Emp._fields, ('name', 'id'))
35623562
self.assertEqual(Emp.__annotations__,
35633563
collections.OrderedDict([('name', str), ('id', int)]))
3564-
self.assertIs(Emp._field_types, Emp.__annotations__)
35653564

35663565
def test_namedtuple_pyversion(self):
35673566
if sys.version_info[:2] < (3, 6):
@@ -3581,7 +3580,6 @@ def test_annotation_usage(self):
35813580
self.assertEqual(CoolEmployee._fields, ('name', 'cool'))
35823581
self.assertEqual(CoolEmployee.__annotations__,
35833582
collections.OrderedDict(name=str, cool=int))
3584-
self.assertIs(CoolEmployee._field_types, CoolEmployee.__annotations__)
35853583

35863584
def test_annotation_usage_with_default(self):
35873585
jelle = CoolEmployeeWithDefault('Jelle')
@@ -3594,7 +3592,8 @@ def test_annotation_usage_with_default(self):
35943592

35953593
self.assertEqual(CoolEmployeeWithDefault.__name__, 'CoolEmployeeWithDefault')
35963594
self.assertEqual(CoolEmployeeWithDefault._fields, ('name', 'cool'))
3597-
self.assertEqual(CoolEmployeeWithDefault._field_types, dict(name=str, cool=int))
3595+
self.assertEqual(CoolEmployeeWithDefault.__annotations__,
3596+
dict(name=str, cool=int))
35983597
self.assertEqual(CoolEmployeeWithDefault._field_defaults, dict(cool=0))
35993598

36003599
with self.assertRaises(TypeError):
@@ -3641,7 +3640,6 @@ def test_namedtuple_keyword_usage(self):
36413640
self.assertEqual(LocalEmployee.__name__, 'LocalEmployee')
36423641
self.assertEqual(LocalEmployee._fields, ('name', 'age'))
36433642
self.assertEqual(LocalEmployee.__annotations__, dict(name=str, age=int))
3644-
self.assertIs(LocalEmployee._field_types, LocalEmployee.__annotations__)
36453643
with self.assertRaises(TypeError):
36463644
NamedTuple('Name', [('x', int)], y=str)
36473645
with self.assertRaises(TypeError):

Lib/typing.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1705,9 +1705,7 @@ def _make_nmtuple(name, types):
17051705
msg = "NamedTuple('Name', [(f0, t0), (f1, t1), ...]); each t must be a type"
17061706
types = [(n, _type_check(t, msg)) for n, t in types]
17071707
nm_tpl = collections.namedtuple(name, [n for n, t in types])
1708-
# Prior to PEP 526, only _field_types attribute was assigned.
1709-
# Now __annotations__ are used and _field_types is deprecated (remove in 3.9)
1710-
nm_tpl.__annotations__ = nm_tpl._field_types = dict(types)
1708+
nm_tpl.__annotations__ = dict(types)
17111709
try:
17121710
nm_tpl.__module__ = sys._getframe(2).f_globals.get('__name__', '__main__')
17131711
except (AttributeError, ValueError):
@@ -1717,7 +1715,7 @@ def _make_nmtuple(name, types):
17171715

17181716
# attributes prohibited to set in NamedTuple class syntax
17191717
_prohibited = {'__new__', '__init__', '__slots__', '__getnewargs__',
1720-
'_fields', '_field_defaults', '_field_types',
1718+
'_fields', '_field_defaults',
17211719
'_make', '_replace', '_asdict', '_source'}
17221720

17231721
_special = {'__module__', '__name__', '__annotations__'}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Removed the ``_field_types`` attribute of the :class:`typing.NamedTuple`
2+
class.

0 commit comments

Comments
 (0)