classification
Title: unittest.mock.call does not chain __getitem__ to another _Call object
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.9, Python 3.8
process
Status: open Resolution:
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 2019-09-12 10:52 by python-dev.

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 (6)
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 triager) 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
History
Date User Action Args
2019-09-12 10:52:52python-devsetmessages: + msg352102
2019-09-11 14:28:20miss-islingtonsetpull_requests: + pull_request15598
2019-09-11 14:28:09python-devsetnosy: + python-dev
messages: + msg351903
2019-09-11 13:42:18blhsingsetmessages: + msg351877
2019-09-11 12:59:00blhsingsetstatus: pending -> open
2019-09-11 12:45:03blhsingsetstatus: open -> pending

messages: + msg351846
2019-09-11 11:34:48xtreaksetnosy: + cjw296, michael.foord, mariocj89

messages: + msg351830
versions: - Python 2.7, Python 3.5, Python 3.6, Python 3.7
2019-08-28 21:17:21blhsingsetkeywords: + patch
stage: patch review
pull_requests: + pull_request15240
2019-08-28 20:58:09xtreaksetnosy: + xtreak
2019-08-28 20:33:01blhsingcreate