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

bpo-33805: Improve error message of dataclasses.replace() #7580

Merged
merged 1 commit into from
Jun 23, 2018

Conversation

corona10
Copy link
Member

@corona10 corona10 commented Jun 10, 2018

@corona10
Copy link
Member Author

@ericvsmith PTAL

Copy link
Member

@ericvsmith ericvsmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. See comments.

_FIELD_CLASSVAR: 'ClassVar',
_FIELD_INITVAR: 'InitVar'
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's worth adding this mapping and the new __str__ just for this one error message.

@@ -1173,7 +1182,11 @@ class C:
continue

if f.name not in changes:
changes[f.name] = getattr(obj, f.name)
if f._field_type is _FIELD_INITVAR:
raise ValueError(f"{f._field_type} '{f.name}' "
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For consistency with the rest of the code, please change '{f.name}' to {f.name!r}. Also, just hard-code InitVar in the message, since this message is only applicable with InitVars.

if f._field_type is _FIELD_INITVAR:
raise ValueError(f"{f._field_type} '{f.name}' "
'must be specified with replace()')
else:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The else is not needed.

c = C(1, 10)
with self.assertRaisesRegex(ValueError, r"InitVar 'y' must be "
"specified with replace()"):
replace(c, i=3)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test that shows that an InitVar does work with replace(), and check that its value is used (probably via a __post_init__ method). Maybe something like:

def __post_init__(self, y):
   self.x *= y

or similar.

@@ -0,0 +1 @@
Update error message of dataclasses.replace() when InitVar is not specified.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer "Improve error message of dataclasses.replace() when an InitVar is not specified".

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@corona10 corona10 changed the title bpo-33805: Update error message of dataclasses.replace() bpo-33805: Improve error message of dataclasses.replace() Jun 10, 2018
@corona10
Copy link
Member Author

@ericvsmith
I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@ericvsmith: please review the changes made to this pull request.

@ericvsmith
Copy link
Member

Thanks! Looks good.

I'm going to wait a few days before I merge this, because at this late date I don't want to put this in to 3.7.0, I'll wait for 3.7.1.

@ericvsmith ericvsmith merged commit 3d70f7a into python:master Jun 23, 2018
@miss-islington
Copy link
Contributor

Thanks @corona10 for the PR, and @ericvsmith for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒⛏🤖

@bedevere-bot
Copy link

GH-7876 is a backport of this pull request to the 3.7 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jun 23, 2018
)

(cherry picked from commit 3d70f7a)

Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
miss-islington added a commit that referenced this pull request Jun 23, 2018
(cherry picked from commit 3d70f7a)

Co-authored-by: Dong-hee Na <donghee.na92@gmail.com>
@corona10 corona10 deleted the bpo-33805 branch June 25, 2018 04:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants