-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
typing.NamedTuple() should prefix parameters with '_' #82372
Comments
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. |
Looks as a good case for positional-only parameters. But first passing these arguments by keyword should be deprecated. |
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). |
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(). |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: