Skip to content

Conversation

o-santi
Copy link
Contributor

@o-santi o-santi commented Sep 26, 2025

What kind of change does this PR introduce?

Fix type definitions enough for mypy with a weak ruleset to accept them.

What is the current behavior?

mypy is not happy.

$ uv run --package supabase_functions mypy src/postgrest tests
(...)
Found 58 errors in 8 files (checked 30 source files)

What is the new behavior?

mypy happy.

$ uv run --package supabase_functions mypy src/postgrest tests
Success: no issues found in 30 source files

Also improved src/postgrest/Makefile and fixed some stuff like make build-sync that was broken.

Additional context

I had to remove all generics from the response type, and from all the builders that used it, as its usage is completely wrong. I believe the idea for it is based on Swift, where the types used can be used to monomorphize the implementation to one that validates form JSON to the type itself, which is great.

Sadly, not only the current python implementation did not do that, but python as a whole is unable to leverage any smart implementation, as the types cannot contribute with computational content, and thus we cannot use the type declared at the class to decode the payload.

Moreover, in the actual implementation, the only thing we did was decode the response as JSON, by way of response.json(), which returns Any type. This means that any type was passable in the generic, and the type checker wouldn't complain, making the types unsound and the type checker useless. This also means that this whole change is non breaking, as any user relying on these generics is by definition incorrect.

The way to fix this was to downgrade the generic types to a less informative JSON type, which is just a way of saying that the best we can do is ensure the data is JSON. We can think of better ways to improve it further by using pydantic classes, but I believe those would require breaking changes.

the generics usage is completely wrong, and had to be entirely
removed. the idea was to make the type use the generic to know how to
validate the payload, but that does not work in python, as type hints
cannot contribute with computational content, and there is no
monomorphization of the calls.

instead, we use a simple JSON type that reflects the fact that the
best information we can know about the result of a query is that it
returns valid json, and nothing more.
@o-santi o-santi changed the title feat(postgrest): mypy postgrest feat: mypy postgrest Sep 29, 2025
@o-santi o-santi merged commit 82f60b9 into supabase:main Sep 29, 2025
9 checks passed
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.

2 participants