0

Im trying patch an async property on a mock instance.

class Foo:
    @property
    async def bar(self) -> bool:
        return True


foo_mock = AsyncMock(Foo)
foo_mock.bar.return_value = False

assert await foo_mock.bar is False

but it fails with

>       assert await foo_mock.bar is False
E       TypeError: object MagicMock can't be used in 'await' expression

When the @property is not async it works as intended.

There is a similar question but the answer relies on patching the class instead of working with a mock instance which isn't an option for me. Mocking an Async Property in Python

1 Answers1

0

Likely pytest-asyncio will solve your issue: https://pypi.org/project/pytest-asyncio/

Ashton Honnecke
  • 688
  • 10
  • 16
  • I'm using trio instead of asyncio, together with https://pytest-trio.readthedocs.io/ What does pytest-asyncio do that would solve the issue? – daniel.gratzl Mar 22 '23 at 07:15
  • I'm just guessing as I can't get your example to run verbatim, can you alter your question to be a runnable example? – Ashton Honnecke Mar 23 '23 at 22:31