edgedb / edgedb-python

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

Codegen with the describe_query() API #363

Closed fantix closed 2 years ago

fantix commented 2 years ago

Please review individual commits.

~edgedb/edgedb#4325 is preferred to generate more friendly code.~

Generated sample:

from __future__ import annotations
import dataclasses
import edgedb
import enum
import typing
import uuid

MyString = str

class NoPydanticValidation:
    @classmethod
    def __get_validators__(cls):
        from pydantic.dataclasses import dataclass as pydantic_dataclass
        pydantic_dataclass(cls)
        cls.__pydantic_model__.__get_validators__ = lambda: []
        return []

class Color(enum.Enum):
    RED = "Red"
    GREEN = "Green"
    BLUE = "Blue"

@dataclasses.dataclass
class FullQueryResult(NoPydanticValidation):
    id: uuid.UUID
    mystr: MyString
    literal: int
    enumval: Color
    userstr: str
    user: list[FullQueryResultUserItem]
    num: FullQueryResultNum | None
    named: FullQueryResultNamed
    arr: list[int]
    tup: tuple[int, int, int]
    intrange: edgedb.Range[int]

class FullQueryResultNamed(typing.NamedTuple):
    a: int
    b: float

@dataclasses.dataclass
class FullQueryResultNum(NoPydanticValidation):
    id: uuid.UUID
    n: int

@dataclasses.dataclass
class FullQueryResultUserItem(NoPydanticValidation):
    id: uuid.UUID
    mobile: str
    role: FullQueryResultUserItemRole | None

@dataclasses.dataclass
class FullQueryResultUserItemRole(NoPydanticValidation):
    id: uuid.UUID
    name: str

async def full_query(
    client: edgedb.AsyncIOClient,
    arg0: str,
) -> FullQueryResult | None:
    return await client.query_single(
        """\
        select {
            mystr := <MyString>'abc',
            literal := 42,
            enumval := <Color>'Red',
            userstr := <str>$0,
            user := (select User { mobile, role: { name } }),
            num := (select {{n := 1},{},{}} limit 2),
            named := (a := 1, b := 2.2),
            arr := [1,2,3],
            tup := (1,2,3),
            intrange := range(9, 16),
        }\
        """,
        arg0,
    )
1st1 commented 2 years ago

Can you add a comment to the top of generated file like # autogenerated with {command}