-
-
Notifications
You must be signed in to change notification settings - Fork 31.5k
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
Conversation
Lib/unittest/mock.py
Outdated
@@ -820,7 +820,16 @@ def _get_call_signature_from_name(self, name): | |||
break | |||
else: | |||
children = child._mock_children |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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?
Sorry, I somehow forgot this issue in my list. Would appreciate a review of this since it caused a regression. cc: @mariocj89 Thanks |
Lib/unittest/mock.py
Outdated
sig = child._spec_signature | ||
try: | ||
sig = child._spec_signature | ||
except AttributeError: |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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... 🤷♂ 😛
There was a problem hiding this comment.
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.
I'm afraid I don't have a lot to offer here, since I've never used |
I have removed the Line 434 in 161e7b3
_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.
|
Lib/unittest/mock.py
Outdated
@@ -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 |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
Thanks @tirkarthi for the PR, and @cjw296 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7. |
Thanks @tirkarthi for the PR, and @cjw296 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.8. |
…mock (pythonGH-16784) (cherry picked from commit 66b00a9) Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
GH-18166 is a backport of this pull request to the 3.7 branch. |
…mock (pythonGH-16784) (cherry picked from commit 66b00a9) Co-authored-by: Karthikeyan Singaravelan <tir.karthi@gmail.com>
GH-18167 is a backport of this pull request to the 3.8 branch. |
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