[Python-checkins] bpo-37212: Preserve keyword argument order in unittest.mock.call and error messages (GH-14310)

Zachary Ware webhook-mailer at python.org
Mon Sep 9 06:55:26 EDT 2019


https://github.com/python/cpython/commit/9d607061c9c888913ae2c18543775cf360d55f27
commit: 9d607061c9c888913ae2c18543775cf360d55f27
branch: master
author: Xtreak <tir.karthi at gmail.com>
committer: Zachary Ware <zachary.ware at gmail.com>
date: 2019-09-09T05:55:22-05:00
summary:

bpo-37212: Preserve keyword argument order in unittest.mock.call and error messages (GH-14310)

files:
A Misc/NEWS.d/next/Library/2019-06-22-22-00-35.bpo-37212.Zhv-tq.rst
M Lib/unittest/mock.py
M Lib/unittest/test/testmock/testmock.py

diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index 89312f18c967..c9194fff4386 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -2320,7 +2320,7 @@ def _format_call_signature(name, args, kwargs):
     formatted_args = ''
     args_string = ', '.join([repr(arg) for arg in args])
     kwargs_string = ', '.join([
-        '%s=%r' % (key, value) for key, value in sorted(kwargs.items())
+        '%s=%r' % (key, value) for key, value in kwargs.items()
     ])
     if args_string:
         formatted_args = args_string
diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py
index 265eb1bba56c..581afaaeb815 100644
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -1571,11 +1571,11 @@ def test_assert_called_once_message_not_called(self):
             m.assert_called_once()
         self.assertNotIn("Calls:", str(e.exception))
 
-    #Issue21256 printout of keyword args should be in deterministic order
-    def test_sorted_call_signature(self):
+    #Issue37212 printout of keyword args now preserves the original order
+    def test_ordered_call_signature(self):
         m = Mock()
         m.hello(name='hello', daddy='hero')
-        text = "call(daddy='hero', name='hello')"
+        text = "call(name='hello', daddy='hero')"
         self.assertEqual(repr(m.hello.call_args), text)
 
     #Issue21270 overrides tuple methods for mock.call objects
diff --git a/Misc/NEWS.d/next/Library/2019-06-22-22-00-35.bpo-37212.Zhv-tq.rst b/Misc/NEWS.d/next/Library/2019-06-22-22-00-35.bpo-37212.Zhv-tq.rst
new file mode 100644
index 000000000000..520a0229aa9d
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2019-06-22-22-00-35.bpo-37212.Zhv-tq.rst
@@ -0,0 +1,2 @@
+:func:`unittest.mock.call` now preserves the order of keyword arguments in
+repr output. Patch by Karthikeyan Singaravelan.



More information about the Python-checkins mailing list