]> granicus.if.org Git - python/commitdiff
Closes #21256: Printout of keyword args in deterministic order in mock calls.
authorKushal Das <kushaldas@gmail.com>
Mon, 9 Jun 2014 08:15:56 +0000 (13:45 +0530)
committerKushal Das <kushaldas@gmail.com>
Mon, 9 Jun 2014 08:15:56 +0000 (13:45 +0530)
Printout of keyword args should be in deterministic order in
a mock function call. This will help to write better doctests.

Lib/unittest/mock.py
Lib/unittest/test/testmock/testmock.py
Misc/NEWS

index d9c2ee96b09193954c868b4845796039f865b403..d00197688ae0f04b1111bac6f8315942ae9b3931 100644 (file)
@@ -1894,7 +1894,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 kwargs.items()
+        '%s=%r' % (key, value) for key, value in sorted(kwargs.items())
     ])
     if args_string:
         formatted_args = args_string
index b65dc321cff655e63066d15782f19504bd6924b9..7019ee901217f93346765a3ef20d710d5109f7cc 100644 (file)
@@ -1206,6 +1206,12 @@ class MockTest(unittest.TestCase):
         with self.assertRaises(AssertionError):
             m.hello.assert_not_called()
 
+    #Issue21256 printout of keyword args should be in deterministic order
+    def test_sorted_call_signature(self):
+        m = Mock()
+        m.hello(name='hello', daddy='hero')
+        text = "call(daddy='hero', name='hello')"
+        self.assertEquals(repr(m.hello.call_args), text)
 
     def test_mock_add_spec(self):
         class _One(object):
index 90f2208302eb1e47bbf9f42b44cb4992e83ff41f..99507251149ff56ab8bc252c3f4ac41a0960c752 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -92,6 +92,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #21256: Printout of keyword args should be in deterministic order in
+  a mock function call. This will help to write better doctests.
+
 - Issue #21677: Fixed chaining nonnormalized exceptions in io close() methods.
 
 - Issue #11709: Fix the pydoc.help function to not fail when sys.stdin is not a