Skip to content

fix(postgrest-typegen): python escaping, 3.9 imports, JsonValue json columns, composite nullability - #124

Merged
spydon merged 4 commits into
mainfrom
fix/typegen-python-fixes
Sep 1, 2026
Merged

spydon merged 4 commits into
mainfrom
fix/typegen-python-fixes

Conversation

@spydon

@spydon spydon commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Summary

Ports the worthwhile Python generator fixes from postgres-meta's open template PRs into this package (the templates are being deleted in favor of this package in supabase/postgres-meta#1084, so open fixes there are triaged and re-landed here). Four fixes, one commit each:

  1. Identifier escaping (from fix: escape identifiers in the Python type generator postgres-meta#1082): enum Literal labels and Field(alias=...) values were interpolated unescaped, so a quote, backslash, or newline in a database name broke the generated module. A shared escapePythonString helper (JSON escaping, a strict subset of Python's) now covers all three interpolation sites.
  2. Python 3.9/3.10 support (from fix: support Python 3.9 type generation postgres-meta#1094): NotRequired (3.11+) and TypeAlias (3.10+) now import from typing_extensions, which is always installed as a required dependency of pydantic.
  3. Deserialized json/jsonb (from fix(typegen): accept deserialized json/jsonb values in generated types for Python postgres-meta#1129): json/jsonb map to pydantic's JsonValue instead of Json[Any]. PostgREST returns these columns already deserialized, while Json[Any] validates a JSON string and parses it, so every generated model with a JSON column failed model_validate (Json columns don't deserialize correctly #250).
  4. Composite type nullability (the Python side of fix: add | null to composite type fields in TypeScript generator postgres-meta#1063, reimplemented): composite type attributes cannot carry NOT NULL constraints in Postgres, so their fields now emit Optional[...]. The origin PR's Python hunks were dead code (an unused PythonDomain class and a type-map entry for a name Postgres never emits), so the actual fix was implemented instead of ported.

Triage of origin PRs

postgres-meta PR Verdict Reasoning
supabase/supabase-py#1082 Ported Real invalid-syntax bug, correct approach.
supabase/supabase-py#1094 Ported Import failure on Python 3.9/3.10, independently verified by community comments on the PR.
supabase/supabase-py#1129 Ported Every JSON column failed validation at runtime; JsonValue is pydantic's native type for a parsed JSON value.
supabase/postgrest-py#573 (python part) Reimplemented Real bug, but the PR's Python changes did not actually fix it (dead code); the underlying fix is one line in typeToClass.
supabase/supabase-py#1072 (frozen=True) Skipped Author-labeled feature and an opinionated behavior change that breaks consumers who mutate row models; belongs behind a generator option if wanted.
supabase/supabase-py#808 Skipped 2023 draft fully superseded by the maintainer-authored template this package ports.

Validation

  • Unit tests per fix (pathological enum labels and aliases, import block assertions, json/jsonb mapping, composite Optional fields).
  • Parity golden regenerated (39 lines): the typing_extensions import split, 16 Json[Any] to JsonValue occurrences, and two composite attributes gaining Optional[...]; reviewed line by line and the golden gate was verified to actually trip on corruption.
  • The regenerated golden imports cleanly under pydantic, passes mypy, and runtime checks confirm deserialized JSON and None composite fields now validate.
  • check-types, format-and-lint, knip, build, test (97 pass, includes Docker-backed introspection and parity) all green.
  • Note: the nightly parity job against real postgres-meta will show this intentional drift until postgres-meta consumes a release containing it (feat: consume @supabase/postgrest-typegen for type generation postgres-meta#1084 replaces the templates with this package, closing the gap).

@spydon
spydon requested a review from a team as a code owner August 31, 2026 13:14
@coderabbitai

coderabbitai Bot commented Aug 31, 2026 •

Copy link
Copy Markdown

Important

  • 🔍 Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Team

Run ID: fa1d9599-5824-4833-b1ff-57c865a24284


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@sornapudisuresh

Copy link
Copy Markdown

Thank you for porting the fix and crediting PR supabase/supabase-py#1094. I’ll follow the shared type-generation implementation in supabase/supabase-py#124.

Enum labels and column aliases were interpolated into the generated
Python source unescaped, so a name containing a double quote, backslash
or newline produced invalid Python and could inject text into the
generated module. Emit them through JSON string escaping, which is a
subset of Python's string literal escaping.

Ported from supabase/postgres-meta#1082
…extensions

NotRequired only exists in typing from Python 3.11 and TypeAlias from
3.10, so the generated module failed to import on Python 3.9 and 3.10.
typing_extensions ships them for all supported versions and is already
a required dependency of pydantic, so the import is always available.

Ported from supabase/postgres-meta#1094
…thon

pydantic's Json[Any] validates a JSON string and deserializes it, but
PostgREST returns json and jsonb columns already deserialized, so every
generated model with a JSON column failed model_validate. JsonValue is
pydantic's type for an already parsed JSON value.

Ported from supabase/postgres-meta#1129
Postgres composite type attributes cannot carry NOT NULL constraints,
so every field is inherently nullable, yet the generated Python models
declared them as required non-None fields and rejected valid rows.

Ported from supabase/postgres-meta#1063 (the Python side of the same
nullability bug; the upstream PR only implemented the TypeScript part)
@spydon
spydon force-pushed the fix/typegen-python-fixes branch from 2954da8 to 76f2903 Compare September 1, 2026 08:12
@spydon
spydon merged commit d1f9b3b into main Sep 1, 2026
5 checks passed
@spydon
spydon deleted the fix/typegen-python-fixes branch September 1, 2026 09:07
spydon pushed a commit that referenced this pull request Sep 2, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.2.1](postgrest-typegen-v0.2.0...postgrest-typegen-v0.2.1)
(2026-09-02)


### Bug Fixes

* **postgrest-typegen:** escape database values in Swift string literals
([#123](#123))
([4a5cef5](4a5cef5))
* **postgrest-typegen:** escape Go struct tags for pathological column
names ([#122](#122))
([42c0249](42c0249))
* **postgrest-typegen:** generated columns, NonNullable json, zero-arg
Args, trigger-writable views
([#125](#125))
([67650c6](67650c6))
* **postgrest-typegen:** inline pg-format literal and drop the
dependency ([#138](#138))
([2c16a15](2c16a15))
* **postgrest-typegen:** python escaping, 3.9 imports, JsonValue json
columns, composite nullability
([#124](#124))
([d1f9b3b](d1f9b3b))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: supabase-releaser[bot] <223506987+supabase-releaser[bot]@users.noreply.github.com>
spydon added a commit that referenced this pull request Sep 16, 2026
…eta (#192)

## Why

The scheduled "Parity vs. real postgres-meta" job in
`postgrest-typegen-validate.yml` has failed every night since 2026-09-10
(SDK-1847). The ticket assumed postgres-meta had changed its Python
template after the cutover. It is the other way around: postgres-meta
deleted its templates in
supabase/postgres-meta#1084 (postgres-meta
v0.99.0, 2026-08-31) and now consumes `@supabase/postgrest-typegen`,
with its lockfile pinned to `0.2.0`.

The job therefore diffs `main` against our own `0.2.0` release as
installed by postgres-meta's `npm ci`. Every intentional generator
change since then shows up as "drift":

- Python `JsonValue`, `typing_extensions` imports, and `Optional[...]`
composite attributes (#124, released in 0.2.1). In the diff, the `-`
lines are postgres-meta's output and the `+` lines are ours.
- TypeScript fixed-collation ordering (#181, released in 0.2.2), which
started failing today's run as well.

There is no independent upstream left to compare against. Comparing
against postgres-meta only measures how far behind its pinned version
is, and postgres-meta's own snapshot tests cover its adapter.

## What

- Remove the `parity-check` job and the `schedule` and
`workflow_dispatch` triggers that only existed for it; the remaining
`test` job runs on push and pull requests as before.
- Delete `scripts/load-fixtures.ts`, `scripts/print-generated.ts`, and
`scripts/canonicalize-typescript.ts`, which only served that job, and
drop their knip entries.
- Replace the "Byte-Parity Constraint" section in the package
`CLAUDE.md` with a description of the post-cutover relationship (this
package is the single source of truth; output changes are behavior
changes for postgres-meta and the CLI, reviewed via the regenerated
snapshots and goldens). Adjust the README and the golden test's doc
comment to match. The `prettier` pin note was already stale since the
package has no prettier dependency.

The golden test under `test/parity/` and its `expected/` files stay as
the in-repo regression gate.

## Verification

`bun run check-types`, `bun run format-and-lint`, `bun run knip`, and
`bun run test` (134 tests, including the testcontainers-backed golden
test) pass locally.

Closes SDK-1847.
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.

3 participants