Skip to content

Commit 26f1dc6

Browse files
author
Batuhan Taşkaya
committed
fix rmod
1 parent 4c3b7e4 commit 26f1dc6

4 files changed

Lines changed: 10 additions & 4 deletions

File tree

Doc/library/collections.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,3 @@ attribute.
12521252
.. versionchanged:: 3.5
12531253
New methods ``__getnewargs__``, ``__rmod__``, ``casefold``,
12541254
``format_map``, ``isprintable``, and ``maketrans``.
1255-
1256-
.. versionchanged:: 3.8
1257-
The ``__rmod__`` method is removed.

Lib/collections/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,10 @@ def __mul__(self, n):
12141214
__rmul__ = __mul__
12151215
def __mod__(self, args):
12161216
return self.__class__(self.data % args)
1217+
def __rmod__(self, template):
1218+
if isinstance(template, str):
1219+
return self.__class__(template % self)
1220+
return NotImplemented
12171221
# the following methods are defined in alphabetical order:
12181222
def capitalize(self): return self.__class__(self.data.capitalize())
12191223
def casefold(self):

Lib/test/test_collections.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ def test_dict_copy(self):
7070
obj[123] = "abc"
7171
self._copy_test(obj)
7272

73+
def test_str_rmod(self):
74+
arg = UserString("python")
75+
template = "I love %s"
76+
self.assertEqual(arg.__rmod__(template), "I love python")
77+
self.assertIs(arg.__rmod__(type('Dummy', (), {})), NotImplemented)
7378

7479
################################################################################
7580
### ChainMap (helper class for configparser and the string module)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
The ``__rmod__`` method of ``collections.UserString`` class is removed.
1+
Fix bug in ``__rmod__`` of ``UserString`` - by Batuhan Taskaya.

0 commit comments

Comments
 (0)