Skip to content

Fix JSON type alias for datetime and UUID in TypedDicts - #1514

Closed
aydgnme wants to merge 1 commit into
supabase:mainfrom
aydgnme:fix/json-type-datetime-uuid
Closed

aydgnme wants to merge 1 commit into
supabase:mainfrom
aydgnme:fix/json-type-datetime-uuid

Conversation

@aydgnme

@aydgnme aydgnme commented Jun 10, 2026

Copy link
Copy Markdown

Problem

The JSON type alias in postgrest/types.py did not include datetime,
date, time, or UUID. The Supabase CLI generates TypedDicts that
use these types, causing type errors when passing them to .insert(),
.upsert(), or .update().

Fix

Added datetime, date, time, and UUID to the JSON type alias.
These types are already serializable by httpx/pydantic — this was purely
a type annotation gap.

Testing

All 43 existing sync unit tests pass.

Fixes #1443

Supabase CLI generates TypedDicts that include datetime and UUID fields.
These are valid serializable types (httpx handles them), but the narrow
JSON type alias caused false type errors. Fixes supabase#1443.
@o-santi

o-santi commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

These types are already serializable by httpx/pydantic — this was purely a type annotation gap.

This is wrong, and this PR does not fix the underlying issue. httpx uses json.dumps to serialize these, which does not know how to handle date, time or UUID. In order to fix this, we'd need to stop using httpx's json serialization and start serializing it on our own. This is already done in v3, so I'm just avoiding re-doing it again to not duplicate work.

@o-santi o-santi closed this Jul 9, 2026
spydon added a commit that referenced this pull request Nov 3, 2026
…d update (#1939)

## What

Fixes #1443. SDK-831.

`insert`, `upsert` and `update` were typed to take `JSON`, so the
`Insert` and `Update` TypedDicts generated by `supabase gen types --lang
python` failed type checking. This PR fixes both the types and the
runtime serialization.

## Why the previous attempt was not enough

PR #1514 added `datetime` and `UUID` to the `JSON` alias. That has two
problems:

1. mypy (and pyright) treat a TypedDict as a `Mapping[str, object]`
only, never as `Mapping[str, <recursive alias>]`, so even a widened
`JSON` still rejects generated TypedDicts. I verified this against a
CLI-style TypedDict.
2. The body was sent with httpx's `json=`, which uses `json.dumps`, so
`datetime`, `UUID` and the other generated types raised `TypeError` at
request time.

## Changes

- New `RequestBody` type alias, `JSON | Mapping[str, object] |
Sequence[Mapping[str, object]]`, used by `insert`, `upsert` and `update`
(async and sync). It is a superset of the old `JSON` annotation, so
existing callers keep type-checking.
- Request bodies are now encoded by a private `_encode_json_body` helper
in `base_request_builder` and sent as `content=`, keeping the same
compact UTF-8 output httpx produced (`ensure_ascii=False`,
`allow_nan=False`). On top of plain JSON it encodes every non-JSON type
the generator emits:

  | Python type                | Encoded as                         |
  | -------------------------- | ---------------------------------- |
  | `datetime.datetime`/`date` | ISO 8601 string                    |
  | `datetime.time`            | ISO 8601 string                    |
  | `uuid.UUID`                | canonical string                   |
  | `bytes`                    | `\x` hex string (PostgreSQL bytea) |

  Any other unsupported type still raises `TypeError`, as before.
- `Content-Type: application/json` is set when a body is sent and the
caller has not set one.
- Bulk bodies passed as any sequence (for example a tuple) now get the
`columns` query parameter, and any `Mapping` or `Sequence` is encoded,
since `RequestBody` accepts them.

## Tests

- Unit tests for `_encode_json_body` and `_is_row_list`: plain JSON
output, every generated type, other mappings and sequences (tuples,
`deque`, `MappingProxyType`, `UserDict`), unsupported types and NaN.
- Client tests that pass CLI-style `PublicEventsInsert` /
`PublicEventsUpdate` TypedDicts to `insert`, bulk `insert` (list and
tuple), `upsert` and `update`, plus an `rpc` call with UUID and date
params, and assert the encoded request body. Because mypy runs over
`tests/`, these also act as type-level regression tests.


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **New Features**
* Insert, upsert, update, and RPC requests support a wider range of
payloads, including typed mappings and sequences of rows.
* JSON request bodies can include UUIDs, dates, times, and byte-like
values, which are converted for transmission.
* Bulk inserts accept tuples and other row sequences, with columns
inferred from row records.
* **Bug Fixes**
* Custom content types are preserved, and select requests continue to
send no body.
* Unsupported values and non-finite numbers are rejected during JSON
encoding.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
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.

insert does not expect TypedDict provided by generated types

2 participants