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: replace() give poor error message if using InitVar #77986

Closed
ericvsmith opened this issue Jun 8, 2018 · 4 comments
Closed

dataclasses: replace() give poor error message if using InitVar #77986

ericvsmith opened this issue Jun 8, 2018 · 4 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 33805
Nosy @ericvsmith, @corona10, @miss-islington
PRs
  • bpo-33805: Improve error message of dataclasses.replace() #7580
  • [3.7] bpo-33805: Improve error message of dataclasses.replace() (GH-7580) #7876
  • 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-06-23.15:28:07.455>
    created_at = <Date 2018-06-08.08:51:39.491>
    labels = ['3.7', '3.8', 'type-bug', 'library']
    title = 'dataclasses: replace() give poor error message if using InitVar'
    updated_at = <Date 2018-06-23.15:28:07.454>
    user = 'https://github.com/ericvsmith'

    bugs.python.org fields:

    activity = <Date 2018-06-23.15:28:07.454>
    actor = 'eric.smith'
    assignee = 'eric.smith'
    closed = True
    closed_date = <Date 2018-06-23.15:28:07.455>
    closer = 'eric.smith'
    components = ['Library (Lib)']
    creation = <Date 2018-06-08.08:51:39.491>
    creator = 'eric.smith'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 33805
    keywords = ['patch']
    message_count = 4.0
    messages = ['319036', '319198', '320309', '320311']
    nosy_count = 3.0
    nosy_names = ['eric.smith', 'corona10', 'miss-islington']
    pr_nums = ['7580', '7876']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue33805'
    versions = ['Python 3.7', 'Python 3.8']

    @ericvsmith
    Copy link
    Member Author

    If a dataclass contains an InitVar without a default value, that InitVar must be specified in the call to replace(). This is because replace() works by first creating a new object, and InitVars without defaults, by definition, must be specified when creating the object. There is no other source for the value of the InitVar to use.

    However, the exception you get is confusing:

    >>> from dataclasses import *
    >>> @dataclass
    ... class C:
    ...   i: int
    ...   j: InitVar[int]
    ...
    >>> c = C(1, 2)
    >>> replace(c, i=3)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "C:\home\eric\local\python\cpython\lib\dataclasses.py", line 1176, in replace
        changes[f.name] = getattr(obj, f.name)
    AttributeError: 'C' object has no attribute 'j'
    >>>

    This message really should say something like "InitVar 'j' must be specified".

    @ericvsmith ericvsmith added 3.7 (EOL) end of life 3.8 only security fixes labels Jun 8, 2018
    @ericvsmith ericvsmith self-assigned this Jun 8, 2018
    @ericvsmith ericvsmith added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jun 8, 2018
    @corona10
    Copy link
    Member

    @eric.smith
    Can I take a look this issue?

    @ericvsmith
    Copy link
    Member Author

    New changeset 3d70f7a by Eric V. Smith (Dong-hee Na) in branch 'master':
    bpo-33805: Improve error message of dataclasses.replace() (GH-7580)
    3d70f7a

    @miss-islington
    Copy link
    Contributor

    New changeset bbef7ab by Miss Islington (bot) in branch '3.7':
    bpo-33805: Improve error message of dataclasses.replace() (GH-7580)
    bbef7ab

    @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

    3 participants