classification
Title: dataclasses.replace raises an exception if InitVar with default argument is not provided.
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: eric.smith Nosy List: Greg Kuhn, ZackerySpytz, anthrotype, eric.smith, levkivskyi, mierzej, miss-islington
Priority: normal Keywords: patch

Created on 2019-03-29 11:52 by Greg Kuhn, last changed 2021-04-05 20:12 by eric.smith. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 17441 closed Claudiu.Popa, 2019-12-02 20:17
PR 20867 merged ZackerySpytz, 2020-06-14 05:38
PR 25200 merged miss-islington, 2021-04-05 19:41
PR 25201 merged miss-islington, 2021-04-05 19:41
Messages (7)
msg339105 - (view) Author: Greg Kuhn (Greg Kuhn) Date: 2019-03-29 11:52
I have a snippet below which runs fine on python 3.7.0 but raises a ValueError exception on 3.7.1. I believe it's related to https://bugs.python.org/issue33805.

The error: c:\python\lib\dataclasses.py:1219: ValueError

The script:

from dataclasses import replace, dataclass, InitVar

@dataclass
class Test:
    a:int = 1
    b:InitVar[int] = None

    def __post_init__(self, b):
        if b is not None:
            self.a = b


if __name__ == '__main__':
    t = Test()
    t1 = Test(b=5)
    assert t1.a == 5

    t2 = replace(t1, **{})
    print(t2)
msg339124 - (view) Author: Greg Kuhn (Greg Kuhn) Date: 2019-03-29 16:12
Fixed title
msg385914 - (view) Author: Cosimo Lupo (anthrotype) Date: 2021-01-29 15:09
any updates on this? Would be great if any of the two candidate PRs was merged. It's basically impossible to use replace() with default InitVars..
Thank you in advace
msg390252 - (view) Author: miss-islington (miss-islington) Date: 2021-04-05 19:41
New changeset 75220674c07abfc90c2cd7862d04cfa2e2354450 by Zackery Spytz in branch 'master':
bpo-36470: Allow dataclasses.replace() to handle InitVars with default values (GH-20867)
https://github.com/python/cpython/commit/75220674c07abfc90c2cd7862d04cfa2e2354450
msg390256 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-04-05 20:07
New changeset bdee2a389e4b10e1c0ab65bbd4fd03defe7b2837 by Miss Islington (bot) in branch '3.8':
bpo-36470: Allow dataclasses.replace() to handle InitVars with default values (GH-20867) (GH-25201)
https://github.com/python/cpython/commit/bdee2a389e4b10e1c0ab65bbd4fd03defe7b2837
msg390257 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-04-05 20:07
New changeset 013c30e5fcee449cee63354d34585d6111782c82 by Miss Islington (bot) in branch '3.9':
bpo-36470: Allow dataclasses.replace() to handle InitVars with default values (GH-20867) (GH-25200)
https://github.com/python/cpython/commit/013c30e5fcee449cee63354d34585d6111782c82
msg390259 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2021-04-05 20:12
Thanks for the fix!
History
Date User Action Args
2021-04-05 20:12:35eric.smithsetstatus: open -> closed
resolution: fixed
messages: + msg390259

stage: patch review -> resolved
2021-04-05 20:07:42eric.smithsetmessages: + msg390257
2021-04-05 20:07:39eric.smithsetmessages: + msg390256
2021-04-05 19:41:23miss-islingtonsetpull_requests: + pull_request23940
2021-04-05 19:41:16miss-islingtonsetpull_requests: + pull_request23939
2021-04-05 19:41:09miss-islingtonsetnosy: + miss-islington
messages: + msg390252
2021-01-29 15:09:54anthrotypesetnosy: + anthrotype
messages: + msg385914
2020-06-14 05:38:50ZackerySpytzsetnosy: + ZackerySpytz
pull_requests: + pull_request20056
2019-12-02 20:17:37Claudiu.Popasetkeywords: + patch
stage: patch review
pull_requests: + pull_request16922
2019-10-01 20:40:45eric.smithsetassignee: eric.smith
2019-04-09 13:19:15mierzejsetnosy: + mierzej
2019-03-29 19:55:59levkivskyisetnosy: + levkivskyi
2019-03-29 16:12:06Greg Kuhnsetmessages: + msg339124
title: dataclasses replace raises an exception with an empty -> dataclasses.replace raises an exception if InitVar with default argument is not provided.
2019-03-29 11:57:41xtreaksetnosy: + eric.smith
2019-03-29 11:52:18Greg Kuhncreate