[issue35577] side_effect mocked method lose reference to instance

2022-03-19 Thread Irit Katriel


Change by Irit Katriel :


--
resolution:  -> not a bug
stage:  -> resolved
status: open -> closed

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35577] side_effect mocked method lose reference to instance

2019-02-14 Thread Karthikeyan Singaravelan


Karthikeyan Singaravelan  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 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35577] side_effect mocked method lose reference to instance

2019-01-01 Thread Rémi Lapeyre

Rémi Lapeyre  added the comment:

Is there a problem with:

from unittest import mock

class SomeClass:
def do_something(self, x):
pass

def some_function(x):
obj = SomeClass()
y = obj.do_something(x)
return y

def do_something_side_effect(self, x):
print(self)
return x

def test_some_function():
with mock.patch.object(SomeClass, "do_something", do_something_side_effect):
assert some_function(1)

?

--
nosy: +remi.lapeyre

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue35577] side_effect mocked method lose reference to instance

2018-12-24 Thread Adnan Umer


New submission from Adnan Umer :

When a method/bounded function is mocked and side_effect is supplied to it, the 
side_effect function doesn't get the reference to the instance.

Suppose we have something like this


class SomeClass:
def do_something(self, x):
pass

def some_function(x):
cls = SomeClass()
y = class.do_something(x)
return y


And the test for some_function will be 


def do_something_side_effect(x):
retrun x

def test_some_function():
with mock.path("SomeCass.do_something") as do_something_mock:
do_something_mock.side_effect = do_something_side_effect
assert some_function(1)


Here do_something_side_effect mock will not have access to SomeClass instance.

--
components: Library (Lib)
messages: 332463
nosy: Adnan Umer
priority: normal
severity: normal
status: open
title: side_effect mocked method lose reference to instance
type: enhancement
versions: Python 2.7, Python 3.4, Python 3.5, Python 3.6, Python 3.7, Python 3.8

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com