Issue 37972: unittest.mock.call does not chain __getitem__ to another _Call object - Python tracker

This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: blhsing, cjw296, mariocj89, michael.foord, python-dev, xtreak
Priority: normal Keywords: patch

Created on 2019-08-28 20:33 by blhsing, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 15565 merged blhsing, 2019-08-28 21:17
PR 15965 merged miss-islington, 2019-09-11 14:28
Messages (8)
msg350688 - (view) Author: Ben Hsing (blhsing) * Date: 2019-08-28 20:33
As reported on StackOverflow:
https://stackoverflow.com/questions/57636747/how-to-perform-assert-has-calls-for-a-getitem-call

The following code would output: [call(), call().foo(), call().foo().__getitem__('bar')]

from unittest.mock import MagicMock, call
mm = MagicMock()
mm().foo()['bar']
print(mm.mock_calls)

but trying to use that list with mm.assert_has_calls([call(), call().foo(), call().foo().__getitem__('bar')]) would result in:

TypeError: tuple indices must be integers or slices, not str
msg351830 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-09-11 11:34
If we go ahead with this then we should also support other dunder methods of tuple like __setitem__ and so on.
msg351846 - (view) Author: Ben Hsing (blhsing) * Date: 2019-09-11 12:45
Agreed. I've submitted a new commit with the call chaining behavior now generalized for all dunder methods by delegating the resolution of all attributes not directly in the _Call object's __dict__ keys to the getattr method.
msg351877 - (view) Author: Ben Hsing (blhsing) * Date: 2019-09-11 13:42
Correction. I've now done this by delegating the resolution of attributes belonging to the tuple class to _Call.__getattr__ instead. Passed all build tests.
msg351903 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2019-09-11 14:28
New changeset 72c359912d36705a94fca8b63d80451905a14ae4 by Michael Foord (blhsing) in branch 'master':
bpo-37972: unittest.mock._Call now passes on __getitem__ to the __getattr__ chaining so that call() can be subscriptable (GH-15565)
https://github.com/python/cpython/commit/72c359912d36705a94fca8b63d80451905a14ae4
msg352102 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2019-09-12 10:52
New changeset db0d8a5b2c803d30d9df436e00b6627ec8e09a13 by Michael Foord (Miss Islington (bot)) in branch '3.8':
bpo-37972: unittest.mock._Call now passes on __getitem__ to the __getattr__ chaining so that call() can be subscriptable (GH-15565) (GH-15965)
https://github.com/python/cpython/commit/db0d8a5b2c803d30d9df436e00b6627ec8e09a13
msg354383 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-10-10 14:40
Closing as fixed since PRs were merged. Thanks Ben.
msg360973 - (view) Author: Chris Withers (cjw296) * (Python committer) Date: 2020-01-29 16:25
New changeset db5e86adbce12350c26e7ffc2c6673369971a2dc by Chris Withers in branch 'master':
Get mock coverage back to 100% (GH-18228)
https://github.com/python/cpython/commit/db5e86adbce12350c26e7ffc2c6673369971a2dc