Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

the sqlalchemy demo is broken with python 3.7 #317

Closed
albertwh1te opened this issue Jul 13, 2018 · 6 comments · Fixed by #403
Closed

the sqlalchemy demo is broken with python 3.7 #317

albertwh1te opened this issue Jul 13, 2018 · 6 comments · Fixed by #403

Comments

@albertwh1te
Copy link
Contributor

albertwh1te commented Jul 13, 2018

        async for row in conn.execute(tbl.select()):
            print(row.id, row.val)

the sqlalchemy demo is broken with python 3.7

TypeError: 'async for' received an object from __aiter__ that does not implement __anext__: coroutine
@albertwh1te albertwh1te changed the title the sqlalchemy demo is broken with python 3.0 the sqlalchemy demo is broken with python 3.7 Jul 13, 2018
@asvetlov
Copy link
Member

@markwh1te sorry, your fix is incorrect.
The proper fix should drop async from def __aiter__ in cursor.py

@sc-syf
Copy link

sc-syf commented Sep 14, 2018

yeah, just as @asvetlov note, with python 3.7, you should change async def __aiter__ to def __aiter__

@dnp1
Copy link

dnp1 commented Sep 25, 2018

@markwh1te

If you yield values from an "async function", an AsyncGenerator is returned and everything is fine for python 3.7

But here you intend to return an object that already implements __anext__ (self), so you must drop the async from __aiter__ as pointed above.

I'm waiting for this fix, also. Are you planning to complete it or I should open my pull request?

@albertwh1te
Copy link
Contributor Author

@dnp1 Sorry for the delay, I am pretty busy recently. I fixed this in #357.

@anyUesr
Copy link

anyUesr commented Jan 2, 2019

0.0.20 do not fix this, please recheck it:

In [15]: import aiomysql                                                                                                                                                            

In [16]: aiomysql.__version__                                                                                                                                                       
Out[16]: '0.0.20'

In [17]: async def test(): 
    ...:     async with mysql_pool.acquire() as conn: 
    ...:         async for one_feature_info_res in conn.execute(FeatureInfoT.select()): 
    ...:             print(one_feature_info_res) 
    ...:                                                                                                                                                                            

In [18]: loop.run_until_complete(test())                                                                                                                                            
/home/redis/.pyenv/versions/3.7.2/bin/ipython:3: RuntimeWarning: coroutine '_SAConnectionContextManager.__aiter__' was never awaited
  # -*- coding: utf-8 -*-
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
/home/redis/.pyenv/versions/3.7.2/bin/ipython:3: RuntimeWarning: coroutine 'SAConnection._execute' was never awaited
  # -*- coding: utf-8 -*-
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-18-66585a8d0722> in <module>
----> 1 loop.run_until_complete(test())

~/.pyenv/versions/3.7.2/lib/python3.7/asyncio/base_events.py in run_until_complete(self, future)
    582             raise RuntimeError('Event loop stopped before Future completed.')
    583 
--> 584         return future.result()
    585 
    586     def stop(self):

<ipython-input-17-fa7af2a10cfe> in test()
      1 async def test():
      2     async with mysql_pool.acquire() as conn:
----> 3         async for one_feature_info_res in conn.execute(FeatureInfoT.select()):
      4             print(one_feature_info_res)
      5 

TypeError: 'async for' received an object from __aiter__ that does not implement __anext__: coroutine

@stkrp
Copy link

stkrp commented Dec 17, 2019

Hello.

I'm facing with same problem on version 0.0.20 from master branch (not PyPI). Could the following code be the cause? I use Python 3.8 in Docker.

class _SAConnectionContextManager(_ContextManager):
    async def __aiter__(self):
        result = await self._coro
        return result

https://github.com/stkrp/aiomysql/blob/b16e5bd8f31e0644d180599c87deb9ffea0c2007/aiomysql/utils.py#L73

Workaround

import asyncio

from aiomysql.sa import create_engine


async def main():
    query = "SELECT table_name FROM information_schema.tables;"

    db = await create_engine(user="root", db="db", host="mysql", password="")

    async with db.acquire() as conn:
        cursor = await conn.execute(query)
        async for row in cursor:
            print(row)


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

UPD. I found a related issue: #410.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants