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

Don't hard-code class names in __repr__ implementations #1335

Merged
merged 8 commits into from
Nov 4, 2020

Conversation

aaronayres35
Copy link
Contributor

fixes #1333

This PR simply updates repr implementations in a few classes to use self.__class__.__name__ rather than hardcoding the class name, and adds simple tests. Namely, it updates the TraitListEvent, TraitDictEvent, and TraitSetEvent classes as well as the AdaptationOffer and WeakIDDict classes.

Checklist

  • Tests
    - [ ] Update API reference (docs/source/traits_api_reference)
    - [ ] Update User manual (docs/source/traits_user_manual)
    - [ ] Update type annotation hints in traits-stubs

Copy link
Contributor

@rahulporuri rahulporuri left a comment

Choose a reason for hiding this comment

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

nit-picky comment : the code might look cleaner if we can use f-strings.

Comment on lines 56 to 61
return (
"{event.__class__.__name__}("
"removed={event.removed!r}, "
"added={event.added!r}, "
"changed={event.changed!r}"
")".format(event=self)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit : this might look cleaner if we use an f-string.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed; and the event -> self switch confused me the first time I looked at this code - just use self throughout.

Copy link
Contributor Author

@aaronayres35 aaronayres35 Nov 3, 2020

Choose a reason for hiding this comment

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

Agreed; and the event -> self switch confused me the first time I looked at this code - just use self throughout.

I had originally been following what was done here:

def __repr__(self):
return (
"{event.__class__.__name__}("
"object={event.object!r}, "
"removed={event.removed!r}, "
"added={event.added!r}"
")".format(event=self)
)

and with the other _something_change_event reprs. Shall I change these to just use self throughout and f strings as well?

Edit: I went ahead and just did this

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I see. Not sure it's worth changing that existing code, given that it's already working. Maybe do that in a separate PR?

Edit: Okay, fine!

@@ -93,6 +93,13 @@ def test_defaults(self):
self.assertEqual(event.removed, [])
self.assertEqual(event.added, [])

def test_trait_list_event_str_representation(self):
Copy link
Member

Choose a reason for hiding this comment

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

How about adding a test for a TraitListEvent subclass, to check that it's the subclass name that appears in the repr rather than TraitListEvent. (That's the original motivation for the change, after all.)

Copy link
Contributor

@rahulporuri rahulporuri left a comment

Choose a reason for hiding this comment

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

LGTM. Not sure if @mdickinson also wants to rereview.

Copy link
Member

@mdickinson mdickinson left a comment

Choose a reason for hiding this comment

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

LGTM2!

@aaronayres35 aaronayres35 merged commit 132db8f into master Nov 4, 2020
@aaronayres35 aaronayres35 deleted the fix-1333-hardcode-class-repr branch November 4, 2020 13:37
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.

Don't hard-code class names in TraitListEvent.__repr__ and similar __repr__ implementations
3 participants