Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-37972: unittest.mock._Call now passes on __getitem__ to the __getattr__ chaining so that call() can be subscriptable #15565

Merged
merged 5 commits into from Sep 11, 2019

Conversation

@blhsing
Copy link
Contributor

commented Aug 28, 2019

As reported on StackOverflow:
https://stackoverflow.com/questions/57636747/how-to-perform-assert-has-calls-for-a-getitem-call

The following code:

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

would output:

[call(), call().foo(), call().foo().__getitem__('bar')]

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

This fix now makes unittest.mock._Call.__getattribute__ raise an AttributeError when the given attribute name is '__getitem__' so that __getitem__ can get the same chaining mechanism provided by the __getattr__ method.

https://bugs.python.org/issue37972

bpo-37972: unittest.mock._Call now passes on __getitem__ to the __get…
…attr__ chaining so that call() can be subscriptable
@the-knights-who-say-ni

This comment has been minimized.

Copy link

commented Aug 28, 2019

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Our records indicate we have not received your CLA. For legal reasons we need you to sign this before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

If you have recently signed the CLA, please wait at least one business day
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for your contribution, we look forward to reviewing it!

blurb-it bot and others added 2 commits Aug 28, 2019
Update 2019-08-28-21-40-12.bpo-37972.kP-n4L.rst
added name of the contributor
@voidspace
Copy link
Contributor

left a comment

LGTM. Nice catch and fix.

@voidspace voidspace merged commit 72c3599 into python:master Sep 11, 2019

5 checks passed

Azure Pipelines PR #20190911.110 succeeded
Details
bedevere/issue-number Issue number 37972 found
Details
bedevere/news News entry found in Misc/NEWS.d
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@miss-islington

This comment has been minimized.

Copy link

commented Sep 11, 2019

Thanks @blhsing for the PR, and @voidspace for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8.
🐍🍒🤖

miss-islington added a commit to miss-islington/cpython that referenced this pull request Sep 11, 2019
bpo-37972: unittest.mock._Call now passes on __getitem__ to the __get…
…attr__ chaining so that call() can be subscriptable (pythonGH-15565)

* bpo-37972: unittest.mock._Call now passes on __getitem__ to the __getattr__ chaining so that call() can be subscriptable

* 📜🤖 Added by blurb_it.

* Update 2019-08-28-21-40-12.bpo-37972.kP-n4L.rst

added name of the contributor

* bpo-37972: made all dunder methods chainable for _Call

* bpo-37972: delegate only attributes of tuple instead to __getattr__
(cherry picked from commit 72c3599)

Co-authored-by: blhsing <github@ydooby.com>
@bedevere-bot

This comment has been minimized.

Copy link

commented Sep 11, 2019

GH-15965 is a backport of this pull request to the 3.8 branch.

voidspace added a commit that referenced this pull request Sep 12, 2019
bpo-37972: unittest.mock._Call now passes on __getitem__ to the __get…
…attr__ chaining so that call() can be subscriptable (GH-15565) (GH-15965)

* bpo-37972: unittest.mock._Call now passes on __getitem__ to the __getattr__ chaining so that call() can be subscriptable

* 📜🤖 Added by blurb_it.

* Update 2019-08-28-21-40-12.bpo-37972.kP-n4L.rst

added name of the contributor

* bpo-37972: made all dunder methods chainable for _Call

* bpo-37972: delegate only attributes of tuple instead to __getattr__
(cherry picked from commit 72c3599)

Co-authored-by: blhsing <github@ydooby.com>
vrpolakatcisco added a commit to vrpolakatcisco/cpython that referenced this pull request Sep 12, 2019
bpo-37972: unittest.mock._Call now passes on __getitem__ to the __get…
…attr__ chaining so that call() can be subscriptable (pythonGH-15565)

* bpo-37972: unittest.mock._Call now passes on __getitem__ to the __getattr__ chaining so that call() can be subscriptable

* 📜🤖 Added by blurb_it.

* Update 2019-08-28-21-40-12.bpo-37972.kP-n4L.rst

added name of the contributor

* bpo-37972: made all dunder methods chainable for _Call

* bpo-37972: delegate only attributes of tuple instead to __getattr__
DinoV added a commit to DinoV/cpython that referenced this pull request Sep 12, 2019
bpo-37972: unittest.mock._Call now passes on __getitem__ to the __get…
…attr__ chaining so that call() can be subscriptable (pythonGH-15565)

* bpo-37972: unittest.mock._Call now passes on __getitem__ to the __getattr__ chaining so that call() can be subscriptable

* 📜🤖 Added by blurb_it.

* Update 2019-08-28-21-40-12.bpo-37972.kP-n4L.rst

added name of the contributor

* bpo-37972: made all dunder methods chainable for _Call

* bpo-37972: delegate only attributes of tuple instead to __getattr__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants
You can’t perform that action at this time.