You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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=<Date2018-06-23.15:28:07.455>created_at=<Date2018-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=<Date2018-06-23.15:28:07.454>user='https://github.com/ericvsmith'
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
... classC:
... 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".
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: