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.2.0.tar.gz (37.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.2.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: gel_pydantic_codegen-1.2.0.tar.gz
  • Upload date:
  • Size: 37.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for gel_pydantic_codegen-1.2.0.tar.gz
Algorithm Hash digest
SHA256 8f7d790be411dd47d9cbfbf06ef46340317a105280961fd93036525eb47aa0e3
MD5 ca46b47a57e8db4d54ffc65fb3683c3b
BLAKE2b-256 26a5fa25ab80bac5d18236c55272f59c4689681bf871e4d7267c0b14bd9c5021

See more details on using hashes here.

File details

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

File metadata

  • Download URL: gel_pydantic_codegen-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.2 {"installer":{"name":"uv","version":"0.10.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for gel_pydantic_codegen-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d81e49171d5575e1612507e16689bb41b6030b7ec26774bb9233ebd3bee8194f
MD5 299f01393ec40fa73d815794a4815c70
BLAKE2b-256 c68a608f0d53fefd1cebccb775050d11d1f5b8c70d07aadd18048540496f0eb6

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