Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions asyncio/futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

__all__ = ['CancelledError', 'TimeoutError',
'InvalidStateError',
'Future', 'wrap_future',
'Future', 'wrap_future', 'isfuture',
]

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


class Future:
Expand Down
18 changes: 18 additions & 0 deletions tests/test_futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,24 @@ def setUp(self):
self.loop = self.new_test_loop()
self.addCleanup(self.loop.close)

def test_isfuture(self):
class MyFuture:
_asyncio_future_blocking = None

def __init__(self):
self._asyncio_future_blocking = False

self.assertFalse(asyncio.isfuture(MyFuture))
self.assertTrue(asyncio.isfuture(MyFuture()))

self.assertFalse(asyncio.isfuture(1))
self.assertFalse(asyncio.isfuture(asyncio.Future))
self.assertFalse(asyncio.isfuture(mock.Mock()))

f = asyncio.Future(loop=self.loop)
self.assertTrue(asyncio.isfuture(f))
f.cancel()

def test_initial_state(self):
f = asyncio.Future(loop=self.loop)
self.assertFalse(f.cancelled())
Expand Down