Stripe Python 库中的示例似乎不起作用。 自述文件说:
# .auto_paging_iter() implements both AsyncIterable and Iterable
async for c in await stripe.Customer.list_async().auto_paging_iter():
....
我尝试在本地运行它:
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())
但是我收到错误和警告:
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
深入研究源代码,我看到
.auto_paging_iter_async()
存在,但它不起作用:
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
如果我使用
StripeClient
对象也是一样的:
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())
我做错了什么?
我在 Mac 上使用与 PyEnv 2.4.10 一起安装的 Python 3.12.4。我当地的诗歌环境:
[tool.poetry.dependencies]
python = ">=3.8,<3.13"
httpx = "^0.27.0"
stripe = "^10.7.0"
我建议提交一个 github 问题来澄清该示例和您观察到的行为。
在此期间,在
await
上添加 list_async
步骤对我有用:
async def getCustomers():
customers = await stripe.Customer.list_async(limit=3)
async for customer in customers.auto_paging_iter():
print(customer.id, customer.name)