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

Codegen with the describe_query() API #363

Merged
merged 14 commits into from
Oct 20, 2022
Merged

Codegen with the describe_query() API #363

merged 14 commits into from
Oct 20, 2022

Conversation

fantix
Copy link
Member

@fantix fantix commented Sep 5, 2022

Please review individual commits.

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

  • Prior 2.2 EdgeDB always return AT_MOST_ONE as result_cardinality for single results, so there will be a false typing.Optional even if the actual cardinality is ONE.
  • edgedb.Range is supported, but it doesn't support Pydantic/FastAPI out of the box.

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,
    )

edgedb/base_client.py Outdated Show resolved Hide resolved
edgedb/base_client.py Show resolved Hide resolved
edgedb/base_client.py Outdated Show resolved Hide resolved
@fantix fantix force-pushed the codegen-new branch 4 times, most recently from ec358ff to 813e3fc Compare September 6, 2022 16:21
edgedb/abstract.py Outdated Show resolved Hide resolved
@fantix fantix force-pushed the codegen-new branch 2 times, most recently from 29c132a to f0cf862 Compare September 7, 2022 20:18
@fantix fantix requested a review from 1st1 September 8, 2022 19:56
@fantix fantix marked this pull request as ready for review September 8, 2022 19:56
@fantix fantix changed the title [WIP] Codegen with the describe() API [Codegen with the describe() API Sep 8, 2022
@fantix fantix changed the title [Codegen with the describe() API Codegen with the describe() API Sep 8, 2022
edgedb/datatypes/array.c Outdated Show resolved Hide resolved
@fantix fantix mentioned this pull request Sep 8, 2022
edgedb/codegen/generator.py Outdated Show resolved Hide resolved
edgedb/datatypes/array.c Outdated Show resolved Hide resolved
edgedb/datatypes/object.c Outdated Show resolved Hide resolved
@fantix fantix force-pushed the codegen-new branch 6 times, most recently from 50e26c9 to ab02fc8 Compare October 1, 2022 17:22
@fantix fantix changed the title Codegen with the describe() API Codegen with the describe_query() API Oct 1, 2022
@1st1
Copy link
Member

1st1 commented Oct 4, 2022

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

@fantix fantix force-pushed the codegen-new branch 4 times, most recently from dcacf2d to e79304e Compare October 7, 2022 16:57
@fantix fantix force-pushed the codegen-new branch 2 times, most recently from 944c496 to 8b99ab5 Compare October 7, 2022 20:05
edgedb/abstract.py Outdated Show resolved Hide resolved
@fantix fantix force-pushed the codegen-new branch 2 times, most recently from d46a21b to 9bb9537 Compare October 11, 2022 15:41
@fantix fantix requested a review from elprans October 17, 2022 17:58
Copy link
Member

@1st1 1st1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Epic work Fantix. I think this is ready.

@fantix fantix merged commit 361221d into master Oct 20, 2022
@fantix fantix deleted the codegen-new branch October 20, 2022 16:37
fantix added a commit that referenced this pull request Oct 21, 2022
Changes
=======

* Implement dataclass for EdgeObject (#359)
  (by @fantix in dfb8c8b for #359)

* Redo edgedb basic types to inherit from builtin types (#366)
  (by @fantix in b11b991 for #366)

* Officially drop 3.6 support (#373)
  (by @msullivan in 7b76bc7 for #373)

* Support Python 3.11 (#375)
  (by @msullivan in 04b0da2 for #375)

* Codegen with the describe_query() API (#363)
  (by @fantix in 361221d for #363)

* Add codegen docs (#380)
  (by @colinhacks in 23dd42e for #380)

* Use typing_extension.Literal in codegen for Python 3.7
  (by @fantix in 6d0d6ab)

* Add __all__ to edgedb/__init__.py
  (by @fmoor in d3ef6d9)

Fixes
=====

* Improve duration parsing
  (by @fmoor in 241c80d)

* Tweak wording in query_single() error messages (#369)
  (by @msullivan in e24bb53 for #369)

* Fix flake tests on python3.7 (#371)
  (by @msullivan in 583e1cb for #371)

* Don't reject tuple arguments on the client side (#370)
  (by @msullivan in 09c950f for #370)

* Correct edgedb.Client.close() timeout behavior
  (by @fantix in 33a912c)

* Ping first if conn is idle for too long (#365)
  (by @fantix in 99cf78a for #365)

Deprecations
============

* Deprecate object links and simplify link property access (#379)
  (by @fantix in 2c5dcc7 for #379)
@fantix fantix mentioned this pull request Oct 21, 2022
fantix added a commit that referenced this pull request Oct 21, 2022
Changes
=======

* Implement dataclass for EdgeObject (#359)
  (by @fantix in dfb8c8b for #359)

* Redo edgedb basic types to inherit from builtin types (#366)
  (by @fantix in b11b991 for #366)

* Officially drop 3.6 support (#373)
  (by @msullivan in 7b76bc7 for #373)

* Support Python 3.11 (#375)
  (by @msullivan in 04b0da2 for #375)

* Codegen with the describe_query() API (#363)
  (by @fantix in 361221d for #363)

* Add codegen docs (#380)
  (by @colinhacks in 23dd42e for #380)

* Use typing_extension.Literal in codegen for Python 3.7
  (by @fantix in 6d0d6ab)

* Add __all__ to edgedb/__init__.py
  (by @fmoor in d3ef6d9)

Fixes
=====

* Improve duration parsing
  (by @fmoor in 241c80d)

* Tweak wording in query_single() error messages (#369)
  (by @msullivan in e24bb53 for #369)

* Fix flake tests on python3.7 (#371)
  (by @msullivan in 583e1cb for #371)

* Don't reject tuple arguments on the client side (#370)
  (by @msullivan in 09c950f for #370)

* Correct edgedb.Client.close() timeout behavior
  (by @fantix in 33a912c)

* Ping first if conn is idle for too long (#365)
  (by @fantix in 99cf78a for #365)

* Use @-prefixed keys in object codec for link properties (#384)
  (by @fantix in 68480f9 for #377)

Deprecations
============

* Deprecate object links and simplify link property access (#379)
  (by @fantix in 2c5dcc7 for #379)
@fantix fantix mentioned this pull request Oct 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants