Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 047f14c

Browse files
committed
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.
1 parent 85e4235 commit 047f14c

3 files changed

Lines changed: 10 additions & 1 deletion

File tree

Lib/unittest/mock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1894,7 +1894,7 @@ def _format_call_signature(name, args, kwargs):
18941894
formatted_args = ''
18951895
args_string = ', '.join([repr(arg) for arg in args])
18961896
kwargs_string = ', '.join([
1897-
'%s=%r' % (key, value) for key, value in kwargs.items()
1897+
'%s=%r' % (key, value) for key, value in sorted(kwargs.items())
18981898
])
18991899
if args_string:
19001900
formatted_args = args_string

Lib/unittest/test/testmock/testmock.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,12 @@ def test_assert_not_called(self):
12061206
with self.assertRaises(AssertionError):
12071207
m.hello.assert_not_called()
12081208

1209+
#Issue21256 printout of keyword args should be in deterministic order
1210+
def test_sorted_call_signature(self):
1211+
m = Mock()
1212+
m.hello(name='hello', daddy='hero')
1213+
text = "call(daddy='hero', name='hello')"
1214+
self.assertEquals(repr(m.hello.call_args), text)
12091215

12101216
def test_mock_add_spec(self):
12111217
class _One(object):

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ Core and Builtins
9292
Library
9393
-------
9494

95+
- Issue #21256: Printout of keyword args should be in deterministic order in
96+
a mock function call. This will help to write better doctests.
97+
9598
- Issue #21677: Fixed chaining nonnormalized exceptions in io close() methods.
9699

97100
- Issue #11709: Fix the pydoc.help function to not fail when sys.stdin is not a

0 commit comments

Comments
 (0)