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.
Files changed (71) hide show
  1. sqlty/__init__.py +99 -0
  2. sqlty/__main__.py +5 -0
  3. sqlty/_breaking.py +191 -0
  4. sqlty/_check.py +880 -0
  5. sqlty/_cli.py +1219 -0
  6. sqlty/_config.py +666 -0
  7. sqlty/_core.cp315-win_amd64.pyd +0 -0
  8. sqlty/_core.pyi +108 -0
  9. sqlty/_decode.py +62 -0
  10. sqlty/_drivers.py +274 -0
  11. sqlty/_gitignore.py +58 -0
  12. sqlty/_helpers.py +613 -0
  13. sqlty/_json_shape.py +299 -0
  14. sqlty/_json_types.py +225 -0
  15. sqlty/_lint.py +350 -0
  16. sqlty/_params.py +177 -0
  17. sqlty/_psycopg.py +269 -0
  18. sqlty/_source.py +654 -0
  19. sqlty/_type_extractor/__init__.py +51 -0
  20. sqlty/_type_extractor/ast_fields.py +823 -0
  21. sqlty/_type_extractor/compat.py +252 -0
  22. sqlty/_type_extractor/extract.py +179 -0
  23. sqlty/_type_extractor/generic.py +95 -0
  24. sqlty/_type_extractor/info.py +161 -0
  25. sqlty/_type_extractor/sandbox.py +385 -0
  26. sqlty/_type_extractor/subtype.py +156 -0
  27. sqlty/_widening.py +89 -0
  28. sqlty/core.py +566 -0
  29. sqlty/drivers/psycopg.py +104 -0
  30. sqlty/magic.py +126 -0
  31. sqlty/py.typed +0 -0
  32. sqlty/rules/ambiguous-column.md +48 -0
  33. sqlty/rules/column-count-mismatch.md +30 -0
  34. sqlty/rules/column-name-mismatch.md +34 -0
  35. sqlty/rules/column-type-mismatch.md +75 -0
  36. sqlty/rules/fstring-skipped.md +23 -0
  37. sqlty/rules/inline-sql-comment.md +27 -0
  38. sqlty/rules/insert-missing-column.md +58 -0
  39. sqlty/rules/json-shape-mismatch.md +96 -0
  40. sqlty/rules/literal-mismatch.md +83 -0
  41. sqlty/rules/mysql-sql-marker.md +32 -0
  42. sqlty/rules/nullable-mismatch.md +76 -0
  43. sqlty/rules/param-count-mismatch.md +66 -0
  44. sqlty/rules/param-type-mismatch.md +73 -0
  45. sqlty/rules/redundant-literal.md +68 -0
  46. sqlty/rules/redundant-optional.md +100 -0
  47. sqlty/rules/select-star-no-schema.md +24 -0
  48. sqlty/rules/select-star.md +77 -0
  49. sqlty/rules/stale-rewrite.md +33 -0
  50. sqlty/rules/stale-type.md +50 -0
  51. sqlty/rules/tstring-skipped.md +68 -0
  52. sqlty/rules/typeddict-inheritance.md +35 -0
  53. sqlty/rules/typeddict-key-mismatch.md +38 -0
  54. sqlty/rules/unchecked-query.md +34 -0
  55. sqlty/rules/ungrouped-column.md +67 -0
  56. sqlty/rules/unknown-column.md +68 -0
  57. sqlty/rules/unknown-function.md +49 -0
  58. sqlty/rules/unknown-table.md +47 -0
  59. sqlty/rules/unknown-type.md +74 -0
  60. sqlty/rules/unparsed-query.md +46 -0
  61. sqlty/rules/unsupported-syntax.md +69 -0
  62. sqlty/rules/untyped-query.md +39 -0
  63. sqlty/rules/unused-ignore.md +36 -0
  64. sqlty/skill/SKILL.md +110 -0
  65. sqlty-0.1.0.dist-info/METADATA +210 -0
  66. sqlty-0.1.0.dist-info/RECORD +71 -0
  67. sqlty-0.1.0.dist-info/WHEEL +4 -0
  68. sqlty-0.1.0.dist-info/entry_points.txt +2 -0
  69. sqlty-0.1.0.dist-info/licenses/LICENSE +21 -0
  70. sqlty-0.1.0.dist-info/sboms/sqlty-core.cyclonedx.json +1891 -0
  71. sqlty_magic.pth +1 -0
