[issue35577] side_effect mocked method lose reference to instance

Karthikeyan Singaravelan report at bugs.python.org
Thu Feb 14 13:04:09 EST 2019


Karthikeyan Singaravelan <tir.karthi at gmail.com> added the comment:

Thanks for the report. I think this is design by choice that self is not passed to the side_effect directly set on the mock [0]. Changing this would break existing tests like [1] . You can use the approach by @remi.lapeyre which directly replaces the function on the target inside the context manager at [2] passing self to the side_effect callable.

[0] https://github.com/python/cpython/blob/8a1657b93469580ef345c7c91738587f3d76e87d/Lib/unittest/mock.py#L1043
[1] https://github.com/python/cpython/blob/8a1657b93469580ef345c7c91738587f3d76e87d/Lib/unittest/test/testmock/testmock.py#L664
[2] https://github.com/python/cpython/blob/8a1657b93469580ef345c7c91738587f3d76e87d/Lib/unittest/mock.py#L1376

----------
nosy: +cjw296, mariocj89, xtreak
versions:  -Python 2.7, Python 3.4, Python 3.5, Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue35577>
_______________________________________


More information about the Python-bugs-list mailing list