sqlty 0.1.0__cp315-cp315-win_amd64.whl
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.
- sqlty/__init__.py +99 -0
- sqlty/__main__.py +5 -0
- sqlty/_breaking.py +191 -0
- sqlty/_check.py +880 -0
- sqlty/_cli.py +1219 -0
- sqlty/_config.py +666 -0
- sqlty/_core.cp315-win_amd64.pyd +0 -0
- sqlty/_core.pyi +108 -0
- sqlty/_decode.py +62 -0
- sqlty/_drivers.py +274 -0
- sqlty/_gitignore.py +58 -0
- sqlty/_helpers.py +613 -0
- sqlty/_json_shape.py +299 -0
- sqlty/_json_types.py +225 -0
- sqlty/_lint.py +350 -0
- sqlty/_params.py +177 -0
- sqlty/_psycopg.py +269 -0
- sqlty/_source.py +654 -0
- sqlty/_type_extractor/__init__.py +51 -0
- sqlty/_type_extractor/ast_fields.py +823 -0
- sqlty/_type_extractor/compat.py +252 -0
- sqlty/_type_extractor/extract.py +179 -0
- sqlty/_type_extractor/generic.py +95 -0
- sqlty/_type_extractor/info.py +161 -0
- sqlty/_type_extractor/sandbox.py +385 -0
- sqlty/_type_extractor/subtype.py +156 -0
- sqlty/_widening.py +89 -0
- sqlty/core.py +566 -0
- sqlty/drivers/psycopg.py +104 -0
- sqlty/magic.py +126 -0
- sqlty/py.typed +0 -0
- sqlty/rules/ambiguous-column.md +48 -0
- sqlty/rules/column-count-mismatch.md +30 -0
- sqlty/rules/column-name-mismatch.md +34 -0
- sqlty/rules/column-type-mismatch.md +75 -0
- sqlty/rules/fstring-skipped.md +23 -0
- sqlty/rules/inline-sql-comment.md +27 -0
- sqlty/rules/insert-missing-column.md +58 -0
- sqlty/rules/json-shape-mismatch.md +96 -0
- sqlty/rules/literal-mismatch.md +83 -0
- sqlty/rules/mysql-sql-marker.md +32 -0
- sqlty/rules/nullable-mismatch.md +76 -0
- sqlty/rules/param-count-mismatch.md +66 -0
- sqlty/rules/param-type-mismatch.md +73 -0
- sqlty/rules/redundant-literal.md +68 -0
- sqlty/rules/redundant-optional.md +100 -0
- sqlty/rules/select-star-no-schema.md +24 -0
- sqlty/rules/select-star.md +77 -0
- sqlty/rules/stale-rewrite.md +33 -0
- sqlty/rules/stale-type.md +50 -0
- sqlty/rules/tstring-skipped.md +68 -0
- sqlty/rules/typeddict-inheritance.md +35 -0
- sqlty/rules/typeddict-key-mismatch.md +38 -0
- sqlty/rules/unchecked-query.md +34 -0
- sqlty/rules/ungrouped-column.md +67 -0
- sqlty/rules/unknown-column.md +68 -0
- sqlty/rules/unknown-function.md +49 -0
- sqlty/rules/unknown-table.md +47 -0
- sqlty/rules/unknown-type.md +74 -0
- sqlty/rules/unparsed-query.md +46 -0
- sqlty/rules/unsupported-syntax.md +69 -0
- sqlty/rules/untyped-query.md +39 -0
- sqlty/rules/unused-ignore.md +36 -0
- sqlty/skill/SKILL.md +110 -0
- sqlty-0.1.0.dist-info/METADATA +210 -0
- sqlty-0.1.0.dist-info/RECORD +71 -0
- sqlty-0.1.0.dist-info/WHEEL +4 -0
- sqlty-0.1.0.dist-info/entry_points.txt +2 -0
- sqlty-0.1.0.dist-info/licenses/LICENSE +21 -0
- sqlty-0.1.0.dist-info/sboms/sqlty-core.cyclonedx.json +1891 -0
- sqlty_magic.pth +1 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# typeddict-key-mismatch
|
|
2
|
+
|
|
3
|
+
Default severity: **error**
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
Matches query columns to the fields of a TypedDict, dataclass or Pydantic
|
|
8
|
+
model by name, as `dict_row` and `class_row` do: every required field must be
|
|
9
|
+
selected, and columns without a field are allowed only through
|
|
10
|
+
[PEP 728](https://peps.python.org/pep-0728/) `extra_items=...` or a Pydantic
|
|
11
|
+
model's `extra="allow"`. `NotRequired` keys and fields with a default may be
|
|
12
|
+
missing.
|
|
13
|
+
|
|
14
|
+
## Why is this bad?
|
|
15
|
+
|
|
16
|
+
A row factory that passes columns by name fails on a missing required field
|
|
17
|
+
and on a column the type does not declare. A Pydantic model drops an undeclared column by
|
|
18
|
+
default, so the query fetches data, possibly sensitive, that is never used.
|
|
19
|
+
|
|
20
|
+
## Example
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
from typing import NotRequired, TypedDict
|
|
24
|
+
|
|
25
|
+
from sqlty import SQL
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class User(TypedDict, closed=True):
|
|
29
|
+
id: int
|
|
30
|
+
name: str
|
|
31
|
+
bio: NotRequired[str]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
# error: [typeddict-key-mismatch] "User.name is required but missing from query"
|
|
35
|
+
missing = SQL[User]("--sql\nSELECT id::INTEGER, bio::TEXT FROM users")
|
|
36
|
+
# error: [typeddict-key-mismatch] "User has no key for query column 'email'"
|
|
37
|
+
extra = SQL[User]("--sql\nSELECT id::INTEGER, name::TEXT, email::TEXT FROM users")
|
|
38
|
+
```
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# unchecked-query
|
|
2
|
+
|
|
3
|
+
Default severity: **warn**
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
Flags `--sql` queries that are not plain string literals: a raw or bytes
|
|
8
|
+
string, alone or implicitly concatenated with others, or a `SQL[T](...)`
|
|
9
|
+
argument that holds anything besides string literals. `SQL[Any](...)`, with
|
|
10
|
+
`typing.Any` under any import name, is not flagged, nor is a psycopg
|
|
11
|
+
composition passed to a helper whose row type is `Any`.
|
|
12
|
+
|
|
13
|
+
## Why is this bad?
|
|
14
|
+
|
|
15
|
+
sqlty reads queries from the source text, so it neither rewrites nor checks
|
|
16
|
+
these: the row type is never inferred or compared.
|
|
17
|
+
|
|
18
|
+
## Example
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
from sqlty import SQL
|
|
22
|
+
|
|
23
|
+
# error: [unchecked-query] "--sql query is not plain string literals, so it is not checked or rewritten"
|
|
24
|
+
q = SQL[tuple[int]]("--sql\nSELECT id::INTEGER FROM " + table)
|
|
25
|
+
# error: [unchecked-query] "--sql query is not plain string literals, so it is not checked or rewritten"
|
|
26
|
+
r = r"--sql\nSELECT id::INTEGER FROM users WHERE name ~ '\d'"
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Fix
|
|
30
|
+
|
|
31
|
+
Write the query as one plain literal, or as implicitly concatenated plain
|
|
32
|
+
literals, and pass values as query parameters. A query that must stay dynamic
|
|
33
|
+
can opt out with `SQL[Any](...)`, or `Any` as the row type of a helper called
|
|
34
|
+
with a psycopg composition.
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# ungrouped-column
|
|
2
|
+
|
|
3
|
+
Default severity: **error**
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
Reports a column that a grouped query selects without grouping by it or
|
|
8
|
+
aggregating it. A query is grouped by a `GROUP BY`, or by an aggregate such as
|
|
9
|
+
`count(*)` in its select list.
|
|
10
|
+
|
|
11
|
+
```toml
|
|
12
|
+
[tool.sqlty]
|
|
13
|
+
schema = "schema.sql"
|
|
14
|
+
|
|
15
|
+
[tool.sqlty.rules]
|
|
16
|
+
ungrouped-column = "warn"
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
`schema.sql`:
|
|
20
|
+
|
|
21
|
+
```sql
|
|
22
|
+
CREATE TABLE orders (id INTEGER PRIMARY KEY, status TEXT NOT NULL, total INTEGER NOT NULL);
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
from sqlty import SQL
|
|
27
|
+
|
|
28
|
+
# error: [ungrouped-column] "column `orders.total` must appear in GROUP BY or be used in an aggregate function"
|
|
29
|
+
q = SQL[tuple[str, int]]("--sql\nSELECT status, total FROM orders GROUP BY status")
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
A column is grouped when a key names it, by position (`GROUP BY 1`) or by its
|
|
33
|
+
result alias too, or when a key is the expression it sits in. Grouping by a
|
|
34
|
+
table's primary key covers the table's other columns, as PostgreSQL's functional
|
|
35
|
+
dependency does:
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
from sqlty import SQL
|
|
39
|
+
|
|
40
|
+
# error:
|
|
41
|
+
by_id = SQL[tuple[int, str, int]]("--sql\nSELECT id, status, count(*) FROM orders GROUP BY id")
|
|
42
|
+
by_position = SQL[tuple[str, int]]("--sql\nSELECT status, count(*) FROM orders GROUP BY 1")
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Why is this bad?
|
|
46
|
+
|
|
47
|
+
PostgreSQL rejects the query: `column "orders.total" must appear in the GROUP BY
|
|
48
|
+
clause or be used in an aggregate function`. Each group has one row in the
|
|
49
|
+
result, so a column that varies within a group has no single value.
|
|
50
|
+
|
|
51
|
+
## How to fix
|
|
52
|
+
|
|
53
|
+
Group by the column, or aggregate it: `sum(total)`, `max(total)`.
|
|
54
|
+
|
|
55
|
+
## Configuration
|
|
56
|
+
|
|
57
|
+
An error by default: the database rejects such SQL. Lower or turn it off:
|
|
58
|
+
|
|
59
|
+
```toml
|
|
60
|
+
[tool.sqlty.rules]
|
|
61
|
+
ungrouped-column = "warn"
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Needs a [schema](../../../docs/guide/schema-inference.md): only columns of
|
|
65
|
+
schema tables are judged. Judged in the `postgres` dialect; MySQL's
|
|
66
|
+
`ONLY_FULL_GROUP_BY` follows more dependencies (unique keys, equalities in
|
|
67
|
+
`WHERE`) and is not modelled.
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# unknown-column
|
|
2
|
+
|
|
3
|
+
Default severity: **error**
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
Reports a column that the table it is read from does not have: in the select
|
|
8
|
+
list, `WHERE` and `JOIN ... ON`, and in what `INSERT`, `UPDATE` and `DELETE`
|
|
9
|
+
write and read. The table must be known: a column of an unknown table is
|
|
10
|
+
covered by `unknown-table` instead.
|
|
11
|
+
|
|
12
|
+
```toml
|
|
13
|
+
[tool.sqlty]
|
|
14
|
+
schema = "schema.sql"
|
|
15
|
+
|
|
16
|
+
[tool.sqlty.rules]
|
|
17
|
+
unknown-column = "warn"
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
`schema.sql`:
|
|
21
|
+
|
|
22
|
+
```sql
|
|
23
|
+
CREATE TABLE users (id INTEGER, name TEXT);
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
```python
|
|
27
|
+
from sqlty import SQL
|
|
28
|
+
|
|
29
|
+
# error: [unknown-column] "column `email` does not exist in `users`"
|
|
30
|
+
q = SQL[tuple[int, str]]("--sql\nSELECT id, email FROM users")
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Composite fields
|
|
34
|
+
|
|
35
|
+
A field of a composite type the schema declares is checked the same way.
|
|
36
|
+
|
|
37
|
+
`schema.sql`:
|
|
38
|
+
|
|
39
|
+
```sql
|
|
40
|
+
CREATE TYPE address AS (street TEXT, zip TEXT);
|
|
41
|
+
CREATE TABLE users (id INTEGER, home address);
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
```python
|
|
45
|
+
from sqlty import SQL
|
|
46
|
+
|
|
47
|
+
# error: [unknown-column] "type `address` has no field `city`"
|
|
48
|
+
q = SQL[tuple[int, str]]("--sql\nSELECT id, (home).city FROM users")
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
## Why is this bad?
|
|
52
|
+
|
|
53
|
+
An unknown column has no type, so its annotation is not checked, and the query fails when it runs.
|
|
54
|
+
|
|
55
|
+
## How to fix
|
|
56
|
+
|
|
57
|
+
Fix the name or add the column to the schema file. Columns of CTEs and derived tables, `USING`/`NATURAL` join columns, outer-query columns in correlated subqueries and `LATERAL`, and whole-row references (`SELECT u FROM users u`) are resolved and not reported. A column that may come from a table function or another source whose columns are not read is reported as `unsupported-syntax`. To accept one case, put `# sqlty: ignore[unknown-column]` on its line.
|
|
58
|
+
|
|
59
|
+
## Configuration
|
|
60
|
+
|
|
61
|
+
An error by default: the database rejects such SQL. Lower or turn it off:
|
|
62
|
+
|
|
63
|
+
```toml
|
|
64
|
+
[tool.sqlty.rules]
|
|
65
|
+
unknown-column = "warn"
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
Only checked when a schema with tables is configured. Columns are resolved in the select list, `WHERE`, `JOIN ... ON`, `GROUP BY`, `HAVING`, `ORDER BY`, and what `INSERT`, `UPDATE` and `DELETE` write; a result column's alias or position in `GROUP BY` and `ORDER BY` is not a table column. Applies to the `postgres`, `mysql` and `sqlite` dialects.
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# unknown-function
|
|
2
|
+
|
|
3
|
+
Default severity: **warn**
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
Reports a function call whose result sqlty cannot type because it does not know the function. The column is left untyped.
|
|
8
|
+
|
|
9
|
+
```toml
|
|
10
|
+
[tool.sqlty]
|
|
11
|
+
schema = "schema.sql"
|
|
12
|
+
|
|
13
|
+
[tool.sqlty.rules]
|
|
14
|
+
unknown-function = "warn"
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`schema.sql`:
|
|
18
|
+
|
|
19
|
+
```sql
|
|
20
|
+
CREATE TABLE users (id INTEGER, name TEXT);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
from sqlty import SQL
|
|
25
|
+
|
|
26
|
+
# error: [unknown-function] "function `my_udf` is not known to sqlty"
|
|
27
|
+
q = SQL[tuple[int]]("--sql\nSELECT my_udf(id) FROM users")
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Why is this bad?
|
|
31
|
+
|
|
32
|
+
A function sqlty does not know gives the column no type, so the annotation is not checked.
|
|
33
|
+
|
|
34
|
+
## How to fix
|
|
35
|
+
|
|
36
|
+
Cast the call, `my_udf(1)::INTEGER`, which also types it. A built-in function missing from sqlty's table is a gap in sqlty: please report it. To accept one case, put `# sqlty: ignore[unknown-function]` on its line.
|
|
37
|
+
|
|
38
|
+
## Configuration
|
|
39
|
+
|
|
40
|
+
A warning by default: the SQL is valid, but sqlty cannot type the function's result, so the query is
|
|
41
|
+
not fully checked. A warning tells you why without failing CI on sqlty's gaps. Make it
|
|
42
|
+
fail, or turn it off:
|
|
43
|
+
|
|
44
|
+
```toml
|
|
45
|
+
[tool.sqlty.rules]
|
|
46
|
+
unknown-function = "error"
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Only checked when a schema with tables is configured: without one every user function would be unknown. A function the schema declares with `CREATE FUNCTION` is known.
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# unknown-table
|
|
2
|
+
|
|
3
|
+
Default severity: **error**
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
Reports a query that reads a table the configured schema does not define: a `FROM` or `JOIN` item, a DML target with `RETURNING`, or a qualifier (`x.id`, `x.*`) that names no table or alias in the query.
|
|
8
|
+
|
|
9
|
+
```toml
|
|
10
|
+
[tool.sqlty]
|
|
11
|
+
schema = "schema.sql"
|
|
12
|
+
|
|
13
|
+
[tool.sqlty.rules]
|
|
14
|
+
unknown-table = "warn"
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`schema.sql`:
|
|
18
|
+
|
|
19
|
+
```sql
|
|
20
|
+
CREATE TABLE users (id INTEGER, name TEXT);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
from sqlty import SQL
|
|
25
|
+
|
|
26
|
+
# error: [unknown-table] "table `user` is not defined in the schema"
|
|
27
|
+
q = SQL[tuple[int]]("--sql\nSELECT id FROM user")
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Why is this bad?
|
|
31
|
+
|
|
32
|
+
A column of an unknown table has no type, so the annotation of the row is not checked. A typo in a table name, or a table missing from the schema dump, hides every column below it.
|
|
33
|
+
|
|
34
|
+
## How to fix
|
|
35
|
+
|
|
36
|
+
Fix the name, or add the table to the schema file. CTEs, derived tables, views in the schema, table functions (`generate_series`, `unnest`), PostgreSQL system catalogs (`pg_catalog`, `information_schema`, `pg_*`) and DML `FROM`/`USING` sources are not reported. To accept one case, put `# sqlty: ignore[unknown-table]` on its line.
|
|
37
|
+
|
|
38
|
+
## Configuration
|
|
39
|
+
|
|
40
|
+
An error by default: the database rejects such SQL. Lower or turn it off:
|
|
41
|
+
|
|
42
|
+
```toml
|
|
43
|
+
[tool.sqlty.rules]
|
|
44
|
+
unknown-table = "warn"
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Only checked when a schema with tables is configured: without one every table is unknown, so the rule stays silent. Applies to the `postgres`, `mysql` and `sqlite` dialects. Case is folded as the database does, so `Users` and `users` are one table.
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# unknown-type
|
|
2
|
+
|
|
3
|
+
Default severity: **warn**
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
Reports a type that has no Python type: the target of a cast (`x::mood`), or the declared type of a schema column (`geometry`). Enum and domain types declared in the schema are known.
|
|
8
|
+
|
|
9
|
+
A column the rule reports is untyped because of its type, not because sqlty
|
|
10
|
+
cannot type the expression: an unknown function is
|
|
11
|
+
[`unknown-function`](unknown-function.md), and another construct
|
|
12
|
+
[`unsupported-syntax`](unsupported-syntax.md).
|
|
13
|
+
|
|
14
|
+
```toml
|
|
15
|
+
[tool.sqlty]
|
|
16
|
+
schema = "schema.sql"
|
|
17
|
+
|
|
18
|
+
[tool.sqlty.rules]
|
|
19
|
+
unknown-type = "warn"
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
`schema.sql`:
|
|
23
|
+
|
|
24
|
+
```sql
|
|
25
|
+
CREATE TABLE places (id INTEGER, shape GEOMETRY);
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
from sqlty import SQL
|
|
30
|
+
|
|
31
|
+
# error: [unknown-type] "column `places.shape` has type `GEOMETRY`"
|
|
32
|
+
q = SQL[tuple[int, str]]("--sql\nSELECT id, shape FROM places")
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Built-in types
|
|
36
|
+
|
|
37
|
+
A built-in type sqlty does not map, such as a PostgreSQL range or geometric type,
|
|
38
|
+
is reported without a schema too, since no schema can declare it:
|
|
39
|
+
|
|
40
|
+
```toml
|
|
41
|
+
[tool.sqlty]
|
|
42
|
+
dialect = "postgres"
|
|
43
|
+
|
|
44
|
+
[tool.sqlty.rules]
|
|
45
|
+
unknown-type = "warn"
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
from sqlty import SQL
|
|
50
|
+
|
|
51
|
+
# error: [unknown-type] "type `INT4RANGE` has no Python type"
|
|
52
|
+
q = SQL[tuple[object]]("--sql\nSELECT CAST(x AS INT4RANGE) AS r")
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Why is this bad?
|
|
56
|
+
|
|
57
|
+
A column of an unmapped type is untyped, so its annotation is not checked.
|
|
58
|
+
|
|
59
|
+
## How to fix
|
|
60
|
+
|
|
61
|
+
Cast to a type sqlty maps: `shape::TEXT`. Declare the type in the schema file if it is an enum or domain. To accept one case, put `# sqlty: ignore[unknown-type]` on its line.
|
|
62
|
+
|
|
63
|
+
## Configuration
|
|
64
|
+
|
|
65
|
+
A warning by default: the SQL is valid, but sqlty cannot map the type to a Python type, so the query is
|
|
66
|
+
not fully checked. A warning tells you why without failing CI on sqlty's gaps. Make it
|
|
67
|
+
fail, or turn it off:
|
|
68
|
+
|
|
69
|
+
```toml
|
|
70
|
+
[tool.sqlty.rules]
|
|
71
|
+
unknown-type = "error"
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Without a schema with tables only built-in types are checked: every enum and domain would be unknown. A cast to an enum is reported until the enum is declared in the schema.
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# unparsed-query
|
|
2
|
+
|
|
3
|
+
Default severity: **warn**
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
Reports a query that does not parse in the configured dialect: a `--sql`
|
|
8
|
+
string, the argument of `SQL[T](...)`, or a string passed to an
|
|
9
|
+
`Annotated[LiteralString, SQL]` parameter. The message quotes sqlparser's
|
|
10
|
+
error, which points at the token it could not read.
|
|
11
|
+
|
|
12
|
+
```toml
|
|
13
|
+
[tool.sqlty.rules]
|
|
14
|
+
unparsed-query = "warn"
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
```python
|
|
18
|
+
from sqlty import SQL
|
|
19
|
+
|
|
20
|
+
# error: [unparsed-query] "query does not parse: Expected: ), found: foo"
|
|
21
|
+
q = SQL[tuple[str]]("--sql\nSELECT xmlelement(name foo)")
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Why is this bad?
|
|
25
|
+
|
|
26
|
+
A query sqlty cannot parse gets no row type, so its annotation is not checked
|
|
27
|
+
against the columns and a rewrite skips it. Once a query is unparsed, the
|
|
28
|
+
coverage gap is silent unless this rule is on. `sqlty check --statistics`
|
|
29
|
+
counts such queries as `unparsed` either way.
|
|
30
|
+
|
|
31
|
+
## How to fix
|
|
32
|
+
|
|
33
|
+
Rewrite the query in a form the parser reads, or leave it: a query sqlparser
|
|
34
|
+
does not support in your dialect stays unchecked. To accept one case, put
|
|
35
|
+
`# sqlty: ignore[unparsed-query]` on its line.
|
|
36
|
+
|
|
37
|
+
## Configuration
|
|
38
|
+
|
|
39
|
+
A warning by default: sqlty's parser may not read SQL your database accepts, so the
|
|
40
|
+
warning lists the queries sqlty cannot check without failing CI. Make it fail, or turn
|
|
41
|
+
it off:
|
|
42
|
+
|
|
43
|
+
```toml
|
|
44
|
+
[tool.sqlty.rules]
|
|
45
|
+
unparsed-query = "ignore"
|
|
46
|
+
```
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# unsupported-syntax
|
|
2
|
+
|
|
3
|
+
Default severity: **warn**
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
Reports a query that parses, but uses a construct sqlty does not type: an expression it has no rule for, `VALUES` outside PostgreSQL (unless it is the source of an `INSERT` without `RETURNING`, where nothing reads its types), `*` over a `USING` or `NATURAL` join, a column of a table function or of a `FROM`/`USING` source of a DML statement, branches of a set operation that disagree on a type. The result is not trusted, so the column stays untyped.
|
|
8
|
+
|
|
9
|
+
```toml
|
|
10
|
+
[tool.sqlty]
|
|
11
|
+
schema = "schema.sql"
|
|
12
|
+
|
|
13
|
+
[tool.sqlty.rules]
|
|
14
|
+
unsupported-syntax = "warn"
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`schema.sql`:
|
|
18
|
+
|
|
19
|
+
```sql
|
|
20
|
+
CREATE TABLE users (id INTEGER, name TEXT);
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```python
|
|
24
|
+
from sqlty import SQL
|
|
25
|
+
|
|
26
|
+
# error: [unsupported-syntax] "expression `1 OPERATOR(pg_catalog.+) 1` is not typed"
|
|
27
|
+
q = SQL[tuple[object]]("--sql\nSELECT 1 OPERATOR(pg_catalog.+) 1")
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
### `INSERT ... VALUES` outside PostgreSQL
|
|
31
|
+
|
|
32
|
+
```toml
|
|
33
|
+
[tool.sqlty]
|
|
34
|
+
dialect = "sqlite"
|
|
35
|
+
schema = "schema.sql"
|
|
36
|
+
|
|
37
|
+
[tool.sqlty.rules]
|
|
38
|
+
stale-rewrite = "ignore"
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
# error:
|
|
43
|
+
a = "--sql\nINSERT INTO users (id, name) VALUES (1, 'a')"
|
|
44
|
+
# error: [unsupported-syntax] "a VALUES list is not typed"
|
|
45
|
+
b = "--sql\nINSERT INTO users (id, name) VALUES (1, 'a') RETURNING id"
|
|
46
|
+
# error: [unsupported-syntax] "a VALUES list is not typed"
|
|
47
|
+
c = "--sql\nSELECT * FROM (VALUES (1, 'a')) AS v"
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Why is this bad?
|
|
51
|
+
|
|
52
|
+
sqlty does not guess a type for a construct it does not understand. Without this rule the column is untyped and nothing says why; with it, every untyped column has a diagnostic. `NULL` and placeholders such as `$1` are untyped by nature and not reported.
|
|
53
|
+
|
|
54
|
+
## How to fix
|
|
55
|
+
|
|
56
|
+
Cast the expression to a type: `(1 OPERATOR(pg_catalog.+) 1)::INTEGER`. For a `*` over a `USING` join, list the columns. To accept one case, put `# sqlty: ignore[unsupported-syntax]` on its line.
|
|
57
|
+
|
|
58
|
+
## Configuration
|
|
59
|
+
|
|
60
|
+
A warning by default: the SQL is valid, but sqlty cannot type the construct, so the query is
|
|
61
|
+
not fully checked. A warning tells you why without failing CI on sqlty's gaps. Make it
|
|
62
|
+
fail, or turn it off:
|
|
63
|
+
|
|
64
|
+
```toml
|
|
65
|
+
[tool.sqlty.rules]
|
|
66
|
+
unsupported-syntax = "error"
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Does not need a schema, but without one only constructs are reported, not unresolved names.
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# untyped-query
|
|
2
|
+
|
|
3
|
+
Default severity: **warn**
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
Flags `SQL[UNTYPED]` queries, written by `sqlty rewrite --untyped sentinel`
|
|
8
|
+
for queries without an inferable row type, and says when one has become
|
|
9
|
+
inferable.
|
|
10
|
+
|
|
11
|
+
## Why is this bad?
|
|
12
|
+
|
|
13
|
+
`UNTYPED` rows can't be used as a concrete type; the query still needs casts
|
|
14
|
+
or a schema.
|
|
15
|
+
|
|
16
|
+
## Example
|
|
17
|
+
|
|
18
|
+
```python
|
|
19
|
+
from sqlty import SQL, UNTYPED
|
|
20
|
+
|
|
21
|
+
# error: [untyped-query] "SQL[UNTYPED] has no inferable row type: columns `x`, `y` are untyped; add casts (for example id::INTEGER) or a schema"
|
|
22
|
+
values = SQL[UNTYPED]("--sql\nSELECT x, y FROM pairs")
|
|
23
|
+
# error: [untyped-query] "SQL[UNTYPED] can now be SQL[tuple[int]]; run sqlty check --fix"
|
|
24
|
+
cast = SQL[UNTYPED]("--sql\nSELECT id::INTEGER FROM users")
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Fix
|
|
28
|
+
|
|
29
|
+
`sqlty check --fix .` replaces `UNTYPED` once the row type is inferable:
|
|
30
|
+
|
|
31
|
+
```python
|
|
32
|
+
from sqlty import SQL
|
|
33
|
+
from sqlty import UNTYPED
|
|
34
|
+
|
|
35
|
+
q = SQL[UNTYPED]("--sql\nSELECT id::INTEGER FROM users")
|
|
36
|
+
# fixed: from sqlty import SQL
|
|
37
|
+
# fixed:
|
|
38
|
+
# fixed: q = SQL[tuple[int]]("--sql\nSELECT id::INTEGER FROM users")
|
|
39
|
+
```
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# unused-ignore
|
|
2
|
+
|
|
3
|
+
Default severity: **warn**
|
|
4
|
+
|
|
5
|
+
## What it does
|
|
6
|
+
|
|
7
|
+
Reports a `# sqlty: ignore` comment that suppresses no diagnostic. A bare
|
|
8
|
+
comment is reported when nothing on its line is suppressed; a comment with a
|
|
9
|
+
rule list is reported once per listed rule that suppresses nothing.
|
|
10
|
+
|
|
11
|
+
```python
|
|
12
|
+
from sqlty import SQL
|
|
13
|
+
|
|
14
|
+
q = SQL[tuple[int]]("--sql\nSELECT id::INTEGER FROM users") # sqlty: ignore[select-star]
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`sqlty check` reports `'# sqlty: ignore[select-star]' suppresses no diagnostic`
|
|
18
|
+
when this rule is enabled.
|
|
19
|
+
|
|
20
|
+
## Why is this bad?
|
|
21
|
+
|
|
22
|
+
A suppression outlives the problem it hid. Once the query or the row type is
|
|
23
|
+
fixed, a stale comment would silently accept a future regression on that line.
|
|
24
|
+
|
|
25
|
+
## How to fix
|
|
26
|
+
|
|
27
|
+
Remove the comment, or correct the rule name if it is misspelled.
|
|
28
|
+
|
|
29
|
+
## Configuration
|
|
30
|
+
|
|
31
|
+
A warning by default, to keep suppressions honest. Make it fail, or turn it off:
|
|
32
|
+
|
|
33
|
+
```toml
|
|
34
|
+
[tool.sqlty.rules]
|
|
35
|
+
unused-ignore = "error"
|
|
36
|
+
```
|
sqlty/skill/SKILL.md
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: sqlty
|
|
3
|
+
description: Write, type and check raw SQL in Python with sqlty. Use when adding or editing a query in a project that uses sqlty (strings starting with --sql, SQL[T] or SQLTemplate[T] wrappers, a [tool.sqlty] section), when generating Python row types for a query, or when checking that queries still fit a schema migration before deploying.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# sqlty
|
|
7
|
+
|
|
8
|
+
sqlty infers the Python row type of each SQL query from the query and the
|
|
9
|
+
project's schema, writes it as `SQL[T]`, and checks it. `SQL[T]` is a `str`
|
|
10
|
+
subclass: it works wherever a string query works.
|
|
11
|
+
|
|
12
|
+
Read `[tool.sqlty]` in `pyproject.toml` first. `dialect` (default `postgres`)
|
|
13
|
+
and `schema` (SQL files with `CREATE TABLE`, or migrations) decide every type.
|
|
14
|
+
|
|
15
|
+
## Write a typed query
|
|
16
|
+
|
|
17
|
+
1. Write the query as a string whose first line is the marker `--sql`, followed by
|
|
18
|
+
a newline: `"--sql\nSELECT ..."`. With `"--sql SELECT ..."` (a space) the whole
|
|
19
|
+
query is a SQL comment and nothing is checked.
|
|
20
|
+
2. Run `sqlty rewrite <file>`. It wraps the string as `SQL[T]("--sql\n...")`, with
|
|
21
|
+
`T` the inferred row type, and adds the import.
|
|
22
|
+
3. Run `sqlty check <file>`. It must report nothing.
|
|
23
|
+
|
|
24
|
+
```python
|
|
25
|
+
user_id = 1
|
|
26
|
+
q = "--sql\nSELECT id::INTEGER, name::TEXT FROM users WHERE id = %s"
|
|
27
|
+
# rewritten: user_id = 1
|
|
28
|
+
# rewritten: q = SQL[tuple[int, str]]("--sql\nSELECT id::INTEGER, name::TEXT FROM users WHERE id = %s")
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
A query `rewrite` leaves alone has a column sqlty cannot type: a column of a
|
|
32
|
+
table the schema lacks, an unknown function, `SELECT *` with no schema. Cast it
|
|
33
|
+
(`x::INTEGER`) or fix the schema; don't hand-write a guessed `T`.
|
|
34
|
+
|
|
35
|
+
## Name the row type
|
|
36
|
+
|
|
37
|
+
Replace the tuple with a `TypedDict`, `dataclass`, `NamedTuple` or pydantic model
|
|
38
|
+
whose fields are the columns, in order. `sqlty check` compares each field's name,
|
|
39
|
+
type and nullability with the query:
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from typing import TypedDict
|
|
43
|
+
from sqlty import SQL
|
|
44
|
+
|
|
45
|
+
class User(TypedDict):
|
|
46
|
+
id: int
|
|
47
|
+
name: str
|
|
48
|
+
|
|
49
|
+
# error: [column-type-mismatch] "User.name is str but query column 'name' infers int"
|
|
50
|
+
q = SQL[User]("--sql\nSELECT id::INTEGER, name::INTEGER AS name FROM users")
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
A column that can be NULL needs `T | None`. Run `sqlty check --fix` to update
|
|
54
|
+
stale tuple types after editing a query; it never edits a named row type.
|
|
55
|
+
|
|
56
|
+
## Check
|
|
57
|
+
|
|
58
|
+
| Goal | Command |
|
|
59
|
+
|------|---------|
|
|
60
|
+
| Lint everything | `sqlty check .` |
|
|
61
|
+
| Fail CI on warnings | `sqlty check --error-on-warning .` |
|
|
62
|
+
| Fix what is fixable | `sqlty check --fix .` |
|
|
63
|
+
| Report names the schema lacks | `sqlty check --warn unknown-table --warn unknown-column .` |
|
|
64
|
+
| See which rule typed a column | `sqlty check --explain .` |
|
|
65
|
+
| Learn about a rule | `sqlty rule <name>` |
|
|
66
|
+
|
|
67
|
+
To inspect one query without a file, use the Python API:
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
from sqlty.core import analyze_params, analyze_select, infer_type
|
|
71
|
+
|
|
72
|
+
schema = "CREATE TABLE users (id INTEGER NOT NULL, name TEXT)"
|
|
73
|
+
infer_type("SELECT id, name FROM users", schema_sql=schema) # 'tuple[int, str | None]'
|
|
74
|
+
analyze_params("SELECT name FROM users WHERE id = %s", schema_sql=schema) # %s is an int
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
## Check a schema change before deploying
|
|
78
|
+
|
|
79
|
+
```bash
|
|
80
|
+
sqlty breaking --against origin/main
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
It checks the queries of `origin/main` against the schema of the working tree
|
|
84
|
+
(migrate first) and the new queries against the old schema (deploy first), and
|
|
85
|
+
exits 1 on a dropped or renamed table or column, a new `NOT NULL` column an
|
|
86
|
+
`INSERT` leaves out, or a row type that no longer fits. Use
|
|
87
|
+
`--direction backward` when migrations always run before the code ships.
|
|
88
|
+
|
|
89
|
+
## Pitfalls
|
|
90
|
+
|
|
91
|
+
- `SQL[T]` only types the string; pass it to the driver as usual, or use
|
|
92
|
+
`sqlty.drivers.psycopg.execute` to get typed rows.
|
|
93
|
+
- Building a query with `+`, `%` or an f-string loses the type and is not
|
|
94
|
+
checked (`fstring-skipped`). Pass values as parameters (`%s`, `$1`, `?`).
|
|
95
|
+
- t-strings are checked as `SQLTemplate[T]`; psycopg's `{x:i}`, `{x:l}` and
|
|
96
|
+
`{part:q}` fields are read in modules that import psycopg.
|
|
97
|
+
- Types follow the dialect's driver: in SQLite a `DATE` is `str` and a `NUMERIC`
|
|
98
|
+
a `float`. See [Type mapping](https://jirikuncar.github.io/sqlty/reference/types/) before "fixing" a type.
|
|
99
|
+
|
|
100
|
+
## Reference
|
|
101
|
+
|
|
102
|
+
The documentation is at <https://jirikuncar.github.io/sqlty/>:
|
|
103
|
+
|
|
104
|
+
- [Writing queries](https://jirikuncar.github.io/sqlty/guide/writing-queries/), t-strings included, and the
|
|
105
|
+
[type mapping](https://jirikuncar.github.io/sqlty/reference/types/)
|
|
106
|
+
- [Row types](https://jirikuncar.github.io/sqlty/guide/row-types/)
|
|
107
|
+
- [Parameters](https://jirikuncar.github.io/sqlty/guide/parameters/)
|
|
108
|
+
- [Schema changes](https://jirikuncar.github.io/sqlty/guide/breaking-changes/)
|
|
109
|
+
- Every rule: `sqlty rule NAME` prints its page; [all rules](https://jirikuncar.github.io/sqlty/reference/rules/)
|
|
110
|
+
- `sqlty check --output-format json` prints the diagnostics as JSON
|