Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,12 @@ def __init__(self, type):
self.type = type

def __repr__(self):
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).

# typing objects, e.g. Union
type_name = repr(self.type)
return f'dataclasses.InitVar[{type_name}]'


# Instances of Field are only ever created from within this module,
Expand Down
5 changes: 5 additions & 0 deletions Lib/test/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -2883,6 +2883,11 @@ class C:
# x is not an InitVar, so there will be a member x.
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.

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.

'dataclasses.InitVar[typing.Union[str, NoneType]]')

def test_classvar_module_level_import(self):
from test import dataclass_module_1
from test import dataclass_module_1_str
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ Benjamin Collar
Jeffery Collins
Robert Collins
Paul Colomiets
Samuel Colvin
Christophe Combelles
Geremy Condra
Denver Coneybeare
Expand Down
Original file line number Diff line number Diff line change
@@ -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.