The ``__rmod__`` method of ``collections.UserString`` class had a bug that made it unusable.
https://bugs.python.org/issue25652
__rmul__ = __mul__
def __mod__(self, args):
return self.__class__(self.data % args)
- def __rmod__(self, format):
- return self.__class__(format % args)
-
+ def __rmod__(self, template):
+ return self.__class__(str(template) % self)
# the following methods are defined in alphabetical order:
def capitalize(self): return self.__class__(self.data.capitalize())
def casefold(self):
# we don't fix the arguments, because UserString can't cope with it
getattr(object, methodname)(*args)
+ def test_rmod(self):
+ class ustr2(UserString):
+ pass
+
+ class ustr3(ustr2):
+ def __rmod__(self, other):
+ return super().__rmod__(other)
+
+ fmt2 = ustr2('value is %s')
+ str3 = ustr3('TEST')
+ self.assertEqual(fmt2 % str3, 'value is TEST')
+
if __name__ == "__main__":
unittest.main()
--- /dev/null
+Fix bug in ``__rmod__`` of ``UserString`` - by Batuhan Taskaya.