This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: dataclasses: replace() give poor error message if using InitVar
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: corona10, eric.smith, miss-islington
Priority: normal Keywords: patch

Created on 2018-06-08 08:51 by eric.smith, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 7580 merged corona10, 2018-06-10 06:15
PR 7876 merged miss-islington, 2018-06-23 14:47
Messages (4)
msg319036 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-06-08 08:51
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".
msg319198 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2018-06-10 05:23
@eric.smith
Can I take a look this issue?
msg320309 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-06-23 14:46
New changeset 3d70f7aef614c396f516b5fccedeebe98598714d by Eric V. Smith (Dong-hee Na) in branch 'master':
bpo-33805: Improve error message of dataclasses.replace() (GH-7580)
https://github.com/python/cpython/commit/3d70f7aef614c396f516b5fccedeebe98598714d
msg320311 - (view) Author: miss-islington (miss-islington) Date: 2018-06-23 15:04
New changeset bbef7abe922edadc7a1679c19d6053240bf600d5 by Miss Islington (bot) in branch '3.7':
bpo-33805: Improve error message of dataclasses.replace() (GH-7580)
https://github.com/python/cpython/commit/bbef7abe922edadc7a1679c19d6053240bf600d5
History
Date User Action Args
2022-04-11 14:59:01adminsetgithub: 77986
2018-06-23 15:28:07eric.smithsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-06-23 15:04:04miss-islingtonsetnosy: + miss-islington
messages: + msg320311
2018-06-23 14:47:52miss-islingtonsetpull_requests: + pull_request7483
2018-06-23 14:46:36eric.smithsetmessages: + msg320309
2018-06-10 06:15:29corona10setkeywords: + patch
stage: patch review
pull_requests: + pull_request7203
2018-06-10 05:23:38corona10setnosy: + corona10
messages: + msg319198
2018-06-08 08:51:39eric.smithcreate