Skip to content

Commit bee8bfe

Browse files
bpo-37212: Preserve keyword argument order in unittest.mock.call and error messages (GH-14310)
(cherry picked from commit 9d60706) Co-authored-by: Xtreak <[email protected]>
1 parent 2522679 commit bee8bfe

File tree

3 files changed

+6
-4
lines changed

3 files changed

+6
-4
lines changed

Lib/unittest/mock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2320,7 +2320,7 @@ def _format_call_signature(name, args, kwargs):
23202320
formatted_args = ''
23212321
args_string = ', '.join([repr(arg) for arg in args])
23222322
kwargs_string = ', '.join([
2323-
'%s=%r' % (key, value) for key, value in sorted(kwargs.items())
2323+
'%s=%r' % (key, value) for key, value in kwargs.items()
23242324
])
23252325
if args_string:
23262326
formatted_args = args_string

Lib/unittest/test/testmock/testmock.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,11 +1563,11 @@ def test_assert_called_once_message_not_called(self):
15631563
m.assert_called_once()
15641564
self.assertNotIn("Calls:", str(e.exception))
15651565

1566-
#Issue21256 printout of keyword args should be in deterministic order
1567-
def test_sorted_call_signature(self):
1566+
#Issue37212 printout of keyword args now preserves the original order
1567+
def test_ordered_call_signature(self):
15681568
m = Mock()
15691569
m.hello(name='hello', daddy='hero')
1570-
text = "call(daddy='hero', name='hello')"
1570+
text = "call(name='hello', daddy='hero')"
15711571
self.assertEqual(repr(m.hello.call_args), text)
15721572

15731573
#Issue21270 overrides tuple methods for mock.call objects
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:func:`unittest.mock.call` now preserves the order of keyword arguments in
2+
repr output. Patch by Karthikeyan Singaravelan.

0 commit comments

Comments
 (0)