uber / doubles

Test doubles for Python.
MIT License
164 stars 17 forks source link

Make doubles work with partials #115

Closed rabbbit closed 6 years ago

rabbbit commented 8 years ago

Hey-oh,

I realize I should put up a PR instead, but as a step one lets see if you know about it already.

Should doubles work with partials? Tried to make it work, but everything seems to be failing :(

from functools import partial

from doubles import expect

def some_function(args1, arg2):
    print args1, arg2

my_partial = partial(some_function, arg2=2)

# need this to make doubles work nicely I think?
class Namespace(object):
    some_function = some_function
    my_partial = my_partial

def test_function():
    n = Namespace()
    expect(n).some_function.with_args(arg2=2).once()
    n.some_function(arg2=2)

def test_partial():
    n = Namespace()
    expect(n).some_function.with_args(arg2=2).once()
    n.my_partial(n)

def test_another_partial():
    n = Namespace()
    expect(n).my_partial.with_args().once()
    n.my_partial(arg2=2)

def test_more_partial():
    n = Namespace()
    expect(n.my_partial).__call__.with_args().once()
    n.my_partial(arg2=2)
toddsifleet commented 8 years ago

Thanks for reporting!

We had run into a similar issue with objects that define __call__, which was resolved. This sound similar to that.

What error are you seeing? What version are you running?

toddsifleet commented 6 years ago

I think that is because the method returned by functools.partial is not a python method, but a native C method, which doubles cannot validate this. To get around this issue you can use the with no_builtin_verification() context manager.