bpo-35226: Fix equality for nested unittest.mock.call objects. (GH-10555)
Also refactor the call recording imolementation and add some notes
about its limitations.
(cherry picked from commit 8ca0fa9d2f4de6e69f0902790432e0ab2f37ba68)
Co-authored-by: Chris Withers <chris@withers.org>
diff --git a/Doc/library/unittest.mock-examples.rst b/Doc/library/unittest.mock-examples.rst
index 65dee7c..00b9bc1 100644
--- a/Doc/library/unittest.mock-examples.rst
+++ b/Doc/library/unittest.mock-examples.rst
@@ -153,6 +153,15 @@
>>> mock.mock_calls == expected
True
+However, parameters to calls that return mocks are not recorded, which means it is not
+possible to track nested calls where the parameters used to create ancestors are important:
+
+ >>> m = Mock()
+ >>> m.factory(important=True).deliver()
+ <Mock name='mock.factory().deliver()' id='...'>
+ >>> m.mock_calls[-1] == call.factory(important=False).deliver()
+ True
+
Setting Return Values and Attributes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
diff --git a/Doc/library/unittest.mock.rst b/Doc/library/unittest.mock.rst
index 91633b5..f1b2595 100644
--- a/Doc/library/unittest.mock.rst
+++ b/Doc/library/unittest.mock.rst
@@ -680,6 +680,19 @@
unpacked as tuples to get at the individual arguments. See
:ref:`calls as tuples <calls-as-tuples>`.
+ .. note::
+
+ The way :attr:`mock_calls` are recorded means that where nested
+ calls are made, the parameters of ancestor calls are not recorded
+ and so will always compare equal:
+
+ >>> mock = MagicMock()
+ >>> mock.top(a=3).bottom()
+ <MagicMock name='mock.top().bottom()' id='...'>
+ >>> mock.mock_calls
+ [call.top(a=3), call.top().bottom()]
+ >>> mock.mock_calls[-1] == call.top(a=-1).bottom()
+ True
.. attribute:: __class__