Navigation Menu

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

dataclasses: make field() with no annotation an error #76686

Closed
ericvsmith opened this issue Jan 6, 2018 · 9 comments
Closed

dataclasses: make field() with no annotation an error #76686

ericvsmith opened this issue Jan 6, 2018 · 9 comments
Assignees
Labels
3.7 (EOL) end of life 3.8 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@ericvsmith
Copy link
Member

BPO 32505
Nosy @gvanrossum, @ericvsmith, @ilevkivskyi, @miss-islington
PRs
  • bpo-32505: dataclasses: raise TypeError if a member variable is of type Field, but doesn't have a type annotation. #6192
  • [3.7] bpo-32505: dataclasses: raise TypeError if a member variable is of type Field, but doesn't have a type annotation. (GH-6192) #6193
  • 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 2018-03-22.21:00:25.329>
    created_at = <Date 2018-01-06.22:21:04.892>
    labels = ['3.7', '3.8', 'type-bug', 'library']
    title = 'dataclasses: make field() with no annotation an error'
    updated_at = <Date 2018-03-22.21:00:25.328>
    user = 'https://github.com/ericvsmith'

    bugs.python.org fields:

    activity = <Date 2018-03-22.21:00:25.328>
    actor = 'eric.smith'
    assignee = 'eric.smith'
    closed = True
    closed_date = <Date 2018-03-22.21:00:25.329>
    closer = 'eric.smith'
    components = ['Library (Lib)']
    creation = <Date 2018-01-06.22:21:04.892>
    creator = 'eric.smith'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 32505
    keywords = ['patch']
    message_count = 9.0
    messages = ['309586', '314226', '314227', '314228', '314231', '314259', '314263', '314290', '314293']
    nosy_count = 4.0
    nosy_names = ['gvanrossum', 'eric.smith', 'levkivskyi', 'miss-islington']
    pr_nums = ['6192', '6193']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue32505'
    versions = ['Python 3.7', 'Python 3.8']

    @ericvsmith
    Copy link
    Member Author

    This is an attractive nuisance, especially when coming from attrs. Make it an error, since it's using field() with no annotation:

    @dataclass
    class C:
        x = field()

    @ericvsmith ericvsmith self-assigned this Jan 6, 2018
    @ericvsmith ericvsmith added type-bug An unexpected behavior, bug, or error 3.7 (EOL) end of life labels Jan 6, 2018
    @ericvsmith
    Copy link
    Member Author

    I'm not sure this is worth preventing. I can certainly make it an error, but since dataclasses ignores anything without a type annotation, I don't think it's a big deal. Basically I'd have to look through all class attributes that are of type Field but aren't in the class's __annotations__.

    I cannot find the original conversation that caused me to open this issue. If anyone feels strongly that this case should be an error, let me know.

    @gvanrossum
    Copy link
    Member

    Fine to close as wontfix.

    @ilevkivskyi
    Copy link
    Member

    Some part of the discussion is in https://bugs.python.org/issue32428

    I still think it is important to flag things like this as an error:

    @dataclass
    class C:
        x = field()

    This is a big bug magnet. Especially taking into account that attrs behave in the opposite way in this case.

    @ericvsmith
    Copy link
    Member Author

    But surely when you instantiate this:
    c = C(1)

    you'll notice that's an error?

    @ilevkivskyi
    Copy link
    Member

    you'll notice that's an error?

    Yes, but there are other scenarios, like using init=False or updating existing class definition and forgetting to update call sites (which will still work), etc.

    What would we lose by not flagging this as an error? I think there are no legitimate use cases for such code, and implementation is straightforward. If you don't have time I can do this myself.

    @ericvsmith
    Copy link
    Member Author

    Thanks for the offer. I've already got the code written, I just need to write some tests. I'll get it done real soon now.

    @ericvsmith ericvsmith added stdlib Python modules in the Lib dir 3.8 only security fixes labels Mar 22, 2018
    @ericvsmith
    Copy link
    Member Author

    New changeset 56970b8 by Eric V. Smith in branch 'master':
    bpo-32505: dataclasses: raise TypeError if a member variable is of type Field, but doesn't have a type annotation. (GH-6192)
    56970b8

    @miss-islington
    Copy link
    Contributor

    New changeset 3b4c6b1 by Miss Islington (bot) in branch '3.7':
    bpo-32505: dataclasses: raise TypeError if a member variable is of type Field, but doesn't have a type annotation. (GH-6192)
    3b4c6b1

    @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 stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants