From 047f14c3c6ed39371fab2d93db8dfd5b5fdb06f1 Mon Sep 17 00:00:00 2001 From: Kushal Das Date: Mon, 9 Jun 2014 13:45:56 +0530 Subject: Closes #21256: Printout of keyword args in deterministic order in mock calls. 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 | 2 +- Lib/unittest/test/testmock/testmock.py | 6 ++++++ Misc/NEWS | 3 +++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index d9c2ee9..d001976 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -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 diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index b65dc32..7019ee9 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -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): diff --git a/Misc/NEWS b/Misc/NEWS index 90f2208..9950725 100644 --- 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 -- cgit v0.12