Skip to main content

Alternative Python Gel code generator (Pydantic V2 + asyncio, FastAPI compatible)

Project description

gel-pydantic-codegen

This tool generates Python typesafe async code for EdgeQL queries using Pydantic V2.

The generated models can be directly used with other libraries such as FastAPI.

This is an alternative to the built-in code generator of the official gel-python library.

For legacy EdgeDB <v6 support, check the last edgedb-pydantic-codegen release and its PyPI page.

Usage

In an Gel initialized project, simply run

uvx gel-pydantic-codegen <directory>

All *.edgeql files in <directory> and its subdirectories will be processed and the generated code saved next to them.

Generated code example

from enum import StrEnum

from gel import AsyncIOExecutor
from pydantic import BaseModel, TypeAdapter

EDGEQL_QUERY = r"""
with
  discord_id := <int64>$discord_id,
  moecoins := <optional int32>$moecoins,
  blood_shards := <optional int32>$blood_shards,
  updated := (
    update waicolle::Player
    filter .client = global client and .user.discord_id = discord_id
    set {
      moecoins := .moecoins + (moecoins ?? 0),
      blood_shards := .blood_shards + (blood_shards ?? 0),
    }
  )
select updated {
  game_mode,
  moecoins,
  blood_shards,
  user: {
    discord_id,
    discord_id_str,
  },
}
"""


class WaicolleGameMode(StrEnum):
    WAIFU = "WAIFU"
    HUSBANDO = "HUSBANDO"
    ALL = "ALL"


class PlayerAddCoinsResultUser(BaseModel):
    discord_id: int
    discord_id_str: str


class PlayerAddCoinsResult(BaseModel):
    game_mode: WaicolleGameMode
    moecoins: int
    blood_shards: int
    user: PlayerAddCoinsResultUser


adapter = TypeAdapter(PlayerAddCoinsResult | None)


async def player_add_coins(
    executor: AsyncIOExecutor,
    *,
    discord_id: int,
    moecoins: int | None = None,
    blood_shards: int | None = None,
) -> PlayerAddCoinsResult | None:
    resp = await executor.query_single_json(
        EDGEQL_QUERY,
        discord_id=discord_id,
        moecoins=moecoins,
        blood_shards=blood_shards,
    )
    return adapter.validate_json(resp, strict=False)

Caveats

Currently this tool does not support:

  • TupleType, RangeType and MultiRangeType collections
  • std::duration, cal::relative_duration, cal::date_duration, cfg::memory and ext::pgvector::vector objects

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gel_pydantic_codegen-1.1.0.tar.gz (29.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gel_pydantic_codegen-1.1.0-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file gel_pydantic_codegen-1.1.0.tar.gz.

File metadata

File hashes

Hashes for gel_pydantic_codegen-1.1.0.tar.gz
Algorithm Hash digest
SHA256 dc9a72e060715d7dd31a571721cda872cde454429732b25e22652bdb2f2fe985
MD5 2ac3f3eb0fda56e0c572df2b6578c96f
BLAKE2b-256 9484d587d7545c52ef0d98a27b358ccc10f53bc188dbf05b5acfbf0a3df8b6d9

See more details on using hashes here.

File details

Details for the file gel_pydantic_codegen-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for gel_pydantic_codegen-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a3ea42b51dd6cb92b03bdcbb58f6d466b13d8296eaf52c811873f5c62653fccf
MD5 3331a7a6ac1071992deb79acea2bc422
BLAKE2b-256 2ad04a563a7d5493dc792e0aa96045b3bb13cd7bb72a2f01f085b3a3d7871a89

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page