From 4c3b7e4da5773ab2ca176a1ce525faa5ee0f775d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Batuhan=20Ta=C5=9Fkaya?= Date: Tue, 14 May 2019 21:40:16 +0300 Subject: [PATCH 1/5] Remove __rmod__ of UserString --- Doc/library/collections.rst | 3 +++ Lib/collections/__init__.py | 3 --- .../next/Library/2019-05-14-21-39-52.bpo-25652.xLw42k.rst | 1 + 3 files changed, 4 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2019-05-14-21-39-52.bpo-25652.xLw42k.rst diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index ae21db216fdebe..78f3af75b6322b 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -1252,3 +1252,6 @@ attribute. .. versionchanged:: 3.5 New methods ``__getnewargs__``, ``__rmod__``, ``casefold``, ``format_map``, ``isprintable``, and ``maketrans``. + + .. versionchanged:: 3.8 + The ``__rmod__`` method is removed. diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index 960d82a5dcfbf5..31bedba0b520a1 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -1214,9 +1214,6 @@ def __mul__(self, n): __rmul__ = __mul__ def __mod__(self, args): return self.__class__(self.data % args) - def __rmod__(self, format): - return self.__class__(format % args) - # the following methods are defined in alphabetical order: def capitalize(self): return self.__class__(self.data.capitalize()) def casefold(self): diff --git a/Misc/NEWS.d/next/Library/2019-05-14-21-39-52.bpo-25652.xLw42k.rst b/Misc/NEWS.d/next/Library/2019-05-14-21-39-52.bpo-25652.xLw42k.rst new file mode 100644 index 00000000000000..1f77957fb279b1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2019-05-14-21-39-52.bpo-25652.xLw42k.rst @@ -0,0 +1 @@ +The ``__rmod__`` method of ``collections.UserString`` class is removed. From 26f1dc600dc475b0d0b85fbcccd135b0535a9ede Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Batuhan=20Ta=C5=9Fkaya?= Date: Tue, 21 May 2019 20:57:23 +0300 Subject: [PATCH 2/5] fix rmod --- Doc/library/collections.rst | 3 --- Lib/collections/__init__.py | 4 ++++ Lib/test/test_collections.py | 5 +++++ .../next/Library/2019-05-14-21-39-52.bpo-25652.xLw42k.rst | 2 +- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index 78f3af75b6322b..ae21db216fdebe 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -1252,6 +1252,3 @@ attribute. .. versionchanged:: 3.5 New methods ``__getnewargs__``, ``__rmod__``, ``casefold``, ``format_map``, ``isprintable``, and ``maketrans``. - - .. versionchanged:: 3.8 - The ``__rmod__`` method is removed. diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index 31bedba0b520a1..1ca3fbfaaba048 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -1214,6 +1214,10 @@ def __mul__(self, n): __rmul__ = __mul__ def __mod__(self, args): return self.__class__(self.data % args) + def __rmod__(self, template): + if isinstance(template, str): + return self.__class__(template % self) + return NotImplemented # the following methods are defined in alphabetical order: def capitalize(self): return self.__class__(self.data.capitalize()) def casefold(self): diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index e2d04d5b476196..213fa96bc00acc 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -70,6 +70,11 @@ def test_dict_copy(self): obj[123] = "abc" self._copy_test(obj) + def test_str_rmod(self): + arg = UserString("python") + template = "I love %s" + self.assertEqual(arg.__rmod__(template), "I love python") + self.assertIs(arg.__rmod__(type('Dummy', (), {})), NotImplemented) ################################################################################ ### ChainMap (helper class for configparser and the string module) diff --git a/Misc/NEWS.d/next/Library/2019-05-14-21-39-52.bpo-25652.xLw42k.rst b/Misc/NEWS.d/next/Library/2019-05-14-21-39-52.bpo-25652.xLw42k.rst index 1f77957fb279b1..421fccfe8c73a6 100644 --- a/Misc/NEWS.d/next/Library/2019-05-14-21-39-52.bpo-25652.xLw42k.rst +++ b/Misc/NEWS.d/next/Library/2019-05-14-21-39-52.bpo-25652.xLw42k.rst @@ -1 +1 @@ -The ``__rmod__`` method of ``collections.UserString`` class is removed. +Fix bug in ``__rmod__`` of ``UserString`` - by Batuhan Taskaya. From 84e19f71f396a7d5c2b08da8c72112652ae8b70d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Batuhan=20Ta=C5=9Fkaya?= Date: Tue, 21 May 2019 21:47:14 +0300 Subject: [PATCH 3/5] improve tests --- Lib/collections/__init__.py | 4 +--- Lib/test/test_collections.py | 15 +++++++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index 1ca3fbfaaba048..cb7f1bb1fcfe41 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -1215,9 +1215,7 @@ def __mul__(self, n): def __mod__(self, args): return self.__class__(self.data % args) def __rmod__(self, template): - if isinstance(template, str): - return self.__class__(template % self) - return NotImplemented + 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): diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 213fa96bc00acc..2f4973f2ef9224 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -71,10 +71,17 @@ def test_dict_copy(self): self._copy_test(obj) def test_str_rmod(self): - arg = UserString("python") - template = "I love %s" - self.assertEqual(arg.__rmod__(template), "I love python") - self.assertIs(arg.__rmod__(type('Dummy', (), {})), NotImplemented) + 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') ################################################################################ ### ChainMap (helper class for configparser and the string module) From 40cb664bfd81d42cab2cc3303775a16bca388711 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Batuhan=20Ta=C5=9Fkaya?= Date: Tue, 21 May 2019 22:02:39 +0300 Subject: [PATCH 4/5] drop nl --- Lib/test/test_collections.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index 2f4973f2ef9224..f5745309a325e7 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -74,7 +74,6 @@ def test_str_rmod(self): class ustr2(UserString): pass - class ustr3(ustr2): def __rmod__(self, other): return super().__rmod__(other) From 4657c735d803b3e7e9bd8b40d9fc122c0b2e6917 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Batuhan=20Ta=C5=9Fkaya?= Date: Tue, 21 May 2019 23:07:59 +0300 Subject: [PATCH 5/5] move test to proper file --- Lib/test/test_collections.py | 11 ----------- Lib/test/test_userstring.py | 12 ++++++++++++ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Lib/test/test_collections.py b/Lib/test/test_collections.py index f5745309a325e7..e2d04d5b476196 100644 --- a/Lib/test/test_collections.py +++ b/Lib/test/test_collections.py @@ -70,17 +70,6 @@ def test_dict_copy(self): obj[123] = "abc" self._copy_test(obj) - def test_str_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') ################################################################################ ### ChainMap (helper class for configparser and the string module) diff --git a/Lib/test/test_userstring.py b/Lib/test/test_userstring.py index 71528223d35bb5..19b0acfc760fa4 100644 --- a/Lib/test/test_userstring.py +++ b/Lib/test/test_userstring.py @@ -39,6 +39,18 @@ def checkcall(self, object, methodname, *args): # 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()