twilio / twilio-python

A Python module for communicating with the Twilio API and generating TwiML.

Home Page:https://www.twilio.com/docs/libraries/python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Async generators are implemented incorrectly

Andrew-Chen-Wang opened this issue · comments

Issue Summary

Testing the new async client, but async generators don't seem to be working correctly.

There are a bunch of areas in the code base that await stream_async and some don't await the stream_async.

Steps to Reproduce

Just run the below code; it's a simple API call

Code Snippet

twilio_http_client = AsyncTwilioHttpClient()
twilio_client = Client(
    env.get('TWILIO_ACCOUNT_SID'),
    env.get('TWILIO_AUTH_TOKEN'),
    http_client=twilio_http_client
)

# Test 1
await twilio_client.incoming_phone_numbers.list_async()
# Test 2
[x async for x in twilio_client.incoming_phone_numbers.list_async()]
[x async for x in await twilio_client.incoming_phone_numbers.list_async()]
# Test 3
[x for x in await twilio_client.incoming_phone_numbers.list_async()]

The problem is:

TypeError: object async_generator can't be used in 'await' expression

Exception/Log

Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevconsole.py", line 367, in runcode
    loop.run_until_complete(coro)
  File "/usr/local/Cellar/python@3.10/3.10.4/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "<input>", line 1, in <module>
  File "/Users/andrewcwang/Work/Code/test/python/twilio-transcription/venv/lib/python3.10/site-packages/twilio/rest/api/v2010/account/incoming_phone_number/__init__.py", line 1324, in list_async
    await self.stream_async(
  File "/Users/andrewcwang/Work/Code/test/python/twilio-transcription/venv/lib/python3.10/site-packages/twilio/rest/api/v2010/account/incoming_phone_number/__init__.py", line 1254, in stream_async
    return await self._version.stream_async(page, limits["limit"])
TypeError: object async_generator can't be used in 'await' expression

Or if you use async for without await

Traceback (most recent call last):
  File "/Applications/PyCharm.app/Contents/plugins/python/helpers/pydev/pydevconsole.py", line 367, in runcode
    loop.run_until_complete(coro)
  File "/usr/local/Cellar/python@3.10/3.10.4/Frameworks/Python.framework/Versions/3.10/lib/python3.10/asyncio/base_events.py", line 646, in run_until_complete
    return future.result()
  File "<input>", line 1, in <module>
TypeError: 'async for' requires an object with __aiter__ method, got coroutine

Technical details:

  • twilio-python version: 8.0.0-rc.1
  • python version: 3.10

Should be fixed by #701

missed this thank you!