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

@blhsing blhsing 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

…attr__ chaining so that call() can be subscriptable
@the-knights-who-say-ni
Copy link

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!

Copy link
Contributor

@voidspace voidspace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Nice catch and fix.

@miss-islington
Copy link
Contributor

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

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 11, 2019
…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
Copy link

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

voidspace pushed a commit that referenced this pull request Sep 12, 2019
…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>
DinoV pushed a commit to DinoV/cpython that referenced this pull request Sep 12, 2019
…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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants