You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
mock.patch.object does not persist __module__ name for functions #81015
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2019-05-07.14:44:21.027>labels= ['3.7', 'tests', '3.8', 'type-bug', 'library']
title='mock.patch.object does not persist __module__ name for functions'updated_at=<Date2019-05-07.15:46:44.614>user='https://github.com/goodspark'
I guess the problem is that to patch 'bar' in foo.bar the signature of bar is taken and then it's evaluated using exec [1] where the function body has _check_sig that checks for the signature during function call. _check_sig has attributes of func copied. Since it's evaluated using exec the returned funcopy variable might not have __module__ set as per the execution context. One possible approach would be to _copy_func_details(func, funcopy) so that the __module__ and other attributes are copied.
This produces a test failure where in case of lambdas the __name__ is <lambda> which is not a valid identifier and hence funcopy is used. Then __name__ is set as '<lambda>' by my patch. Hence, 'funcopy' is replaced by '<lambda>' causing below test failure. This could be resolved by setting __name__ again to the expected value. This was newly added for test coverage with adbf178.
My main worry is that it's used in create_autospec hence do we really want to copy __module__ and others to the patched function for places with create_autospec also with functions supplying autospec=True. This is a user expectation but I would like to know maintainers opinion as well of this change.
Activity
goodspark commentedon May 7, 2019
The expectation is that the __module__ attribute for a patched function should persist after patching.
Minimal test case is attached. Simply run pytest in a venv with the files.
Output:
def test_zxc():
with mock.patch.object(mymodule, 'asd', side_effect=mymodule.asd, autospec=True) as spy_asd:
E AssertionError: assert None == 'mymodule'
E + where None = <function asd at 0x7fe4cd6fd620>.__module__
test_mymodule.py:8: AssertionError
Originally reported at pytest-dev/pytest-mock#146 before it was determined this was a unittest.mock issue.
Happens on both Python 2.7 and 3.7. Probably not really tied to a specific Python version and more of mock library issue.
My local venv:
Python 3.7.2
pytest 4.4.1
tirkarthi commentedon May 7, 2019
I guess the problem is that to patch 'bar' in foo.bar the signature of bar is taken and then it's evaluated using exec [1] where the function body has _check_sig that checks for the signature during function call. _check_sig has attributes of func copied. Since it's evaluated using exec the returned funcopy variable might not have __module__ set as per the execution context. One possible approach would be to _copy_func_details(func, funcopy) so that the __module__ and other attributes are copied.
This produces a test failure where in case of lambdas the __name__ is <lambda> which is not a valid identifier and hence funcopy is used. Then __name__ is set as '<lambda>' by my patch. Hence, 'funcopy' is replaced by '<lambda>' causing below test failure. This could be resolved by setting __name__ again to the expected value. This was newly added for test coverage with adbf178.
My main worry is that it's used in create_autospec hence do we really want to copy __module__ and others to the patched function for places with create_autospec also with functions supplying autospec=True. This is a user expectation but I would like to know maintainers opinion as well of this change.
Reproducer
# cat /tmp/foo.py
# With patch
➜ cpython git:(master) ✗ ./python.exe /tmp/bar.py # No failure
# Patch
# Test failure without funcopy.__name__ = name in my patch where I don't restore the 'funcopy' for lambdas. But this can be fixed.
======================================================================
FAIL: test_spec_function_no_name (unittest.test.testmock.testhelpers.SpecSignatureTest)
----------------------------------------------------------------------
Traceback (most recent call last): File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/unittest/test/testmock/testhelpers.py", line 946, in test_spec_function_no_name self.assertEqual(mock.__name__, 'funcopy') AssertionError: '<lambda>' != 'funcopy' - <lambda> + funcopyRan 386 tests in 2.911s
[1]
cpython/Lib/unittest/mock.py
Line 165 in e85ef7a