@@ -0,0 +1,76 @@
1
+ # nullable-mismatch
2
+
3
+ Default severity: **error**
4
+
5
+ ## What it does
6
+
7
+ Reports a field of a user-defined row type (or an item of a helper's `tuple[...]` row
8
+ type) that is not optional while its query column can be NULL: the column comes from a
9
+ schema column without `NOT NULL` / `PRIMARY KEY`, from the optional side of an outer join,
10
+ or from an expression that can yield NULL.
11
+
12
+ ```toml
13
+ [tool.sqlty]
14
+ schema = "schema.sql"
15
+ ```
16
+
17
+ `schema.sql`:
18
+
19
+ ```sql
20
+ CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT);
21
+ ```
22
+
23
+ ```python
24
+ from typing import TypedDict
25
+
26
+ from sqlty import SQL
27
+
28
+
29
+ class User(TypedDict):
30
+ id: int
31
+ name: str
32
+
33
+
34
+ # error: [nullable-mismatch] "User.name is str but query column 'name' can be NULL"
35
+ q = SQL[User]("--sql\nSELECT id, name FROM users")
36
+ ```
37
+
38
+ ## Why is this bad?
39
+
40
+ The annotation promises a value the database does not always return. A `None` reaches
41
+ code that type checkers believe handles only `str`.
42
+
43
+ ## How to fix
44
+
45
+ Make the field optional (`name: str | None`), or filter the NULLs out in the query
46
+ (`WHERE name IS NOT NULL`), or give the column a default (`COALESCE(name, '')`).
47
+
48
+ ```python
49
+ from typing import TypedDict
50
+
51
+ from sqlty import SQL
52
+
53
+
54
+ class User(TypedDict):
55
+ id: int
56
+ name: str | None
57
+
58
+
59
+ q = SQL[User]("--sql\nSELECT id, name FROM users")
60
+ # error:
61
+ ```
62
+
63
+ Fields typed `Any` or `object`, and fields whose annotation sqlty cannot resolve, are
64
+ not reported. A column whose nullability is unknown, such as `id::INTEGER` with no
65
+ schema, is never reported: sqlty only reports what the schema or the query proves.
66
+
67
+ ## Configuration
68
+
69
+ A field annotated `T | None` is accepted for a column that cannot be NULL; only
70
+ [`redundant-optional`](redundant-optional.md) reports that. To lower the severity of this
71
+ rule while you add the missing `| None`, or to turn it off:
72
+
73
+ ```toml
74
+ [tool.sqlty.rules]
75
+ nullable-mismatch = "warn"
76
+ ```
@@ -0,0 +1,66 @@
1
+ # param-count-mismatch
2
+
3
+ Default severity: **error**
4
+
5
+ ## What it does
6
+
7
+ Compares the arguments a call passes with the placeholders of its query. It
8
+ applies to calls of a [marked helper](../../../docs/guide/helpers.md) whose parameters are
9
+ spelled out (a `params` argument holding a tuple, list or dict literal,
10
+ `*args`, or `**params`), and reports:
11
+
12
+ - a different number of positional arguments than the query has placeholders
13
+ (`%s`, `$1`, `?`; the highest `$N` counts);
14
+ - names of the query (`%(name)s`, `:name`) the call does not pass. A name used
15
+ twice is one parameter; extra keys are ignored, as the drivers do.
16
+
17
+ Driver calls are checked too: `execute` and `executemany` (each row) on a
18
+ psycopg or sqlite3 connection or cursor, and `execute`, `fetch`, `fetchrow`,
19
+ `fetchval` on an asyncpg connection, when the file binds the receiver to
20
+ `psycopg.connect(...)`, `sqlite3.connect(...)`, `asyncpg.connect(...)`,
21
+ `<connection>.cursor()` or a driver class annotation. The query is a literal,
22
+ `SQL[T]("...")` of one, or a name assigned once to one.
23
+
24
+ Positional arguments for named placeholders, or the reverse, and calls that do
25
+ not spell their arguments out (`params` as a variable, `*rest`, `**kwargs`),
26
+ are not compared. A driver call with no parameters against a query that has
27
+ placeholders is reported.
28
+
29
+ ## Why is this bad?
30
+
31
+ The driver raises at run time, or the query silently runs with the wrong
32
+ values, and only on the path that runs it.
33
+
34
+ ## Example
35
+
36
+ ```toml
37
+ [tool.sqlty]
38
+ schema = "schema.sql"
39
+ ```
40
+
41
+ `schema.sql`:
42
+
43
+ ```sql
44
+ CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
45
+ ```
46
+
47
+ ```python
48
+ from typing import Annotated, LiteralString
49
+
50
+ from sqlty import SQL
51
+
52
+
53
+ def execute(query: Annotated[LiteralString, SQL], params: tuple[object, ...] | dict[str, object]) -> None: ...
54
+
55
+
56
+ # error: [param-count-mismatch] "query has 2 parameters, the call passes 1"
57
+ execute("SELECT id FROM users WHERE id = %s AND name = %s", (1,))
58
+ # error: [param-count-mismatch] "call is missing parameter 'name'"
59
+ execute("SELECT id FROM users WHERE id = %(id)s AND name = %(name)s", {"id": 1})
60
+ ```
61
+
62
+ ## Fix
63
+
64
+ Pass one argument per placeholder, or name each placeholder in the call.
65
+ Turn the rule off with `param-count-mismatch = "ignore"` in
66
+ `[tool.sqlty.rules]`.
@@ -0,0 +1,73 @@
1
+ # param-type-mismatch
2
+
3
+ Default severity: **error**
4
+
5
+ ## What it does
6
+
7
+ Checks the arguments a call passes for its query's placeholders against the
8
+ type each placeholder takes from its context: the column it is compared with
9
+ or stored in, a cast, `LIMIT`, an `IN` list, `= ANY(%s)` (a list). It applies
10
+ to calls of a [marked helper](../../../docs/guide/helpers.md) whose parameters are spelled
11
+ out: a `params` argument holding a tuple, list or dict literal, `*args`, or
12
+ `**params`.
13
+
14
+ Driver calls are checked too: `execute` and `executemany` (each row) on a
15
+ psycopg or sqlite3 connection or cursor, and `execute`, `fetch`, `fetchrow`,
16
+ `fetchval` on an asyncpg connection, when the file binds the receiver to
17
+ `psycopg.connect(...)`, `sqlite3.connect(...)`, `asyncpg.connect(...)`,
18
+ `<connection>.cursor()` or a driver class annotation. The query is a literal,
19
+ `SQL[T]("...")` of one, or a name assigned once to one.
20
+
21
+ An argument is judged only where the source states its type: a literal, a
22
+ variable annotated or assigned a class, a call of a known class (`date(...)`,
23
+ `UUID(...)`). An argument of unknown type is never reported, and neither is a
24
+ `str`, which the server coerces to any type. `int`, `float` and `Decimal` are
25
+ interchangeable. On PostgreSQL a `bool` is not a number and an `int` is not a
26
+ `boolean`, since the server has no implicit cast between them; MySQL takes
27
+ either. A parameter with an explicit cast (`%s::text`) takes
28
+ anything. `None` is reported only for a parameter stored in a `NOT NULL`
29
+ column. SQLite is dynamically typed: with `dialect = "sqlite"` only `None` is
30
+ checked.
31
+
32
+ ## Why is this bad?
33
+
34
+ The database rejects the value, or compares it in a way that never matches,
35
+ at run time and only on the path that runs the query.
36
+
37
+ ## Example
38
+
39
+ ```toml
40
+ [tool.sqlty]
41
+ schema = "schema.sql"
42
+ ```
43
+
44
+ `schema.sql`:
45
+
46
+ ```sql
47
+ CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL, born DATE);
48
+ ```
49
+
50
+ ```python
51
+ from datetime import date
52
+ from typing import Annotated, LiteralString
53
+
54
+ from sqlty import SQL
55
+
56
+
57
+ def execute(query: Annotated[LiteralString, SQL], params: tuple[object, ...]) -> None: ...
58
+
59
+
60
+ execute("SELECT name FROM users WHERE id = %s AND born > %s", (1, date(2000, 1, 1)))
61
+ # error: [param-type-mismatch] "parameter 1 expects int, got datetime.date"
62
+ execute("SELECT name FROM users WHERE id = %s", (date(2000, 1, 1),))
63
+ # error: [param-type-mismatch] "parameter 2 is not nullable, got None"
64
+ execute("INSERT INTO users (id, name) VALUES (%s, %s)", (1, None))
65
+ # error: [param-type-mismatch] "parameter 'id' expects int, got bytes"
66
+ execute("DELETE FROM users WHERE id = %(id)s", {"id": b"1"})
67
+ ```
68
+
69
+ ## Fix
70
+
71
+ Pass a value of the type the query expects, or change the query.
72
+ Turn the rule off with `param-type-mismatch = "ignore"` in
73
+ `[tool.sqlty.rules]`.
@@ -0,0 +1,68 @@
1
+ # redundant-literal
2
+
3
+ Default severity: **ignore**
4
+
5
+ ## What it does
6
+
7
+ Reports a field of a user-defined row type (or an item of a helper's `tuple[...]` row
8
+ type) annotated with a `Literal` that lists values its query column can never take. This
9
+ is the strict side of [`literal-mismatch`](literal-mismatch.md): that rule finds missing
10
+ values, this one finds extra ones.
11
+
12
+ ```toml
13
+ [tool.sqlty]
14
+ schema = "schema.sql"
15
+
16
+ [tool.sqlty.rules]
17
+ redundant-literal = "warn"
18
+ ```
19
+
20
+ `schema.sql`:
21
+
22
+ ```sql
23
+ CREATE TABLE orders (
24
+ id INTEGER PRIMARY KEY,
25
+ status TEXT NOT NULL CHECK (status IN ('pending', 'paid'))
26
+ );
27
+ ```
28
+
29
+ ```python
30
+ from typing import Literal, TypedDict
31
+
32
+ from sqlty import SQL
33
+
34
+
35
+ class Order(TypedDict):
36
+ id: int
37
+ status: Literal["pending", "paid", "refunded"]
38
+
39
+
40
+ # error: [redundant-literal] "Order.status is Literal['pending', 'paid', 'refunded'] but query column 'status' is never 'refunded'"
41
+ q = SQL[Order]("--sql\nSELECT id, status FROM orders")
42
+ ```
43
+
44
+ ## Why is this bad?
45
+
46
+ Callers handle a case that never arrives, and the annotation hides which values the
47
+ query really returns.
48
+
49
+ ## How to fix
50
+
51
+ Drop the extra values, or accept them: a wider `Literal` is correct, just looser than
52
+ needed. That is why the rule is off by default: one shared `Literal` alias for a status
53
+ column is used by many queries, and each of them returns only some of its values.
54
+
55
+ ```python
56
+ from typing import Literal, TypedDict
57
+
58
+ from sqlty import SQL
59
+
60
+
61
+ class Order(TypedDict):
62
+ id: int
63
+ status: Literal["pending", "paid"]
64
+
65
+
66
+ q = SQL[Order]("--sql\nSELECT id, status FROM orders")
67
+ # error:
68
+ ```
@@ -0,0 +1,100 @@
1
+ # redundant-optional
2
+
3
+ Default severity: **ignore**
4
+
5
+ ## What it does
6
+
7
+ Reports a field of a user-defined row type (or an item of a helper's `tuple[...]` row
8
+ type) annotated `T | None` or `Optional[T]` while its query column cannot be NULL. This is
9
+ the strict side of `nullable-mismatch`: that rule finds missing `| None`, this one finds
10
+ extra.
11
+
12
+ ```toml
13
+ [tool.sqlty]
14
+ schema = "schema.sql"
15
+
16
+ [tool.sqlty.rules]
17
+ redundant-optional = "warn"
18
+ ```
19
+
20
+ `schema.sql`:
21
+
22
+ ```sql
23
+ CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
24
+ ```
25
+
26
+ ```python
27
+ from typing import TypedDict
28
+
29
+ from sqlty import SQL
30
+
31
+
32
+ class User(TypedDict):
33
+ id: int
34
+ name: str | None
35
+
36
+
37
+ # error: [redundant-optional] "User.name is str | None but query column 'name' is never NULL"
38
+ q = SQL[User]("--sql\nSELECT id, name FROM users")
39
+ ```
40
+
41
+ A row type shared by several queries is judged by all of them: a field is reported only
42
+ when no query that loads the type can return NULL for it. `sqlty check` gathers the queries
43
+ of every checked file before it reports.
44
+
45
+ ```python
46
+ from typing import TypedDict
47
+
48
+ from sqlty import SQL
49
+
50
+
51
+ class Owned(TypedDict):
52
+ id: int
53
+ name: str | None
54
+
55
+
56
+ named = SQL[Owned]("--sql\nSELECT id, name FROM users")
57
+ maybe = SQL[Owned]("--sql\nSELECT u.id, o.name FROM users u LEFT JOIN users o ON o.id = u.id + 1")
58
+ # error:
59
+ ```
60
+
61
+ ## Why is this bad?
62
+
63
+ Callers handle a `None` that never arrives, and the annotation hides which columns are
64
+ really optional.
65
+
66
+ ## How to fix
67
+
68
+ Drop the `| None`, or accept it: a wider annotation is correct, just looser than needed.
69
+
70
+ ```python
71
+ from typing import TypedDict
72
+
73
+ from sqlty import SQL
74
+
75
+
76
+ class User(TypedDict):
77
+ id: int
78
+ name: str
79
+
80
+
81
+ q = SQL[User]("--sql\nSELECT id, name FROM users")
82
+ # error:
83
+ ```
84
+
85
+ ## Configuration
86
+
87
+ Off by default, because it conflicts with a documented workflow: an
88
+ [expand and contract](../../../docs/guide/breaking-changes.md#expand-and-contract)
89
+ migration widens an annotation to `T | None` before the schema relaxes the column, and
90
+ `T | None` on a non-null column is correct, only loose. Enable it:
91
+
92
+ ```toml
93
+ [tool.sqlty.rules]
94
+ redundant-optional = "warn"
95
+ ```
96
+
97
+ Only columns sqlty knows cannot be NULL are judged: a schema column with `NOT NULL` /
98
+ `PRIMARY KEY`, a `COUNT`, a literal. A column of unknown nullability (a cast with no
99
+ schema, an unknown function) is never reported. `Any` and `object` fields are not
100
+ optional annotations and are not reported.
@@ -0,0 +1,24 @@
1
+ # select-star-no-schema
2
+
3
+ Default severity: **error**
4
+
5
+ ## What it does
6
+
7
+ Flags `SELECT *` when no schema is configured.
8
+
9
+ ## Why is this bad?
10
+
11
+ Without a schema the columns behind `*` are unknown, so the row type can't
12
+ be right.
13
+
14
+ ## Example
15
+
16
+ ```python
17
+ from sqlty import SQL
18
+
19
+ # error: [select-star-no-schema] "SELECT * used but no schema available"
20
+ q = SQL[tuple[str]]("--sql\nSELECT * FROM users")
21
+ ```
22
+
23
+ Configure a [schema](../../../docs/guide/schema-inference.md) or list the
24
+ columns with casts. The annotation is not compared with an inferred type, and `check --fix` does not change it.
@@ -0,0 +1,77 @@
1
+ # select-star
2
+
3
+ Default severity: **warn**
4
+
5
+ The examples use this schema:
6
+
7
+ ```toml
8
+ [tool.sqlty]
9
+ schema = "schema.sql"
10
+ ```
11
+
12
+ `schema.sql`:
13
+
14
+ ```sql
15
+ CREATE TABLE users (id INTEGER NOT NULL, name TEXT NOT NULL);
16
+ CREATE TABLE teams (id INTEGER NOT NULL, owner_id INTEGER NOT NULL);
17
+ ```
18
+
19
+ ## What it does
20
+
21
+ Flags a `*` or qualified `t.*` that shapes the result of a query whose
22
+ columns come from a schema: in the `SELECT` list, in `RETURNING`, or in a
23
+ CTE the query reads. Stars that don't shape the result, such as
24
+ `EXISTS (SELECT * ...)` or `count(*)`, are not reported.
25
+
26
+ ## Why is this bad?
27
+
28
+ The row type is fixed when the query is rewritten. Adding a column to the
29
+ table silently changes what the query returns at runtime.
30
+
31
+ ## Example
32
+
33
+ ```python
34
+ from sqlty import SQL
35
+
36
+ # error: [select-star] "SELECT * used — expand to explicit column list"
37
+ q = SQL[tuple[int, str]]("--sql\nSELECT * FROM users")
38
+ # error: [select-star] "SELECT u.* used — expand to explicit column list"
39
+ r = SQL[tuple[int, str, int]]("--sql\nSELECT u.*, t.id FROM users u JOIN teams t ON t.owner_id = u.id")
40
+ # error: [select-star] "RETURNING * used — expand to explicit column list"
41
+ s = SQL[tuple[int, str]]("--sql\nDELETE FROM users WHERE id = 1 RETURNING *")
42
+ # error: [select-star] "WITH u AS (SELECT *) used — expand to explicit column list"
43
+ t = SQL[tuple[int]]("--sql\nWITH u AS (SELECT * FROM users) SELECT id FROM u")
44
+ ```
45
+
46
+ ## Fix
47
+
48
+ `sqlty check --fix .` lists the columns from the schema, each cast to its
49
+ type with `::`. MySQL and SQLite have no `::`, so there the columns are listed
50
+ bare (`SELECT id, name FROM users`); the schema still types them.
51
+
52
+ ```python
53
+ from sqlty import SQL
54
+
55
+ q = SQL[tuple[int, str]]("--sql\nSELECT * FROM users")
56
+ # fixed: from sqlty import SQL
57
+ # fixed:
58
+ # fixed: q = SQL[tuple[int, str]]("--sql\nSELECT id::INT, name::TEXT FROM users")
59
+ ```
60
+
61
+ A qualified star keeps its qualifier, and a bare `*` over several tables lists each
62
+ table's columns qualified by its alias or name, so joins are expanded too:
63
+
64
+ ```python
65
+ from sqlty import SQL
66
+
67
+ r = SQL[tuple[int, str, int]]("--sql\nSELECT u.*, t.id FROM users u JOIN teams t ON t.owner_id = u.id")
68
+ # fixed: from sqlty import SQL
69
+ # fixed:
70
+ # fixed: r = SQL[tuple[int, str, int]]("--sql\nSELECT u.id::INT, u.name::TEXT, t.id FROM users u JOIN teams t ON t.owner_id = u.id")
71
+ ```
72
+
73
+ Queries with escapes or quotes inside the string, queries split over several
74
+ concatenated strings, and a bare `*` over a `USING` or `NATURAL` join (which
75
+ merges columns), a subquery or a quoted alias, are left for you to edit, as are
76
+ stars in `RETURNING` and in CTEs. `select-star` is
77
+ still reported for them.
@@ -0,0 +1,33 @@
1
+ # stale-rewrite
2
+
3
+ Default severity: **warn**
4
+
5
+ ## What it does
6
+
7
+ Flags `--sql` strings that `sqlty rewrite` would wrap.
8
+
9
+ ## Why is this bad?
10
+
11
+ An unwrapped query is a plain `str`: type checkers and drivers see no row
12
+ type.
13
+
14
+ ## Example
15
+
16
+ ```python
17
+ # error: [stale-rewrite] "Source has --sql strings that aren't wrapped in SQL[T]"
18
+ q = "--sql\nSELECT id::INTEGER FROM users"
19
+ ```
20
+
21
+ ## Fix
22
+
23
+ `sqlty rewrite .` or `sqlty check --fix .`:
24
+
25
+ ```python
26
+ q = "--sql\nSELECT id::INTEGER FROM users"
27
+ # fixed: from sqlty import SQL
28
+ # fixed: q = SQL[tuple[int]]("--sql\nSELECT id::INTEGER FROM users")
29
+ ```
30
+
31
+ Strings rewrite can't type (DDL, DML without `RETURNING`, invalid SQL) are
32
+ not flagged; see [untyped queries](../../../docs/guide/untyped.md) to wrap
33
+ them anyway.
@@ -0,0 +1,50 @@
1
+ # stale-type
2
+
3
+ Default severity: **warn**
4
+
5
+ ## What it does
6
+
7
+ Flags `SQL[tuple[...]]` annotations that no longer match the query. An item
8
+ that only parametrises an inferred generic (`list[str]` for `list`), or narrows
9
+ an inferred `typing.Any` (a JSON column), is more precise than inference and is
10
+ not flagged.
11
+
12
+ ## Why is this bad?
13
+
14
+ The SQL changed after the last rewrite, so the annotation describes rows the
15
+ query no longer returns.
16
+
17
+ ## Example
18
+
19
+ ```python
20
+ from sqlty import SQL
21
+
22
+ # error: [stale-type] "SQL[tuple[int]] should be SQL[tuple[int, str]]"
23
+ q = SQL[tuple[int]]("--sql\nSELECT id::INTEGER, name::TEXT FROM users")
24
+ ```
25
+
26
+ User-defined row types are checked by the `column-*` rules instead.
27
+
28
+ ## Fix
29
+
30
+ `sqlty check --fix .` writes the inferred type:
31
+
32
+ ```python
33
+ from sqlty import SQL
34
+
35
+ q = SQL[tuple[int]]("--sql\nSELECT id::INTEGER, name::TEXT FROM users")
36
+ # fixed: from sqlty import SQL
37
+ # fixed:
38
+ # fixed: q = SQL[tuple[int, str]]("--sql\nSELECT id::INTEGER, name::TEXT FROM users")
39
+ ```
40
+
41
+ Parametrised generics that refine the inferred type are kept by `--fix`:
42
+
43
+ ```python
44
+ from sqlty import SQL
45
+
46
+ q = SQL[tuple[int, list[str]]]("--sql\nSELECT id::INTEGER, tags::TEXT[] FROM users")
47
+ # fixed: from sqlty import SQL
48
+ # fixed:
49
+ # fixed: q = SQL[tuple[int, list[str]]]("--sql\nSELECT id::INTEGER, tags::TEXT[] FROM users")
50
+ ```
@@ -0,0 +1,68 @@
1
+ # tstring-skipped
2
+
3
+ Default severity: **warn**
4
+
5
+ ## What it does
6
+
7
+ Flags t-strings ([PEP 750](https://peps.python.org/pep-0750/)) starting with
8
+ `--sql` that rewrite leaves alone, and names the field that blocks them. A
9
+ field is read when it passes a value:
10
+
11
+ | Field | Read as |
12
+ |-------|---------|
13
+ | `{value}`, `{value:s}`, `{value:b}`, `{value:t}` | a placeholder |
14
+ | `{value:l}` (psycopg literal) | a placeholder |
15
+ | `{name:i}` (psycopg identifier) | an identifier, only in a query that returns no rows |
16
+ | `{part:q}` (psycopg snippet) | the module-level template `part` names, inlined |
17
+
18
+ `:l`, `:i` and `:q` are psycopg's, so they are read only in a module that
19
+ imports `psycopg` or `sqlty.drivers.psycopg`. Any other field, such as a
20
+ conversion (`{value!r}`) or another format spec, is skipped. A t-string whose
21
+ fields are all read is rewritten as `SQLTemplate[T](t"...")` and checked; see
22
+ [t-strings](../../../docs/guide/writing-queries.md#t-strings).
23
+
24
+ ## Why is this bad?
25
+
26
+ Such a field changes the text of the SQL in a way only known at runtime, so the
27
+ query can't be analyzed statically.
28
+
29
+ ## Example
30
+
31
+ An identifier picks the result column:
32
+
33
+ ```python
34
+ import psycopg
35
+
36
+ column = "id"
37
+ # error: [tstring-skipped] "t-string with --sql prefix skipped (the field {column:i} changes the SQL)"
38
+ q = t"--sql\nSELECT {column:i}::INTEGER FROM users"
39
+ ```
40
+
41
+ Outside a psycopg module, `:i`, `:l` and `:q` are not read:
42
+
43
+ ```python
44
+ table = "users"
45
+ name = "Ada"
46
+ # error: [tstring-skipped] "t-string with --sql prefix skipped (the field {table:i} changes the SQL)"
47
+ q = t"--sql\nINSERT INTO {table:i} (name) VALUES ({name})"
48
+ ```
49
+
50
+ A snippet must name a template assigned once at module level:
51
+
52
+ ```python
53
+ import psycopg
54
+
55
+ def condition():
56
+ return t"id = 1"
57
+
58
+ # error: [tstring-skipped] "t-string with --sql prefix skipped (the field {condition():q} changes the SQL)"
59
+ q = t"--sql\nSELECT id::INTEGER FROM users WHERE {condition():q}"
60
+ ```
61
+
62
+ A conversion is never read:
63
+
64
+ ```python
65
+ user_id = 1
66
+ # error: [tstring-skipped] "t-string with --sql prefix skipped (the field {user_id!r} changes the SQL)"
67
+ q = t"--sql\nSELECT id::INTEGER FROM users WHERE id = {user_id!r}"
68
+ ```
@@ -0,0 +1,35 @@
1
+ # typeddict-inheritance
2
+
3
+ Default severity: **error**
4
+
5
+ ## What it does
6
+
7
+ Checks that a TypedDict subclass respects its bases' openness
8
+ ([PEP 728](https://peps.python.org/pep-0728/)): it may not add keys to a
9
+ closed base, reopen it, or change a base's `extra_items` in a way the PEP
10
+ forbids. Reported on the subclass's `class` line.
11
+
12
+ ## Why is this bad?
13
+
14
+ Type checkers reject such a class, and the row type sqlty validates no longer
15
+ means what its base promises.
16
+
17
+ ## Example
18
+
19
+ ```python
20
+ from typing import TypedDict
21
+
22
+ from sqlty import SQL
23
+
24
+
25
+ class Base(TypedDict, closed=True):
26
+ id: int
27
+
28
+
29
+ # error: [typeddict-inheritance] "User cannot add key 'name' to closed base Base"
30
+ class User(Base):
31
+ name: str
32
+
33
+
34
+ q = SQL[User]("--sql\nSELECT id::INTEGER, name::TEXT FROM users")
35
+ ```