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-38473: Handle autospecced functions and methods used with attach_mock #16784

Merged
merged 6 commits into from
Jan 24, 2020

Conversation

tirkarthi
Copy link
Member

@tirkarthi tirkarthi commented Oct 14, 2019

Extract signature from the inner mock object for methods/functions autospecced and attached to a mock object using attach_mock.

https://bugs.python.org/issue38473

@tirkarthi tirkarthi changed the title bpo38473: Handle autospecced method's signature used with attach_mock bpo-38473: Handle autospecced method's signature used with attach_mock Oct 14, 2019
@@ -820,7 +820,16 @@ def _get_call_signature_from_name(self, name):
break
else:
children = child._mock_children
Copy link
Member Author

Choose a reason for hiding this comment

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

Note to self, reviewer : Do we need to default to empty dictionary too guarding against AttributeError for _mock_children?

Copy link
Contributor

Choose a reason for hiding this comment

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

What test cases could we add to verify that?

@tirkarthi tirkarthi changed the title bpo-38473: Handle autospecced method's signature used with attach_mock bpo-38473: Handle autospecced functions and methods used with attach_mock Oct 14, 2019
@tirkarthi
Copy link
Member Author

Sorry, I somehow forgot this issue in my list. Would appreciate a review of this since it caused a regression. cc: @mariocj89

Thanks

@tirkarthi tirkarthi closed this Jan 21, 2020
@tirkarthi tirkarthi reopened this Jan 21, 2020
sig = child._spec_signature
try:
sig = child._spec_signature
except AttributeError:
Copy link
Contributor

Choose a reason for hiding this comment

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

In which situation will we fall here? Can you add a tests that exercises this?

Copy link
Member Author

Choose a reason for hiding this comment

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

I couldn't think of a situation that would get to this as autospecced ones should have this. But given that this was a regression of the previous change without tests catching it I added it as a fallback to have signature as None in case there are scenarios not tested here or try to use getattr with None as default.

Copy link
Contributor

Choose a reason for hiding this comment

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

I personally don't like the idea of having code "just if", but you wrote this function initially, so... 🤷‍♂ 😛

Copy link
Contributor

Choose a reason for hiding this comment

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

If we can't prove it's needed, let's drop it until we can. The backport caught up now, just want to get as many upstream PRs merged first before releasing, so we should get good feedback if this code can be reached.

@cjw296
Copy link
Contributor

cjw296 commented Jan 24, 2020

I'm afraid I don't have a lot to offer here, since I've never used attach_mock.

@cjw296 cjw296 removed their request for review January 24, 2020 07:59
@tirkarthi
Copy link
Member Author

I have removed the try/catch block. From

self._mock_add_spec(spec, spec_set, _spec_as_instance, _eat_self)
_spec_signature seems to be always initialized with None on mock object. The attribute error was due to inner mock object not being extracted which is fixed. I was slightly concerned since the tests didn't catch it and caused a regression. I understand your points on having an untested block as a fallback. I will revisit this if there is a report of it in future. Thanks for your inputs.

@@ -817,6 +817,12 @@ def _get_call_signature_from_name(self, name):
if child is None or isinstance(child, _SpecState):
break
else:
# If an autospecced object is attached using attach_mock the
# child would be a function with mock object as attribute from
# which signature has to be derived. If there is no signature
Copy link
Contributor

Choose a reason for hiding this comment

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

You might want to review this comment (maybe remove it as well and include it in the commit message).

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for catching this. I prefer to keep it here with the part about fallback updated since it will be good to know why extract_mock is needed. I will add this in the comment over this change.

@miss-islington
Copy link
Contributor

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

@miss-islington
Copy link
Contributor

Thanks @tirkarthi for the PR, and @cjw296 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 Jan 24, 2020
…mock (pythonGH-16784)

(cherry picked from commit 66b00a9)

Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
@bedevere-bot
Copy link

GH-18166 is a backport of this pull request to the 3.7 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jan 24, 2020
…mock (pythonGH-16784)

(cherry picked from commit 66b00a9)

Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
@bedevere-bot
Copy link

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

tirkarthi added a commit that referenced this pull request Jan 25, 2020
…mock (GH-16784) (GH-18167)

If an autospecced object is attached using attach_mock the
child would be a function with mock object as attribute from
which signature has to be derived.

(cherry picked from commit 66b00a9)

Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
tirkarthi added a commit that referenced this pull request Jan 25, 2020
…mock (GH-16784) (#18166)

If an autospecced object is attached using attach_mock the
child would be a function with mock object as attribute from
which signature has to be derived.

(cherry picked from commit 66b00a9)

Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
shihai1991 pushed a commit to shihai1991/cpython that referenced this pull request Jan 31, 2020
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.

6 participants