supabase_typegen 3.0.0a1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Supabase
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,108 @@
1
+ Metadata-Version: 2.4
2
+ Name: supabase_typegen
3
+ Version: 3.0.0a1
4
+ Summary: Generates the typed tables and functions of a Supabase database for supabase-py.
5
+ License-Expression: MIT
6
+ License-File: LICENSE
7
+ Classifier: Programming Language :: Python :: 3
8
+ Classifier: Programming Language :: Python :: 3.12
9
+ Classifier: Programming Language :: Python :: 3.13
10
+ Classifier: Programming Language :: Python :: 3.14
11
+ Classifier: Programming Language :: Python :: 3.15
12
+ Classifier: Operating System :: OS Independent
13
+ Requires-Dist: pydantic>=2.12,<3
14
+ Maintainer: Leonardo Santiago
15
+ Maintainer-email: Leonardo Santiago <leonardo.santiago@supabase.io>
16
+ Requires-Python: >=3.12
17
+ Project-URL: homepage, https://github.com/supabase/supabase-py/tree/main/src/typegen
18
+ Project-URL: repository, https://github.com/supabase/supabase-py
19
+ Project-URL: changelog, https://github.com/supabase/supabase-py/tree/main/CHANGELOG.md
20
+ Description-Content-Type: text/markdown
21
+
22
+ # supabase_typegen
23
+
24
+ Generates the typed tables and functions of a Supabase database for the typed
25
+ API of `postgrest` and `supabase`, so a query names its columns through
26
+ attributes the type checker knows and reads its rows as pydantic models.
27
+
28
+ For every table, view and materialized view it generates:
29
+
30
+ - a `Row` pydantic model, `BooksRow`, the row `select()` reads,
31
+ - an `Insert` and an `Update` `TypedDict`, `BooksInsert` and `BooksUpdate`,
32
+ the only values the `values()` of a typed `insert()` and `update()` accept. A
33
+ key left out of an insert gets the database default, and `None` writes
34
+ `NULL`. Columns the database generates itself are left out. A view gets
35
+ them only when it is insertable or updatable, and a materialized view never,
36
+ - a `Table` subclass, `Books`, that `client.select(Books)` reads in its own
37
+ schema, with a `Column` or `NullableColumn` per column, a `ComputedField`
38
+ per computed field, and a `ToOneRelation` or `ToManyRelation` per foreign
39
+ key and computed relationship.
40
+
41
+ Postgres enums become `StrEnum`s and composite types pydantic models. A
42
+ `bytea` column reads as `postgrest.Bytea`, `bytes` decoded from the `\x…`
43
+ hex text PostgREST sends, and takes plain `bytes` on writes and filters. A
44
+ `Functions` class has a static method per function PostgREST exposes, which
45
+ takes the arguments as keyword parameters, optional where the function has a
46
+ default, and returns the `FunctionCall` that `client.rpc()` runs.
47
+
48
+ Objects of the `public` schema are named after themselves, `books` becomes
49
+ `Books` and `BooksRow`. Objects of any other schema carry the schema as a
50
+ prefix, so `inventory.books` becomes `InventoryBooks` and `InventoryBooksRow`.
51
+ A name that is a keyword, or that the module uses itself, gets a trailing
52
+ underscore, and a column whose attribute differs from its name is sent under
53
+ its name.
54
+
55
+ ## Usage
56
+
57
+ Add `supabase_typegen` as a development dependency of your project, next to
58
+ `supabase` or `postgrest`:
59
+
60
+ ```sh
61
+ uv add --dev supabase_typegen
62
+ ```
63
+
64
+ It reads the `GeneratorMetadata` document of
65
+ [`@supabase/postgrest-typegen`](https://github.com/supabase/sdk/tree/main/packages/postgrest-typegen)
66
+ on standard input and writes the module to standard output:
67
+
68
+ ```sh
69
+ python -m supabase_typegen < generator_metadata.json > supabase_schema.py
70
+ ```
71
+
72
+ The Supabase CLI introspects the database into that document, so
73
+ `supabase gen types --lang python` runs it in your project once the CLI
74
+ supports it. Regenerate the module after every change to the database schema
75
+ and commit it. Never edit it by hand.
76
+
77
+ ```python skip
78
+ from supabase import Client
79
+
80
+ from .supabase_schema import Books, BooksInsert, Functions
81
+
82
+
83
+ def recent_books(client: Client) -> None:
84
+ books = (
85
+ client.select(Books)
86
+ .where(Books.title.like("%Python%") & ~Books.published_on.is_null())
87
+ .order(Books.created_at.desc())
88
+ .limit(10)
89
+ .execute()
90
+ )
91
+ row: BooksInsert = {"title": "A typed row", "author_id": 2}
92
+ client.insert(Books).values(row).execute()
93
+ matches = client.rpc(Functions.search_books(query="dune")).execute()
94
+ ```
95
+
96
+ ## Known limitations
97
+
98
+ - An optional function argument passed as `None` is left out, so the
99
+ function uses its default. A `NULL` for such an argument can only be sent
100
+ with `client.rpc("name", params)`.
101
+ - A model field whose attribute differs from its column name, such as
102
+ `class_` for `class`, is read and written under the column name, which
103
+ type checkers also take as its parameter name. Build such a model from its
104
+ columns: `Address.model_validate({"class": "first"})`.
105
+ - A function returning `json`, `jsonb`, a record or rows of a relation that
106
+ is not generated returns `object`.
107
+ - `numeric` is `float`, ranges and `interval` are `str`, and arrays assume
108
+ their elements are not `NULL`.
@@ -0,0 +1,87 @@
1
+ # supabase_typegen
2
+
3
+ Generates the typed tables and functions of a Supabase database for the typed
4
+ API of `postgrest` and `supabase`, so a query names its columns through
5
+ attributes the type checker knows and reads its rows as pydantic models.
6
+
7
+ For every table, view and materialized view it generates:
8
+
9
+ - a `Row` pydantic model, `BooksRow`, the row `select()` reads,
10
+ - an `Insert` and an `Update` `TypedDict`, `BooksInsert` and `BooksUpdate`,
11
+ the only values the `values()` of a typed `insert()` and `update()` accept. A
12
+ key left out of an insert gets the database default, and `None` writes
13
+ `NULL`. Columns the database generates itself are left out. A view gets
14
+ them only when it is insertable or updatable, and a materialized view never,
15
+ - a `Table` subclass, `Books`, that `client.select(Books)` reads in its own
16
+ schema, with a `Column` or `NullableColumn` per column, a `ComputedField`
17
+ per computed field, and a `ToOneRelation` or `ToManyRelation` per foreign
18
+ key and computed relationship.
19
+
20
+ Postgres enums become `StrEnum`s and composite types pydantic models. A
21
+ `bytea` column reads as `postgrest.Bytea`, `bytes` decoded from the `\x…`
22
+ hex text PostgREST sends, and takes plain `bytes` on writes and filters. A
23
+ `Functions` class has a static method per function PostgREST exposes, which
24
+ takes the arguments as keyword parameters, optional where the function has a
25
+ default, and returns the `FunctionCall` that `client.rpc()` runs.
26
+
27
+ Objects of the `public` schema are named after themselves, `books` becomes
28
+ `Books` and `BooksRow`. Objects of any other schema carry the schema as a
29
+ prefix, so `inventory.books` becomes `InventoryBooks` and `InventoryBooksRow`.
30
+ A name that is a keyword, or that the module uses itself, gets a trailing
31
+ underscore, and a column whose attribute differs from its name is sent under
32
+ its name.
33
+
34
+ ## Usage
35
+
36
+ Add `supabase_typegen` as a development dependency of your project, next to
37
+ `supabase` or `postgrest`:
38
+
39
+ ```sh
40
+ uv add --dev supabase_typegen
41
+ ```
42
+
43
+ It reads the `GeneratorMetadata` document of
44
+ [`@supabase/postgrest-typegen`](https://github.com/supabase/sdk/tree/main/packages/postgrest-typegen)
45
+ on standard input and writes the module to standard output:
46
+
47
+ ```sh
48
+ python -m supabase_typegen < generator_metadata.json > supabase_schema.py
49
+ ```
50
+
51
+ The Supabase CLI introspects the database into that document, so
52
+ `supabase gen types --lang python` runs it in your project once the CLI
53
+ supports it. Regenerate the module after every change to the database schema
54
+ and commit it. Never edit it by hand.
55
+
56
+ ```python skip
57
+ from supabase import Client
58
+
59
+ from .supabase_schema import Books, BooksInsert, Functions
60
+
61
+
62
+ def recent_books(client: Client) -> None:
63
+ books = (
64
+ client.select(Books)
65
+ .where(Books.title.like("%Python%") & ~Books.published_on.is_null())
66
+ .order(Books.created_at.desc())
67
+ .limit(10)
68
+ .execute()
69
+ )
70
+ row: BooksInsert = {"title": "A typed row", "author_id": 2}
71
+ client.insert(Books).values(row).execute()
72
+ matches = client.rpc(Functions.search_books(query="dune")).execute()
73
+ ```
74
+
75
+ ## Known limitations
76
+
77
+ - An optional function argument passed as `None` is left out, so the
78
+ function uses its default. A `NULL` for such an argument can only be sent
79
+ with `client.rpc("name", params)`.
80
+ - A model field whose attribute differs from its column name, such as
81
+ `class_` for `class`, is read and written under the column name, which
82
+ type checkers also take as its parameter name. Build such a model from its
83
+ columns: `Address.model_validate({"class": "first"})`.
84
+ - A function returning `json`, `jsonb`, a record or rows of a relation that
85
+ is not generated returns `object`.
86
+ - `numeric` is `float`, ranges and `interval` are `str`, and arrays assume
87
+ their elements are not `NULL`.
@@ -0,0 +1,80 @@
1
+ [project]
2
+ name = "supabase_typegen"
3
+ version = "3.0.0-a1"
4
+ description = "Generates the typed tables and functions of a Supabase database for supabase-py."
5
+ readme = "README.md"
6
+ license = "MIT"
7
+ license-files = ["LICENSE"]
8
+ classifiers = [
9
+ "Programming Language :: Python :: 3",
10
+ "Programming Language :: Python :: 3.12",
11
+ "Programming Language :: Python :: 3.13",
12
+ "Programming Language :: Python :: 3.14",
13
+ "Programming Language :: Python :: 3.15",
14
+ "Operating System :: OS Independent",
15
+ ]
16
+ requires-python = ">=3.12"
17
+ dependencies = ["pydantic >=2.12,<3"]
18
+
19
+ [[project.maintainers]]
20
+ name = "Leonardo Santiago"
21
+ email = "leonardo.santiago@supabase.io"
22
+
23
+ [project.urls]
24
+ homepage = "https://github.com/supabase/supabase-py/tree/main/src/typegen"
25
+ repository = "https://github.com/supabase/supabase-py"
26
+ changelog = "https://github.com/supabase/supabase-py/tree/main/CHANGELOG.md"
27
+
28
+ [dependency-groups]
29
+ tests = [
30
+ "pytest >=9.0.3",
31
+ "pytest-cov >=6.2.1",
32
+ "postgrest == 3.0.0-a1",
33
+ "supabase_utils[all] == 3.0.0-a1",
34
+ ]
35
+ lints = [
36
+ "ruff >=0.12.1",
37
+ "python-lsp-server (>=1.12.2,<2.0.0)",
38
+ "pyrefly >=1.4.0,<2.0.0",
39
+ "mypy >=2.4.1",
40
+ "pyright >=1.1.414",
41
+ "python-lsp-ruff (>=2.2.2,<3.0.0)",
42
+ ]
43
+
44
+ [[dependency-groups.dev]]
45
+ include-group = "lints"
46
+
47
+ [[dependency-groups.dev]]
48
+ include-group = "tests"
49
+
50
+ [tool.uv]
51
+ default-groups = ["dev"]
52
+
53
+ [tool.uv.sources.postgrest]
54
+ workspace = true
55
+
56
+ [tool.uv.sources.supabase-utils]
57
+ workspace = true
58
+
59
+ [tool.pytest.ini_options]
60
+ testpaths = ["tests"]
61
+ filterwarnings = ["ignore::DeprecationWarning"]
62
+
63
+ [tool.ruff]
64
+ extend = "../../pyproject.toml"
65
+ extend-exclude = ["tests/goldens"]
66
+
67
+ [tool.pyrefly]
68
+ extends = "../../pyrefly.toml"
69
+ project-includes = [
70
+ "src",
71
+ "tests",
72
+ ]
73
+ search-path = [
74
+ "src",
75
+ ".",
76
+ ]
77
+
78
+ [build-system]
79
+ requires = ["uv_build>=0.13.7,<0.14.0"]
80
+ build-backend = "uv_build"
@@ -0,0 +1,71 @@
1
+ [project]
2
+ name = "supabase_typegen"
3
+ version = "3.0.0-a1" # {x-release-please-version}
4
+ description = "Generates the typed tables and functions of a Supabase database for supabase-py."
5
+ maintainers = [
6
+ { name = "Leonardo Santiago", email = "leonardo.santiago@supabase.io" }
7
+ ]
8
+ readme = "README.md"
9
+ license = "MIT"
10
+ license-files = [ "LICENSE" ]
11
+ classifiers = [
12
+ "Programming Language :: Python :: 3",
13
+ "Programming Language :: Python :: 3.12",
14
+ "Programming Language :: Python :: 3.13",
15
+ "Programming Language :: Python :: 3.14",
16
+ "Programming Language :: Python :: 3.15",
17
+ "Operating System :: OS Independent",
18
+ ]
19
+ requires-python = ">=3.12"
20
+ dependencies = [
21
+ "pydantic >=2.12,<3",
22
+ ]
23
+
24
+ [project.urls]
25
+ homepage = "https://github.com/supabase/supabase-py/tree/main/src/typegen"
26
+ repository = "https://github.com/supabase/supabase-py"
27
+ changelog = "https://github.com/supabase/supabase-py/tree/main/CHANGELOG.md"
28
+
29
+ [dependency-groups]
30
+ tests = [
31
+ "pytest >=9.0.3",
32
+ "pytest-cov >=6.2.1",
33
+ "postgrest == 3.0.0-a1", # x-release-please-version
34
+ "supabase_utils[all] == 3.0.0-a1", # x-release-please-version
35
+ ]
36
+ lints = [
37
+ "ruff >=0.12.1",
38
+ "python-lsp-server (>=1.12.2,<2.0.0)",
39
+ "pyrefly >=1.4.0,<2.0.0",
40
+ "mypy >=2.4.1",
41
+ "pyright >=1.1.414",
42
+ "python-lsp-ruff (>=2.2.2,<3.0.0)",
43
+ ]
44
+ dev = [{ include-group = "lints" }, { include-group = "tests" }]
45
+
46
+ [tool.uv]
47
+ default-groups = [ "dev" ]
48
+
49
+ [tool.uv.sources]
50
+ postgrest = { workspace = true }
51
+ supabase-utils = { workspace = true }
52
+
53
+ [tool.pytest.ini_options]
54
+ testpaths = ["tests"]
55
+ filterwarnings = [
56
+ "ignore::DeprecationWarning", # ignore deprecation warnings globally
57
+ ]
58
+
59
+ [tool.ruff]
60
+ extend = "../../pyproject.toml"
61
+ # The goldens are generated; test_goldens.py checks them with Ruff's defaults.
62
+ extend-exclude = ["tests/goldens"]
63
+
64
+ [build-system]
65
+ requires = ["uv_build>=0.13.7,<0.14.0"]
66
+ build-backend = "uv_build"
67
+
68
+ [tool.pyrefly]
69
+ extends = "../../pyrefly.toml"
70
+ project-includes = ["src", "tests"]
71
+ search-path = ["src", "."]
@@ -0,0 +1,17 @@
1
+ """Generates the typed tables and functions of a Supabase database for the
2
+ typed API of supabase-py, from the `GeneratorMetadata` document that
3
+ `supabase gen types --lang python` pipes in."""
4
+
5
+ from __future__ import annotations
6
+
7
+ from ._generator import generate
8
+ from ._metadata import GeneratorMetadata, MetadataError, parse_generator_metadata
9
+ from .version import __version__
10
+
11
+ __all__ = [
12
+ "GeneratorMetadata",
13
+ "MetadataError",
14
+ "__version__",
15
+ "generate",
16
+ "parse_generator_metadata",
17
+ ]
@@ -0,0 +1,68 @@
1
+ """`python -m supabase_typegen`: reads a `GeneratorMetadata` document on
2
+ standard input and writes the generated module to standard output.
3
+
4
+ Exits with 64 when standard input is a terminal and with 65 when the
5
+ document is not one this version reads, or describes nothing to generate.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import argparse
11
+ import sys
12
+ from typing import Final
13
+
14
+ from ._generator import describes_nothing, generate
15
+ from ._metadata import MetadataError, parse_generator_metadata
16
+ from .version import __version__
17
+
18
+ EXIT_USAGE: Final = 64
19
+ EXIT_DATA_ERROR: Final = 65
20
+
21
+
22
+ def main(arguments: list[str] | None = None) -> int:
23
+ parser = argparse.ArgumentParser(
24
+ prog="python -m supabase_typegen",
25
+ description=(
26
+ "Generates the typed tables and functions of a Supabase database "
27
+ "from the GeneratorMetadata document that `supabase gen types "
28
+ "--lang python` pipes in on standard input, and writes the module "
29
+ "to standard output."
30
+ ),
31
+ )
32
+ parser.add_argument(
33
+ "--version", action="version", version=f"supabase_typegen {__version__}"
34
+ )
35
+ parser.parse_args(arguments)
36
+
37
+ if sys.stdin.isatty():
38
+ return _fail(
39
+ EXIT_USAGE,
40
+ "Expected a GeneratorMetadata document on standard input. Run "
41
+ "`supabase gen types --lang python` to generate types.",
42
+ )
43
+ try:
44
+ metadata = parse_generator_metadata(sys.stdin.buffer.read())
45
+ except MetadataError as error:
46
+ return _fail(EXIT_DATA_ERROR, str(error))
47
+ if metadata.rpc_functions is None and metadata.functions:
48
+ sys.stderr.write(
49
+ 'The document has no "rpcFunctions" collection, so no function '
50
+ "calls are generated. Upgrade the Supabase CLI to generate them.\n"
51
+ )
52
+ if describes_nothing(metadata):
53
+ return _fail(
54
+ EXIT_DATA_ERROR,
55
+ "The document describes no tables, views, enums or functions, so "
56
+ "there is nothing to generate.",
57
+ )
58
+ sys.stdout.write(generate(metadata))
59
+ return 0
60
+
61
+
62
+ def _fail(exit_code: int, message: str) -> int:
63
+ sys.stderr.write(f"{message}\n")
64
+ return exit_code
65
+
66
+
67
+ if __name__ == "__main__":
68
+ sys.exit(main())