Search code examples
pythonpostgresqlrestaiohttpasyncpg

python asyncpg TypeError: _execute() got an unexpected keyword argument 'record_class'


So I'm working on REST API using aiohttp & asyncpg. Here is my base view for handlers:

from aiohttp.web_urldispatcher import View
from asyncpgsa import PG


class BaseView(View):
    URL_PATH: str

    @property
    def pg(self) -> PG:
        return self.request.app['pg']

I'm trying to do a select query for one of my tables and fetch rows:

query = select([regions_table.c.region_id]).select_from(regions_table)
regions = await self.pg.fetch(query)

However, I get the error from the title:

File "blahblahblah/env/lib/python3.8/site-packages/asyncpg/connection.py", line 583, in fetch
    return await self._execute(
TypeError: _execute() got an unexpected keyword argument 'record_class'

My guess is that fetch and fetchrow has an argument 'record_class' when calling execute() that hasn't the argument. Here is fetch() implementation:

    async def fetch(
        self,
        query,
        *args,
        timeout=None,
        record_class=None
    ) -> list:
        
        self._check_open()
        return await self._execute(
            query,
            args,
            0,
            timeout,
            record_class=record_class,
        )

And here is _execute():

    def _execute(self, query, args, limit, timeout, return_status=False):
        query, compiled_args = compile_query(query, dialect=self._dialect)
        args = compiled_args or args
        return super()._execute(query, args, limit, timeout,
                                return_status=return_status)

But I haven't seen any issue related, and the code from the other project worked alright with the same query. Maybe I miss something on documentation or dealing with these libraries? Any advice is welcomed.


Solution

  • The problem was due to incompatibility between asyncpg and its wrapper asyncpgsa. The fetch() snippet I pasted above was from asyncpg/connection.py of asyncpg v0.22, and _execute() snippet is from asyncpgsa/connection.py of asyncpgsa v0.16.5, which is now not even a valid version. Version 0.17.0 is compatible with 0.22 asyncpg and its record_class fields, and 0.16.5 is obviously outdated. So, what I had to do is reconfiguring my requirements.txt:

    asyncpgsa==0.27.0
    setuptools~=54.1.2
    

    I'm sure anybody who make venv from skratch won't have the same problem. I used the requirements from the project that was made over half a year ago, so the incompatibility occured. Morale here is: don't trust copypasta.