Patch __call__ of a function

warvariuc

I need to patch current datetime in tests. I am using this solution:

def _utcnow():
    return datetime.datetime.utcnow()


def utcnow():
    """A proxy which can be patched in tests.
    """
    # another level of indirection, because some modules import utcnow
    return _utcnow()

Then in my tests I do something like:

    with mock.patch('***.utils._utcnow', return_value=***):
        ...

But today an idea came to me, that I could make the implementation simpler by patching __call__ of function utcnow instead of having an additional _utcnow.

This does not work for me:

    from ***.utils import utcnow
    with mock.patch.object(utcnow, '__call__', return_value=***):
        ...

How to do this elegantly?

zvone

When you patch __call__ of a function, you are setting the __call__ attribute of that instance. Python actually calls the __call__ method defined on the class.

For example:

>>> class A(object):
...     def __call__(self):
...         print 'a'
...
>>> a = A()
>>> a()
a
>>> def b(): print 'b'
...
>>> b()
b
>>> a.__call__ = b
>>> a()
a
>>> a.__call__ = b.__call__
>>> a()
a

Assigning anything to a.__call__ is pointless.

However:

>>> A.__call__ = b.__call__
>>> a()
b

TLDR;

a() does not call a.__call__. It calls type(a).__call__(a).

Links

There is a good explanation of why that happens in answer to "Why type(x).__enter__(x) instead of x.__enter__() in Python standard contextlib?".

This behaviour is documented in Python documentation on Special method lookup.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to monkey patch a `__call__` method?

From Dev

How to monkey patch a `__call__` method?

From Dev

Python how to patch a function in another module and call it internally?

From Dev

Angularjs PATCH - undefined is not a function

From Dev

How to patch a local function?

From Dev

How can i write a decorator using a class's __call__ function?

From Dev

mock.patch() not patching class who called a couples of levels inside function call

From Dev

Is it possible to patch a dotnet function on the fly

From Dev

Filling in rectangles drawn with the patch function

From Dev

<method-wrapper '__call__' of functools.partial object at 0x1356e10> is not a Python function

From Dev

Patch over a function imported inside another function

From Dev

decorate __call__ with @staticmethod

From Dev

Python mock patch a function missing arguments

From Dev

Removing patch on matlab figure without close function

From Dev

How to patch a function that a Flask view calls

From Dev

NetLogo - Stepping on patch initializes new function

From Dev

Python: monkey patch a function's source code

From Dev

Python mock patch a function missing arguments

From Dev

How to patch an function which intermittently raises exceptions

From Dev

Using patch to to mock a function (as opposed to a method)

From Dev

Function patch not being picked up by imported module

From Dev

Is there a diff/patch function/library for varchar field in Postgres

From Dev

Python Decorators - __call__ in class

From Dev

attaching __call__ at runtime not working

From Dev

Overriding Enum __call__ method

From Dev

How to get the call count using Mock @patch?

From Dev

Patch/mock function inside another function for testing in javascript

From Dev

Monkey Patch private instance function in python 2 with calling old function

From Dev

Python mock patch a function decorator's parameter value

Related Related

  1. 1

    How to monkey patch a `__call__` method?

  2. 2

    How to monkey patch a `__call__` method?

  3. 3

    Python how to patch a function in another module and call it internally?

  4. 4

    Angularjs PATCH - undefined is not a function

  5. 5

    How to patch a local function?

  6. 6

    How can i write a decorator using a class's __call__ function?

  7. 7

    mock.patch() not patching class who called a couples of levels inside function call

  8. 8

    Is it possible to patch a dotnet function on the fly

  9. 9

    Filling in rectangles drawn with the patch function

  10. 10

    <method-wrapper '__call__' of functools.partial object at 0x1356e10> is not a Python function

  11. 11

    Patch over a function imported inside another function

  12. 12

    decorate __call__ with @staticmethod

  13. 13

    Python mock patch a function missing arguments

  14. 14

    Removing patch on matlab figure without close function

  15. 15

    How to patch a function that a Flask view calls

  16. 16

    NetLogo - Stepping on patch initializes new function

  17. 17

    Python: monkey patch a function's source code

  18. 18

    Python mock patch a function missing arguments

  19. 19

    How to patch an function which intermittently raises exceptions

  20. 20

    Using patch to to mock a function (as opposed to a method)

  21. 21

    Function patch not being picked up by imported module

  22. 22

    Is there a diff/patch function/library for varchar field in Postgres

  23. 23

    Python Decorators - __call__ in class

  24. 24

    attaching __call__ at runtime not working

  25. 25

    Overriding Enum __call__ method

  26. 26

    How to get the call count using Mock @patch?

  27. 27

    Patch/mock function inside another function for testing in javascript

  28. 28

    Monkey Patch private instance function in python 2 with calling old function

  29. 29

    Python mock patch a function decorator's parameter value

HotTag

Archive