bpo-37251: MagicMock with function spec used as a spec in mock.patch should return a MagicMock by tirkarthi · Pull Request #14117 · python/cpython

MagicMock with a function spec will have __code__ object. When it is used a spec to mock.patch then __code__ object which is a MagicMock is used to detect if it's a coroutine. MagicMock.__code__ & 128 returns another MagicMock which is evaluated to True and returned as a coroutine incorrectly. Hence check __code__ to be a CodeType to make sure MagicMock with __code__ is not detected as coroutine.

https://bugs.python.org/issue37251