Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

Commit 26d01d7

Browse files
1st1gvanrossum
authored andcommitted
Fix isfuture() to return False for unitttest.Mock. (#455)
1 parent 07ac834 commit 26d01d7

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

asyncio/futures.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
__all__ = ['CancelledError', 'TimeoutError',
44
'InvalidStateError',
5-
'Future', 'wrap_future',
5+
'Future', 'wrap_future', 'isfuture',
66
]
77

88
import concurrent.futures._base
@@ -117,7 +117,8 @@ def isfuture(obj):
117117
itself as duck-type compatible by setting _asyncio_future_blocking.
118118
See comment in Future for more details.
119119
"""
120-
return getattr(obj, '_asyncio_future_blocking', None) is not None
120+
return (hasattr(type(obj), '_asyncio_future_blocking') and
121+
obj._asyncio_future_blocking is not None)
121122

122123

123124
class Future:

tests/test_futures.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,24 @@ def setUp(self):
101101
self.loop = self.new_test_loop()
102102
self.addCleanup(self.loop.close)
103103

104+
def test_isfuture(self):
105+
class MyFuture:
106+
_asyncio_future_blocking = None
107+
108+
def __init__(self):
109+
self._asyncio_future_blocking = False
110+
111+
self.assertFalse(asyncio.isfuture(MyFuture))
112+
self.assertTrue(asyncio.isfuture(MyFuture()))
113+
114+
self.assertFalse(asyncio.isfuture(1))
115+
self.assertFalse(asyncio.isfuture(asyncio.Future))
116+
self.assertFalse(asyncio.isfuture(mock.Mock()))
117+
118+
f = asyncio.Future(loop=self.loop)
119+
self.assertTrue(asyncio.isfuture(f))
120+
f.cancel()
121+
104122
def test_initial_state(self):
105123
f = asyncio.Future(loop=self.loop)
106124
self.assertFalse(f.cancelled())

0 commit comments

Comments
 (0)