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

Skip to content

Commit 8fc5839

Browse files
bpo-38191: Turn warnings into errors in NamedTuple() and TypedDict(). (GH-16238)
1 parent 2bf31cc commit 8fc5839

3 files changed

Lines changed: 13 additions & 87 deletions

File tree

Lib/test/test_typing.py

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3582,16 +3582,10 @@ def test_namedtuple_errors(self):
35823582
NamedTuple('Emp', [('name', str)], None)
35833583
with self.assertRaises(ValueError):
35843584
NamedTuple('Emp', [('_name', str)])
3585-
3586-
with self.assertWarns(DeprecationWarning):
3587-
Emp = NamedTuple(typename='Emp', name=str, id=int)
3588-
self.assertEqual(Emp.__name__, 'Emp')
3589-
self.assertEqual(Emp._fields, ('name', 'id'))
3590-
3591-
with self.assertWarns(DeprecationWarning):
3592-
Emp = NamedTuple('Emp', fields=[('name', str), ('id', int)])
3593-
self.assertEqual(Emp.__name__, 'Emp')
3594-
self.assertEqual(Emp._fields, ('name', 'id'))
3585+
with self.assertRaises(TypeError):
3586+
NamedTuple(typename='Emp', name=str, id=int)
3587+
with self.assertRaises(TypeError):
3588+
NamedTuple('Emp', fields=[('name', str), ('id', int)])
35953589

35963590
def test_pickle(self):
35973591
global Emp # pickle wants to reference the class by name
@@ -3654,15 +3648,10 @@ def test_typeddict_create_errors(self):
36543648
with self.assertRaises(TypeError):
36553649
TypedDict('Emp', [('name', str)], None)
36563650

3657-
with self.assertWarns(DeprecationWarning):
3658-
Emp = TypedDict(_typename='Emp', name=str, id=int)
3659-
self.assertEqual(Emp.__name__, 'Emp')
3660-
self.assertEqual(Emp.__annotations__, {'name': str, 'id': int})
3661-
3662-
with self.assertWarns(DeprecationWarning):
3663-
Emp = TypedDict('Emp', _fields={'name': str, 'id': int})
3664-
self.assertEqual(Emp.__name__, 'Emp')
3665-
self.assertEqual(Emp.__annotations__, {'name': str, 'id': int})
3651+
with self.assertRaises(TypeError):
3652+
TypedDict(_typename='Emp', name=str, id=int)
3653+
with self.assertRaises(TypeError):
3654+
TypedDict('Emp', _fields={'name': str, 'id': int})
36663655

36673656
def test_typeddict_errors(self):
36683657
Emp = TypedDict('Emp', {'name': str, 'id': int})

Lib/typing.py

Lines changed: 4 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,81 +1653,20 @@ class Employee(NamedTuple):
16531653
"""
16541654
_root = True
16551655

1656-
def __new__(*args, **kwargs):
1657-
if not args:
1658-
raise TypeError('NamedTuple.__new__(): not enough arguments')
1659-
cls, *args = args # allow the "cls" keyword be passed
1660-
if args:
1661-
typename, *args = args # allow the "typename" keyword be passed
1662-
elif 'typename' in kwargs:
1663-
typename = kwargs.pop('typename')
1664-
import warnings
1665-
warnings.warn("Passing 'typename' as keyword argument is deprecated",
1666-
DeprecationWarning, stacklevel=2)
1667-
else:
1668-
raise TypeError("NamedTuple.__new__() missing 1 required positional "
1669-
"argument: 'typename'")
1670-
if args:
1671-
try:
1672-
fields, = args # allow the "fields" keyword be passed
1673-
except ValueError:
1674-
raise TypeError(f'NamedTuple.__new__() takes from 2 to 3 '
1675-
f'positional arguments but {len(args) + 2} '
1676-
f'were given') from None
1677-
elif 'fields' in kwargs and len(kwargs) == 1:
1678-
fields = kwargs.pop('fields')
1679-
import warnings
1680-
warnings.warn("Passing 'fields' as keyword argument is deprecated",
1681-
DeprecationWarning, stacklevel=2)
1682-
else:
1683-
fields = None
1684-
1656+
def __new__(cls, typename, fields=None, /, **kwargs):
16851657
if fields is None:
16861658
fields = kwargs.items()
16871659
elif kwargs:
16881660
raise TypeError("Either list of fields or keywords"
16891661
" can be provided to NamedTuple, not both")
16901662
return _make_nmtuple(typename, fields)
1691-
__new__.__text_signature__ = '($cls, typename, fields=None, /, **kwargs)'
16921663

16931664

1694-
def _dict_new(*args, **kwargs):
1695-
if not args:
1696-
raise TypeError('TypedDict.__new__(): not enough arguments')
1697-
cls, *args = args # allow the "cls" keyword be passed
1665+
def _dict_new(cls, /, *args, **kwargs):
16981666
return dict(*args, **kwargs)
1699-
_dict_new.__text_signature__ = '($cls, _typename, _fields=None, /, **kwargs)'
1700-
1701-
1702-
def _typeddict_new(*args, total=True, **kwargs):
1703-
if not args:
1704-
raise TypeError('TypedDict.__new__(): not enough arguments')
1705-
cls, *args = args # allow the "cls" keyword be passed
1706-
if args:
1707-
typename, *args = args # allow the "_typename" keyword be passed
1708-
elif '_typename' in kwargs:
1709-
typename = kwargs.pop('_typename')
1710-
import warnings
1711-
warnings.warn("Passing '_typename' as keyword argument is deprecated",
1712-
DeprecationWarning, stacklevel=2)
1713-
else:
1714-
raise TypeError("TypedDict.__new__() missing 1 required positional "
1715-
"argument: '_typename'")
1716-
if args:
1717-
try:
1718-
fields, = args # allow the "_fields" keyword be passed
1719-
except ValueError:
1720-
raise TypeError(f'TypedDict.__new__() takes from 2 to 3 '
1721-
f'positional arguments but {len(args) + 2} '
1722-
f'were given') from None
1723-
elif '_fields' in kwargs and len(kwargs) == 1:
1724-
fields = kwargs.pop('_fields')
1725-
import warnings
1726-
warnings.warn("Passing '_fields' as keyword argument is deprecated",
1727-
DeprecationWarning, stacklevel=2)
1728-
else:
1729-
fields = None
17301667

1668+
1669+
def _typeddict_new(cls, typename, fields=None, /, *, total=True, **kwargs):
17311670
if fields is None:
17321671
fields = kwargs
17331672
elif kwargs:
@@ -1742,7 +1681,6 @@ def _typeddict_new(*args, total=True, **kwargs):
17421681
pass
17431682

17441683
return _TypedDictMeta(typename, (), ns)
1745-
_typeddict_new.__text_signature__ = '($cls, _typename, _fields=None, /, *, total=True, **kwargs)'
17461684

17471685

17481686
def _check_fails(cls, other):
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
Constructors of :class:`~typing.NamedTuple` and :class:`~typing.TypedDict`
22
types now accept arbitrary keyword argument names, including "cls", "self",
3-
"typename", "_typename", "fields" and "_fields". Passing positional
4-
arguments by keyword is deprecated.
3+
"typename", "_typename", "fields" and "_fields".

0 commit comments

Comments
 (0)