edgedb / edgedb-python

The official Python client library for EdgeDB
https://edgedb.com
Apache License 2.0
366 stars 43 forks source link

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

Open jimkring opened 1 month ago

jimkring commented 1 month ago

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.