bpo-32153: Add unit test for create_autospec with partial function returned in getattr#10398
Conversation
| class Foo(object): | ||
|
|
||
| def __getattr__(self, name): | ||
| return partial(lambda name: name, name) |
There was a problem hiding this comment.
What is name here? The parameter of __getattr__ or the parameter of the lambda? It is better to use different names for avoiding confusion.
Unconditional return in __getattr__() can cause issues, because internal code can get some special instance attributes and be confused by getting a lambda. Return only for specific name, and raise an AttributeError in other cases.
if name == '...':
return ...
raise AttributeErrorThere was a problem hiding this comment.
What is name here? The parameter of getattr or the parameter of the lambda? It is better to use different names for avoiding confusion.
I will change them. Thanks.
Unconditional return in getattr() can cause issues, because internal code can get some special instance attributes and be confused by getting a lambda. Return only for specific name, and raise an AttributeError in other cases.
From the original report this is about __getattr__ returning a partial function unconditionally for all attributes and partial functions have no __name__ attribute during copy which occurs at https://github.com/python/cpython/blob/master/Lib/unittest/mock.py#L108. I am slightly confused over why we should have an if clause for this and raise AttributeError that might change the original report that needs to be tested.
| def test_autospec_getattr_partial_function(self): | ||
| # bpo-32153 : getattr returning partial functions without | ||
| # __name__ should not create AttributeError in create_autospec | ||
| class Foo(object): |
There was a problem hiding this comment.
(object) is not needed.
|
@cjw296: Please replace |
|
@cjw296 Thanks for merging the PR. This test can be back ported to 3.7 since it's fixed there. I think @miss-islington will make the back port PR once you add the label "needs backport to 3.7". |
|
Thanks @tirkarthi for the PR, and @cjw296 for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7. |
|
GH-10855 is a backport of this pull request to the 3.7 branch. |
…turned in getattr (pythonGH-10398) * Add create_autospec with partial function returned in getattr * Use self.assertFalse instead of assert * Use different names and remove object (cherry picked from commit c667b09) Co-authored-by: Xtreak <tirkarthi@users.noreply.github.com>
|
@tirkarthi - done, do you know if the fix made it to 3.6 too? |
|
@cjw296 No, the issue was not fixed in 3.6 as noted in https://bugs.python.org/issue32153#msg327531 . I also ran the test for confirmation in latest 3.6 branch while opening the PR #10398 (comment) and it fails. So this test should not be back ported to 3.6. |
This adds an unit test for https://bugs.python.org/issue32153 which was out dated and fixed by https://bugs.python.org/issue28919 . During create_autospec the function attributes are copied and when getattr returns a partial function then it doesn't contain
__name__causingAttributeError. This was fixed in 3.7 suppressingAttributeErrorbut was not backported thus this test can be applied only on 3.7 and master.Fails on 3.6 branch
Passes on master
https://bugs.python.org/issue32513