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

Add keyword-only fields to dataclasses #87698

Closed
ericvsmith opened this issue Mar 17, 2021 · 11 comments
Closed

Add keyword-only fields to dataclasses #87698

ericvsmith opened this issue Mar 17, 2021 · 11 comments
Assignees
Labels
3.10 only security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@ericvsmith
Copy link
Member

BPO 43532
Nosy @rhettinger, @ericvsmith, @ryanhiebert, @henryiii, @miss-islington, @tirkarthi, @EpicWink, @jack1142, @kamilturek, @cdce8p, @finite-state-machine
PRs
  • bpo-43532: Add kw_only to dataclass() and field(), and add KW_ONLY marker. #24909
  • bpo-43532: Add keyword-only fields to dataclasses. #25608
  • bpo-43532: add version added to KW_ONLY #31235
  • [3.10] bpo-43532: add version added to KW_ONLY (GH-31235) #31236
  • 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/ericvsmith'
    closed_at = <Date 2022-02-09.21:49:39.659>
    created_at = <Date 2021-03-17.17:39:21.912>
    labels = ['type-feature', 'library', '3.10']
    title = 'Add keyword-only fields to dataclasses'
    updated_at = <Date 2022-02-09.21:49:39.658>
    user = 'https://github.com/ericvsmith'

    bugs.python.org fields:

    activity = <Date 2022-02-09.21:49:39.658>
    actor = 'eric.smith'
    assignee = 'eric.smith'
    closed = True
    closed_date = <Date 2022-02-09.21:49:39.659>
    closer = 'eric.smith'
    components = ['Library (Lib)']
    creation = <Date 2021-03-17.17:39:21.912>
    creator = 'eric.smith'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 43532
    keywords = ['patch']
    message_count = 11.0
    messages = ['388949', '388951', '388984', '391920', '392018', '392115', '398856', '404609', '412943', '412944', '412946']
    nosy_count = 11.0
    nosy_names = ['rhettinger', 'eric.smith', 'ryanhiebert', 'Henry Schreiner', 'miss-islington', 'xtreak', 'Epic_Wink', 'jack1142', 'kamilturek', 'cdce8p', 'finite-state-machine']
    pr_nums = ['24909', '25608', '31235', '31236']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue43532'
    versions = ['Python 3.10']

    @ericvsmith
    Copy link
    Member Author

    The idea is that a keyword-only field becomes a keyword-only argument to __init__().

    For the proposal and a discussion, see https://mail.python.org/archives/list/python-ideas@python.org/message/FI6KS4O67XDEIDYOFWCXMDLDOSCNSEYG/

    The @DataClass decorator will get a new parameter, kw_only, which defaults to False. If kw_only=True, all fields in the dataclass will be by efault keyword-only. In addition, field() will have a new kw_only parameter. If true, the field will be keyword-only. If false, it will not be keyword-only. If unspecified, it will use the value of dataclass's kw_only parameter.

    In addition, a module-level variable KW_ONLY will be added. If a field has this type, then all fields after it will default to kw_only=True. The field is otherwise completely ignored.

    Examples:

    @dataclasses.dataclass
    class A:
        a: Any = field(kw_only=True) 

    Will have __init__(self, *, a)

    @dataclasses.dataclass(kw_only=True)
    class B:
        a: Any
        b: Any 

    Will have __init__(self, *, a, b)

    @dataclasses.dataclass
    class C:
        a: Any
        _: dataclasses.KW_ONLY
        b: Any
        c: Any

    Will have __init__(self, a, *, b, c)

    If any non-keyword-only parameters are present, they will be moved before all keyword-only parameters, only for the generated __init__. All other generated methods (repr, __lt__, etc.) will keep fields in the declared order, which is the case in versions 3.9 and earlier.

    @dataclasses.dataclass
    class D:
        a: Any
        b: Any = field(kw_only=True)
        c: Any

    Will have __init__(self, a, c, *, b)

    PR to follow.

    @ericvsmith ericvsmith added the 3.10 only security fixes label Mar 17, 2021
    @ericvsmith ericvsmith self-assigned this Mar 17, 2021
    @ericvsmith ericvsmith added stdlib Python modules in the Lib dir type-feature A feature request or enhancement 3.10 only security fixes labels Mar 17, 2021
    @ericvsmith ericvsmith self-assigned this Mar 17, 2021
    @ericvsmith ericvsmith added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Mar 17, 2021
    @tirkarthi
    Copy link
    Member

    This looks like a duplicate of https://bugs.python.org/issue33129 or that ticket can be closed.

    @rhettinger
    Copy link
    Contributor

    +1 for this idea. I don't see any downside.

    @ericvsmith
    Copy link
    Member Author

    I've checked in the code, tests, and some basic documentation. I didn't finish the documentation in order to make sure I could get this in before beta 1. I'm going to leave this issue open until I have time for the rest of the documentation.

    @EpicWink
    Copy link
    Mannequin

    EpicWink mannequin commented Apr 27, 2021

    Will this close https://bugs.python.org/issue36077 ?

    @ericvsmith
    Copy link
    Member Author

    Will this close https://bugs.python.org/issue36077 ?

    No, not directly, although I'm going to close that issue as "won't fix".

    But you can specify that the child class requires all params as kw_only. Continuing the example:

    >>> @dataclass(kw_only=True)
    ... class Child(Parent):
    ...   y: int
    ...
    >>> Child(0, y=1)
    Child(x=0, y=1)

    @jack1142
    Copy link
    Mannequin

    jack1142 mannequin commented Aug 4, 2021

    Does this change deserve an entry in the What's New in Python document in addition to the changelog entry?

    @finite-state-machine
    Copy link
    Mannequin

    finite-state-machine mannequin commented Oct 21, 2021

    I doubt it's worth opening a separate issue for this, so I'll mention it here:

    In the documentation (https://docs.python.org/3.10/library/dataclasses.html#dataclasses.KW_ONLY) nothing documents KW_ONLY as being new in Python 3.10.

    @miss-islington
    Copy link
    Contributor

    New changeset 5a3f972 by Henry Schreiner in branch 'main':
    bpo-43532: add version added to KW_ONLY (GH-31235)
    5a3f972

    @miss-islington
    Copy link
    Contributor

    New changeset 7445949 by Miss Islington (bot) in branch '3.10':
    bpo-43532: add version added to KW_ONLY (GH-31235)
    7445949

    @ericvsmith
    Copy link
    Member Author

    Thanks, Henry Schreiner!

    @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.10 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

    4 participants