cpython icon indicating copy to clipboard operation
cpython copied to clipboard

`unittest.mock.patch.dict` returns function when applied to coroutines

Open seifertm opened this issue 3 years ago • 0 comments

Bug report

unittest.mock.patch.dict returns a synchronous function when applied to coroutines. Contrary to that, unittest.mock.patch returns coroutines when applied to coroutines (see #81177).

My expectation is that patch and patch.dict behave the same when applied to coroutines. I expect the following code to print True and True, but it prints True and False for CPython>=3.8:

import inspect
from unittest.mock import Mock, patch


@patch("binascii.b64encode")
async def patch_coro():
    ...


@patch.dict("sys.modules", test_module=Mock())
async def patch_dict_coro():
    ...

print(inspect.iscoroutinefunction(patch_coro))
print(inspect.iscoroutinefunction(patch_dict_coro))

Your environment

  • CPython versions tested on: 3.7, 3.8, 3.9, 3.10, 3.11-rc2
  • Operating system and architecture: Linux x86_64

CPython v3.7 is not affected by this issue, because support for coroutines was added to mock.patch in v3.8 (see #81177). The supplied example prints False and False.

  • PR: gh-99365
  • PR: gh-99366

seifertm avatar Oct 08 '22 06:10 seifertm