[New-bugs-announce] [issue38191] typing.NamedTuple() should prefix parameters with '_'

Glenn Gribble report at bugs.python.org
Mon Sep 16 15:59:55 EDT 2019

New submission from Glenn Gribble <dev at gribbelus.com>:

At present, it is not possible to use the shorthand notation to define a NamedTuple with typename or fields.  I.e., NamedTuple('MyTuple', typename=str, fields=int) does not work.  Changing the parameter names to _typename and _fields would allow any non-private, legal identifier to be used in the shorthand notation.
>>> import typing
>>> typing.NamedTuple('Example', fieldz=int)
<class '__main__.Example'>
>>> typing.NamedTuple('Example2', fields=int)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Program Files\Python37\lib\typing.py", line 1411, in __new__
    return _make_nmtuple(typename, fields)
  File "C:\Program Files\Python37\lib\typing.py", line 1326, in _make_nmtuple
    types = [(n, _type_check(t, msg)) for n, t in types]
TypeError: 'type' object is not iterable

Of course, it is fairly easy to work around the issue by using fields parameter:

>>> typing.NamedTuple('Example3', [('fields', int)])
<class '__main__.Example3'>

There would be backwards compatibility issues with any code using named arguments for fields or typename.

components: Library (Lib)
messages: 352579
nosy: gribbg
priority: normal
severity: normal
status: open
title: typing.NamedTuple() should prefix parameters with '_'
type: enhancement
versions: Python 3.7, Python 3.8, Python 3.9

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list