pythonpython-3.xasynchronousstripe-paymentspython-asyncio

Stripe Python AttributeError: 'coroutine' object has no attribute 'auto_paging_iter'


An example in the Stripe Python library doesn't seem to work. The README says:

# .auto_paging_iter() implements both AsyncIterable and Iterable
async for c in await stripe.Customer.list_async().auto_paging_iter():
    ....

I try running this locally:

import asyncio
import stripe

stripe.api_key = "secret"


async def main():
    async for c in await stripe.Customer.list_async().auto_paging_iter():
        print(c)


if __name__ == "__main__":
    asyncio.run(main())

But I get an error and a warning:

Traceback (most recent call last):
  File "/Users/person/project/call_stripe/main.py", line 13, in <module>
    asyncio.run(main())
  File "/Users/person/.pyenv/versions/3.12.4/lib/python3.12/asyncio/runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/Users/person/.pyenv/versions/3.12.4/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/person/.pyenv/versions/3.12.4/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/person/project/call_stripe/main.py", line 8, in main
    async for c in await stripe.Customer.list_async().auto_paging_iter():
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'coroutine' object has no attribute 'auto_paging_iter'
sys:1: RuntimeWarning: coroutine 'Customer.list_async' was never awaited

Digging through the source code, I see .auto_paging_iter_async() exists, but it doesn't work:

import asyncio
import stripe

stripe.api_key = "secret"


async def main():
    async for c in await stripe.Customer.list_async().auto_paging_iter_async():
        print(c)


if __name__ == "__main__":
    asyncio.run(main())
Traceback (most recent call last):
  File "/Users/person/project/call_stripe/main.py", line 13, in <module>
    asyncio.run(main())
  File "/Users/person/.pyenv/versions/3.12.4/lib/python3.12/asyncio/runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "/Users/person/.pyenv/versions/3.12.4/lib/python3.12/asyncio/runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/person/.pyenv/versions/3.12.4/lib/python3.12/asyncio/base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "/Users/person/project/call_stripe/main.py", line 8, in main
    async for c in await stripe.Customer.list_async().auto_paging_iter_async():
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'coroutine' object has no attribute 'auto_paging_iter_async'
sys:1: RuntimeWarning: coroutine 'Customer.list_async' was never awaited

And it's the same if I use the StripeClient object:

import asyncio
from stripe import StripeClient

api_key = "secret"
client = StripeClient(api_key)


async def main():
    # auto_paging_iter_async() also fails
    async for c in await client.customers.list_async().auto_paging_iter():
        print(c)


if __name__ == "__main__":
    asyncio.run(main())

What am I doing wrong?

I'm using Python 3.12.4 installed with PyEnv 2.4.10 on Mac. My local Poetry environment:

[tool.poetry.dependencies]
python = ">=3.8,<3.13"
httpx = "^0.27.0"
stripe = "^10.7.0"

Solution

  • I'd suggest filing a github issue for clarification of that example and the behaviour you observe.

    In the interim, adding an await step on list_async worked for me:

    async def getCustomers():
      customers = await stripe.Customer.list_async(limit=3)
      async for customer in customers.auto_paging_iter():
        print(customer.id, customer.name)