Skip to content

Conversation

@isidentical
Copy link
Member

@isidentical isidentical commented May 14, 2019

The __rmod__ method of collections.UserString class had a bug that made it unusable.

https://bugs.python.org/issue25652

@isidentical isidentical requested a review from rhettinger as a code owner May 14, 2019 18:41
@isidentical isidentical changed the title Remove __rmod__ of UserString bpo-25652: Remove __rmod__ of UserString May 14, 2019
@jcgoble3
Copy link
Contributor

As the person who originally filed the bpo issue, I stand by my statement that it's better to fix the bug rather than just remove the method. As demonstrated by my userstringerror.py attachment on the issue, a subclass of a subclass of UserString can be bitten by this due to Python allowing a subclass to override a dunder of the parent class, even when the subclass object is the right-hand operand. (For example, the sub-subclass may be for a specialized case where pre-processing or normalization is required before formatting.)

Therefore __rmod__ is useful, and removing it would force subclass authors to write the implementation themselves rather than extending the UserString method..

@isidentical
Copy link
Member Author

isidentical commented May 14, 2019

@rhettinger whats your thought?

I agree with @jcgoble3

@gvanrossum
Copy link
Member

Yeah, let's fix the bug and add a test.

@isidentical
Copy link
Member Author

Fixed and tested.

@gvanrossum gvanrossum changed the title bpo-25652: Remove __rmod__ of UserString bpo-25652: Fix __rmod__ of UserString May 21, 2019
arg = UserString("python")
template = "I love %s"
self.assertEqual(arg.__rmod__(template), "I love python")
self.assertIs(arg.__rmod__(type('Dummy', (), {})), NotImplemented)
Copy link
Member

Choose a reason for hiding this comment

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

It would be more convincing if the test reproduced the end-to-end example from the bpo issue (https://bugs.python.org/file46854/userstringerror.py).

Copy link
Member Author

Choose a reason for hiding this comment

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

Okey, done.

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

OK, there's one whitespace nit in the new test left.

def test_str_rmod(self):
class ustr2(UserString):
pass

Copy link
Member

Choose a reason for hiding this comment

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

Drop the extra blank line please.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, sorry for that. Fixed

@jcgoble3
Copy link
Contributor

Do the new tests belong in test_collections.py or test_userstring.py? I'm also confused as to why the three test_user*.py files exist when test_collections.py also has tests for those classes.

@isidentical
Copy link
Member Author

I wasn't aware of these tests. Should i move the test?

@jcgoble3
Copy link
Contributor

I'm still new to the contributing process, so I will let the core devs figure out where they belong. I honestly don't know, hence why I asked it as a neutral question.

@gvanrossum
Copy link
Member

Looks like the tests in test_collections.py only make very little mention of UserString etc., and the important tests for those classes are in their own test file. So I recommend moving the new tests to test_userstring.py.

@isidentical
Copy link
Member Author

I moved the test.

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

OK, if the test pass it will be merged. Thanks!

@isidentical
Copy link
Member Author

Thank you for reviewing it.

@miss-islington
Copy link
Contributor

@isidentical: Status check is done, and it's a success ✅ .

@miss-islington miss-islington merged commit 7abf8c6 into python:master May 21, 2019
@isidentical isidentical deleted the issue25652 branch December 7, 2019 12:09
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.

6 participants