This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: typing.NamedTuple() should prefix parameters with '_'
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.9, Python 3.8, Python 3.7
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: gribbg, gvanrossum, levkivskyi, miss-islington, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2019-09-16 19:59 by gribbg, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 16222 merged serhiy.storchaka, 2019-09-17 09:31
PR 16237 merged miss-islington, 2019-09-17 18:22
PR 16238 merged serhiy.storchaka, 2019-09-17 18:33
PR 16239 merged serhiy.storchaka, 2019-09-17 18:42
PR 16240 merged serhiy.storchaka, 2019-09-17 18:51
Messages (9)
msg352579 - (view) Author: Glenn Gribble (gribbg) Date: 2019-09-16 19:59
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\", line 1411, in __new__
    return _make_nmtuple(typename, fields)
  File "C:\Program Files\Python37\lib\", 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.
msg352620 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-09-17 08:11
Looks as a good case for positional-only parameters.

But first passing these arguments by keyword should be deprecated.
msg352622 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-09-17 09:36
PR 16222 adds support for arbitrary keyword argument names in NamedTuple and TypedDict. Passing arguments like "typename", "_typename", "fields" and "_fields" by keyword is still supported, but deprecated. I'm going to backport this to 3.8 and 3.7 (to 3.7 without deprecation warnings), and than convert warnings to error in master (by using positional-only parameters syntax).
msg352654 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-09-17 18:22
New changeset 2bf31ccab3d17f3f35b42dca97f99576dfe2fc7d by Serhiy Storchaka in branch 'master':
bpo-38191: Accept arbitrary keyword names in NamedTuple() and TypedDict(). (GH-16222)
msg352655 - (view) Author: miss-islington (miss-islington) Date: 2019-09-17 18:41
New changeset 54ba5f19d4a654768c785468d736ed0bd05947f5 by Miss Islington (bot) in branch '3.8':
bpo-38191: Accept arbitrary keyword names in NamedTuple() and TypedDict(). (GH-16222)
msg352656 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-09-17 18:53
Oh, when backported to 3.7 I have found that TypedDict is new in 3.8. So I think it is better to get rid of deprecations in TypedDict().
msg352657 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-09-17 19:09
New changeset 69b3718b183a698202bd67488639bffd64a488bc by Serhiy Storchaka in branch '3.7':
[3.7] bpo-38191: Accept arbitrary keyword names in NamedTuple(). (GH-16222) (GH-16239)
msg352659 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-09-17 19:41
New changeset 8fc5839a9def34c13b6025c291434ba5fb5d6442 by Serhiy Storchaka in branch 'master':
bpo-38191: Turn warnings into errors in NamedTuple() and TypedDict(). (GH-16238)
msg352697 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-09-18 06:08
New changeset 2adcd79bf90415147691a51a4cb046d5920f6ce7 by Serhiy Storchaka in branch '3.8':
bpo-38191: Use positional-only parameters in TypedDict(). (GH-16240)
Date User Action Args
2022-04-11 14:59:20adminsetgithub: 82372
2019-09-18 06:08:31serhiy.storchakasetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-09-18 06:08:04serhiy.storchakasetmessages: + msg352697
2019-09-17 19:41:59serhiy.storchakasetmessages: + msg352659
2019-09-17 19:09:15serhiy.storchakasetmessages: + msg352657
2019-09-17 18:53:53serhiy.storchakasetmessages: + msg352656
2019-09-17 18:51:36serhiy.storchakasetpull_requests: + pull_request15837
2019-09-17 18:42:23serhiy.storchakasetpull_requests: + pull_request15836
2019-09-17 18:41:58miss-islingtonsetnosy: + miss-islington
messages: + msg352655
2019-09-17 18:33:28serhiy.storchakasetpull_requests: + pull_request15835
2019-09-17 18:22:13miss-islingtonsetpull_requests: + pull_request15834
2019-09-17 18:22:04serhiy.storchakasetmessages: + msg352654
2019-09-17 09:36:22serhiy.storchakasetmessages: + msg352622
2019-09-17 09:31:48serhiy.storchakasetkeywords: + patch
stage: patch review
pull_requests: + pull_request15822
2019-09-17 08:11:18serhiy.storchakasetassignee: serhiy.storchaka

messages: + msg352620
nosy: + serhiy.storchaka, levkivskyi, gvanrossum
2019-09-16 19:59:55gribbgcreate