Skip to content

bpo-38431: fix dataclasses.InitVar.__repr__#16700

Closed
samuelcolvin wants to merge 1 commit into
python:3.8from
samuelcolvin:InitVar-repr-fix
Closed

bpo-38431: fix dataclasses.InitVar.__repr__#16700
samuelcolvin wants to merge 1 commit into
python:3.8from
samuelcolvin:InitVar-repr-fix

Conversation

@samuelcolvin

@samuelcolvin samuelcolvin commented Oct 10, 2019

Copy link
Copy Markdown
Contributor

Fix repr method of InitVar to work with typing objects.
@samuelcolvin

Copy link
Copy Markdown
Contributor Author

Bit unclear on which branch this should be based off, should I rebase this off master and then create a backport PR?

@tirkarthi

Copy link
Copy Markdown
Member

Please raise the PR to master. The commit was done in master and then 3.8 branch was created. So it has to be fixed in master and will be backported to 3.8 branch.

@samuelcolvin

Copy link
Copy Markdown
Contributor Author

okay.

@samuelcolvin

Copy link
Copy Markdown
Contributor Author

based off wrong branch replaced by #16702

@samuelcolvin
samuelcolvin deleted the InitVar-repr-fix branch October 10, 2019 16:11

@serhiy-storchaka serhiy-storchaka left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you. Mostly LGTM, I left just few nitpicks.

Comment thread Lib/dataclasses.py
return f'dataclasses.InitVar[{self.type.__name__}]'
try:
type_name = self.type.__name__
except AttributeError:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Functions have the __name__ attribute. I suggest to use isinstance(self.type, type).

self.assertEqual(C(10).x, 10)

def test_initvar_repr(self):
self.assertEqual(repr(InitVar[str]), 'dataclasses.InitVar[str]')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There is already a test for repr() in test_init_var_preserve_type. I suggest to remove it from that test.


def test_initvar_repr(self):
self.assertEqual(repr(InitVar[str]), 'dataclasses.InitVar[str]')
self.assertEqual(repr(InitVar[Optional[str]]),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am not sure that InitVar[Optional[...]] makes sense. The example in the documentation does not use Optional, but the default value is None. I suggest to use different example, e.g. List[int]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think optional does make sense, but I can change it if you like.

@@ -0,0 +1,2 @@
Fix ``__repr__`` method for ``dataclasses.InitVar`` to work with typing
objects.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please add "Patch by ...".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I assume these comments also apply to #16702

I'm not sure what you mean here, could you elaborate?

@tirkarthi tirkarthi Oct 10, 2019

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Adding "patch by " at end of file for credit since this is added to changelog.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry, it's "patch by full name." as additional sentence for credit. GitHub removed my content with tags <>

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

wait, you mean patched by: <Samuel Colvin>?

Or perhaps you're confused since you're commenting on the wrong PR? should be #16702

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm just a bit confused since that format isn't used in any other news entries as far as I can tell.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I answered here since it had the question initially :) The news entry would be as below for other PR. Sorry for the confusion due to markdown rendering.

Fix ``__repr__`` method for ``dataclasses.InitVar`` to work with typing
objects. Patch by Samuel Colvin.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

okay, that's pretty much what I have in the other PR. Is there still something to change?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nothing else, I added the verbatim version since patch by would be a separate sentence and not in past tense as above for most cases. But I will leave it to the core dev who is merging to take care of wording and not pedantic about it. Thanks for the changes.

https://devguide.python.org/committing/#handling-others-code and has an example of the format widely used.

Third, ensure the patch is attributed correctly with the contributor’s name in 
Misc/ACKS if they aren’t already there (and didn’t add themselves in their patch) 
and by mentioning “Patch by <x>” in the Misc/NEWS.d entry and the check-in 
message. If the patch has been heavily modified then “Initial patch by <x>” is 
an appropriate alternate wording. GitHub now supports multiple authors in a commit. 
Add Co-authored-by: name <name@example.com> at the end of the commit message.

@samuelcolvin

Copy link
Copy Markdown
Contributor Author

@serhiy-storchaka I've updated #16702 (the new PR) with your comments except on the news entry as I didn't understand what you were suggesting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants