Skip to content
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

Closed
GlennGribble mannequin opened this issue Sep 16, 2019 · 9 comments
Closed

typing.NamedTuple() should prefix parameters with '_' #82372

GlennGribble mannequin opened this issue Sep 16, 2019 · 9 comments
Assignees
Labels
3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@GlennGribble
Copy link
Mannequin

GlennGribble mannequin commented Sep 16, 2019

BPO 38191
Nosy @gvanrossum, @serhiy-storchaka, @ilevkivskyi, @miss-islington, @Glenn Gribble
PRs
  • bpo-38191: Accept arbitrary keyword names in NamedTuple() and TypedDict(). #16222
  • [3.8] bpo-38191: Accept arbitrary keyword names in NamedTuple() and TypedDict(). (GH-16222) #16237
  • bpo-38191: Turn warnings into errors in NamedTuple() and TypedDict(). #16238
  • [3.7] bpo-38191: Accept arbitrary keyword names in NamedTuple(). (GH-16222) #16239
  • [3.8] bpo-38191: Use positional-only parameters in TypedDict(). #16240
  • 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:

    assignee = 'https://github.com/serhiy-storchaka'
    closed_at = <Date 2019-09-18.06:08:31.725>
    created_at = <Date 2019-09-16.19:59:55.784>
    labels = ['3.7', '3.8', 'type-feature', 'library', '3.9']
    title = "typing.NamedTuple() should prefix parameters with '_'"
    updated_at = <Date 2019-09-18.06:08:31.725>
    user = 'https://github.com/GlennGribble'

    bugs.python.org fields:

    activity = <Date 2019-09-18.06:08:31.725>
    actor = 'serhiy.storchaka'
    assignee = 'serhiy.storchaka'
    closed = True
    closed_date = <Date 2019-09-18.06:08:31.725>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2019-09-16.19:59:55.784>
    creator = 'gribbg'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38191
    keywords = ['patch']
    message_count = 9.0
    messages = ['352579', '352620', '352622', '352654', '352655', '352656', '352657', '352659', '352697']
    nosy_count = 5.0
    nosy_names = ['gvanrossum', 'serhiy.storchaka', 'levkivskyi', 'miss-islington', 'gribbg']
    pr_nums = ['16222', '16237', '16238', '16239', '16240']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue38191'
    versions = ['Python 3.7', 'Python 3.8', 'Python 3.9']

    @GlennGribble
    Copy link
    Mannequin Author

    GlennGribble mannequin commented Sep 16, 2019

    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.

    @GlennGribble GlennGribble mannequin added 3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Sep 16, 2019
    @serhiy-storchaka
    Copy link
    Member

    Looks as a good case for positional-only parameters.

    But first passing these arguments by keyword should be deprecated.

    @serhiy-storchaka serhiy-storchaka self-assigned this Sep 17, 2019
    @serhiy-storchaka
    Copy link
    Member

    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).

    @serhiy-storchaka
    Copy link
    Member

    New changeset 2bf31cc by Serhiy Storchaka in branch 'master':
    bpo-38191: Accept arbitrary keyword names in NamedTuple() and TypedDict(). (GH-16222)
    2bf31cc

    @miss-islington
    Copy link
    Contributor

    New changeset 54ba5f1 by Miss Islington (bot) in branch '3.8':
    bpo-38191: Accept arbitrary keyword names in NamedTuple() and TypedDict(). (GH-16222)
    54ba5f1

    @serhiy-storchaka
    Copy link
    Member

    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().

    @serhiy-storchaka
    Copy link
    Member

    New changeset 69b3718 by Serhiy Storchaka in branch '3.7':
    [3.7] bpo-38191: Accept arbitrary keyword names in NamedTuple(). (GH-16222) (GH-16239)
    69b3718

    @serhiy-storchaka
    Copy link
    Member

    New changeset 8fc5839 by Serhiy Storchaka in branch 'master':
    bpo-38191: Turn warnings into errors in NamedTuple() and TypedDict(). (GH-16238)
    8fc5839

    @serhiy-storchaka
    Copy link
    Member

    New changeset 2adcd79 by Serhiy Storchaka in branch '3.8':
    bpo-38191: Use positional-only parameters in TypedDict(). (GH-16240)
    2adcd79

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes 3.9 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants