hRobert12 / mock

Automatically exported from code.google.com/p/mock
BSD 2-Clause "Simplified" License
0 stars 0 forks source link

mock.patch doesn't support functool.partial objects #160

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

import mock, functools
f = functools.partial(lambda a, b : a + b, 1)
with mock.patch(f) as mf:
    pass

-----

What is the expected output? What do you see instead?
I expect: no output from the above minimal example 
I see: 
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/home/agravier/dev/python_cog_neuro/pycogmo/<ipython-input-89-6bed8af839e6> in 
<module>()
----> 1 with mock.patch(f) as mf:
      2     pass

/home/agravier/.local/share/python2.7/venvs/pycogmo/lib/python2.7/site-packages/
mock.pyc in patch(target, new, spec, create, mocksignature, spec_set, autospec, 
new_callable, **kwargs)
   1549     available for alternate use-cases.
   1550     """
-> 1551     getter, attribute = _get_target(target)
   1552     return _patch(
   1553         getter, attribute, new, spec, create, mocksignature,

/home/agravier/.local/share/python2.7/venvs/pycogmo/lib/python2.7/site-packages/
mock.pyc in _get_target(target)
   1387 def _get_target(target):
   1388     try:
-> 1389         target, attribute = target.rsplit('.', 1)
   1390     except (TypeError, ValueError):
   1391         raise TypeError("Need a valid target to patch. You supplied: %r" %

AttributeError: 'functools.partial' object has no attribute 'rsplit'

-----

What version of the product are you using? On what operating system?

python 2.7 and Mock 0.8 on Archlinux x86_64

-----

Please provide any additional information below.

Thanks for this nice mocking library :)

Original issue reported on code.google.com by al.grav...@gmail.com on 24 May 2012 at 8:49

GoogleCodeExporter commented 8 years ago
The first argument to patch has to be a *string* specifying what you want to be 
patched. If you want a mock using the partial as a spec then you can use 
mock.create_autospec instead. From the snippet of code you've posted I'm not 
sure what your intent is - what did you expect to happen?

Original comment by fuzzyman on 24 May 2012 at 8:52

GoogleCodeExporter commented 8 years ago
I wanted to monkey patch a function (defined by partial application) from 
another module using a mock patch and the with syntax. I forgot the syntax of 
mock.patch, hence my string mistake. The real snippet of code is that:

with patch("common.pynn_utils.get_rate_encoder") as mock_get_rate_encoder:
     mock_get_rate_encoder.return_value = mock_rate_encoder
     w = select_kwta_winners(mock_pop, 4)
     eq_(w, [(2, 12), (1, 3), (1, 8), (2, 14)])

But I don't need nose for that of course, and my intent may very well make no 
sense, so let's forget about it all. For reference, my normal code reads as:

try:
    scheduling.nettraining.get_rate_encoder = Mock()
    scheduling.nettraining.get_rate_encoder.return_value = mock_rate_encoder
    w = select_kwta_winners(mock_pop, 4)
    eq_(w, [(2, 12), (1, 3), (1, 8), (2, 14)])
finally:
    scheduling.nettraining.get_rate_encoder = common.pynn_utils.get_rate_encoder

Original comment by al.grav...@gmail.com on 24 May 2012 at 8:45

GoogleCodeExporter commented 8 years ago
I'm afraid I still can't work out what you want to happen from those code 
snippets. Can you produce a minimal code snippet showing how you would like to 
use Mock and why you can't do it already.

Original comment by fuzzyman on 25 May 2012 at 9:18