edgedb-python icon indicating copy to clipboard operation
edgedb-python copied to clipboard

Codegen should raise an error if a variable/argument is named `query`

Open jimkring opened this issue 1 year ago • 0 comments

Describe the bug

If an .edgeql contains a variable named $query codegen should raise an error or otherwise avoid the collision with the existing query argument of the edgedb.AsyncIOExecutor.query() method (whose signature looks like this: async def query(self, query: str, *args, **kwargs) -> list:)

More Details*

See Issue #496.

I have an .edgeql that looks like this: select ext::ai::search(MyModel, <array<float32>>$query)

edgeql-py generates python code that looks like this:

async def ai_search(
    executor: edgedb.AsyncIOExecutor,
    *,
    query: list[float],
) -> list[AiSearchResult]:
    return await executor.query(
        """\
        select ext::ai::search(MyModel, <array<float32>>$query)\
        """,
        query=query,
    )

The problem is that the first argument of edgedb.AsyncIOExecutor.query() is named query, which means that the use of the named argument actually overwrites the first. For example, what is really going on is this (note that query is being passed twice).

async def ai_search(
    executor: edgedb.AsyncIOExecutor,
    *,
    query: list[float],
) -> list[AiSearchResult]:
    return await executor.query(
        query="""\
        select ext::ai::search(MyModel, <array<float32>>$query)\
        """,
        query=query,
    )

Then, I get a runtime error stating: TypeError: AsyncIOReadOnlyExecutor.query() got multiple values for argument 'query' -- it wasn't obvious to me what was going on, since my query argument was a list[float] and I thought it was a problem with it being a list. I didn't realize the root of the problem.

jimkring avatar May 26 '24 22:05 jimkring