title: Implementing an asynchronous iterator
body: <p>Per <a href="https://www.python.org/dev/peps/pep-0492/#asynchronous-iterators-and-async-for" rel="nofollow noreferrer">PEP-492</a> I am trying to implement an asynchronous iterator, such that I can do e.g. </p>
  
  <pre><code>async for foo in bar:
      ...
  </code></pre>
  
  <p>Here is a trivial example, similar to the one in the docs, with a very basic test of instantiation and async iteration:</p>
  
  <pre><code>import pytest
  
  class TestImplementation:
      def __aiter__(self):
          return self
      async def __anext__(self):
          raise StopAsyncIteration
  
  
  @pytest.mark.asyncio  # note use of pytest-asyncio marker
  async def test_async_for():
      async for _ in TestImplementation():
          pass
  </code></pre>
  
  <p>However, when I execute my test suite, I see:</p>
  
  <pre><code>=================================== FAILURES ===================================
  ________________________________ test_async_for ________________________________
  
      @pytest.mark.asyncio
      async def test_async_for():
  &gt;       async for _ in TestImplementation():
  E       TypeError: 'async for' received an invalid object from __aiter__: TestImplementation
  
  ...: TypeError
  ===================== 1 failed, ... passed in 2.89 seconds ======================
  </code></pre>
  
  <p>Why does my <code>TestImplementation</code> appear to be invalid? As far as I can tell it meets the protocol:</p>
  
  <blockquote>
    <ol>
    <li>An object must implement an <code>__aiter__</code> method ... returning an asynchronous iterator object.</li>
    <li>An asynchronous iterator object must implement an <code>__anext__</code> method ... returning an awaitable.</li>
    <li>To stop iteration <code>__anext__</code> must raise a <code>StopAsyncIteration</code> exception.</li>
    </ol>
  </blockquote>
  
  <p>This is failing with the latest released versions of Python (3.5.1), <code>py.test</code> (2.9.2) and <code>pytest-asyncio</code> (0.4.1).</p>