python/asyncio

mock.Mock() acts like a Future

popravich opened this issue · 12 comments

Since this changes 32edb29 unittest.mock.Mock being detected
as asyncio.Future.
asyncio.futures.isfuture checks for private attribute _asyncio_future_blocking
to be not None and Mock() (without specs) returns new mock as this attribute.

The use case is here aio-libs-abandoned/aioredis-py#161 — basically, function wrapped with asyncio.coroutine
and set as Mock's method side_effect started to raise TypeError.

1st1 commented

We can probably check obj.__class__ for _asyncio_future_blocking attribute.

Is there something you want us to do?

Yeah, it was more like FYI.
Maybe a little bit stricter check would be better.
Some use cases like implementing some kind of RPC with dynamic methods resolution might hit this case

1st1 commented

@gvanrossum Please see #455.

I'm not at all clear that a Mock should not be considered a Future. The clarification "Some use cases like implementing some kind of RPC with dynamic methods resolution" sounds awefully vague. Maybe there are just as many use cases that would break with this change?

1st1 commented

Maybe there are just as many use cases that would break with this change?

isinstance(unittest.mock.Mock(), asyncio.Future) returns False. If we want to promote to use isfuture instead of isinstance, we should fix it.

isinstance(unittest.mock.Mock(), asyncio.Future) returns False. If we
want to promote to use isfuture instead of isinstance, we should fix it.

OK, that makes sense.

Fixed by 26d01d7 (#455).

isinstance(unittest.mock.Mock(), asyncio.Future) returns False

isinstance(unittest.mock.Mock(asyncio.Future), asyncio.Future) will return True.
But isfuture(unittest.mock.Mock(asyncio.Future)) will return False.
Is that ok?

1st1 commented

Oh. Let me see how Mocks work.

1st1 commented

But isfuture(unittest.mock.Mock(asyncio.Future)) will return False. Is that ok?

Fixed.