sqlmodel-mypy-plugin 0.12.0__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.
- sqlmodel_mypy_plugin-0.12.0/.gitignore +23 -0
- sqlmodel_mypy_plugin-0.12.0/.pre-commit-config.yaml +7 -0
- sqlmodel_mypy_plugin-0.12.0/AGENTS.md +86 -0
- sqlmodel_mypy_plugin-0.12.0/LICENSE +21 -0
- sqlmodel_mypy_plugin-0.12.0/PKG-INFO +208 -0
- sqlmodel_mypy_plugin-0.12.0/README.md +162 -0
- sqlmodel_mypy_plugin-0.12.0/ROADMAP.md +202 -0
- sqlmodel_mypy_plugin-0.12.0/pyproject.toml +93 -0
- sqlmodel_mypy_plugin-0.12.0/src/sqlmodel_mypy/__init__.py +3 -0
- sqlmodel_mypy_plugin-0.12.0/src/sqlmodel_mypy/plugin.py +1140 -0
- sqlmodel_mypy_plugin-0.12.0/src/sqlmodel_mypy/py.typed +1 -0
- sqlmodel_mypy_plugin-0.12.0/src/sqlmodel_mypy/transform.py +836 -0
- sqlmodel_mypy_plugin-0.12.0/tests/conftest.py +12 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/configs/mypy-plugin-no-untyped.ini +16 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/configs/mypy-plugin-pydantic-first.ini +19 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/configs/mypy-plugin-pydantic-last.ini +19 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/configs/mypy-plugin-strict.ini +18 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/configs/mypy-plugin.ini +18 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/configs/pyproject-plugin.toml +16 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/modules/aliases.py +57 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/modules/constructor.py +32 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/modules/execution_options.py +32 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/modules/expressions.py +17 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/modules/field_sa_type.py +12 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/modules/generics.py +15 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/modules/inheritance.py +27 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/modules/joins.py +34 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/modules/label.py +14 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/modules/many_to_many_relationships.py +34 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/modules/mapped_fields.py +15 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/modules/model_config.py +19 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/modules/model_construct.py +15 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/modules/relationship_comparators.py +35 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/modules/relationship_expressions.py +18 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/modules/relationships.py +29 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/modules/select_varargs.py +18 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/modules/selectinload.py +21 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/modules/session_exec.py +34 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/modules/session_get.py +21 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/modules/table_dunders.py +12 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/modules/table_model_awareness.py +18 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/modules/untyped_fields.py +12 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin-pydantic-first_ini/relationships.py +32 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin-pydantic-last_ini/relationships.py +32 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin-strict_ini/aliases.py +63 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin-strict_ini/constructor.py +39 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin-strict_ini/execution_options.py +33 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin-strict_ini/generics.py +16 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin-strict_ini/joins.py +35 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin-strict_ini/label.py +15 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin-strict_ini/many_to_many_relationships.py +37 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin-strict_ini/mapped_fields.py +16 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin-strict_ini/model_construct.py +18 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin-strict_ini/relationship_comparators.py +39 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin-strict_ini/relationships.py +34 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin-strict_ini/table_model_awareness.py +20 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin_ini/aliases.py +61 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin_ini/constructor.py +36 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin_ini/execution_options.py +33 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin_ini/expressions.py +19 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin_ini/inheritance.py +36 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin_ini/joins.py +35 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin_ini/label.py +15 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin_ini/many_to_many_relationships.py +35 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin_ini/model_construct.py +17 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin_ini/relationship_comparators.py +39 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin_ini/relationship_expressions.py +20 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin_ini/relationships.py +32 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin_ini/table_model_awareness.py +20 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/mypy-plugin_ini/untyped_fields.py +14 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/outputs/pyproject-plugin_toml/constructor.py +36 -0
- sqlmodel_mypy_plugin-0.12.0/tests/mypy/test_mypy.py +210 -0
- sqlmodel_mypy_plugin-0.12.0/tests/smoke_test.py +23 -0
- sqlmodel_mypy_plugin-0.12.0/tests/test_plugin_unit.py +1305 -0
- sqlmodel_mypy_plugin-0.12.0/tests/test_transform_unit.py +898 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
.DS_Store
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.pyo
|
|
5
|
+
*.egg-info/
|
|
6
|
+
*.egg/
|
|
7
|
+
|
|
8
|
+
.mypy_cache/
|
|
9
|
+
.pytest_cache/
|
|
10
|
+
.ruff_cache/
|
|
11
|
+
.uv-cache
|
|
12
|
+
.uv_cache
|
|
13
|
+
coverage/
|
|
14
|
+
.coverage
|
|
15
|
+
.coverage.*
|
|
16
|
+
htmlcov/
|
|
17
|
+
|
|
18
|
+
.venv/
|
|
19
|
+
venv/
|
|
20
|
+
ENV/
|
|
21
|
+
|
|
22
|
+
dist/
|
|
23
|
+
build/
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
# AGENTS.md
|
|
2
|
+
|
|
3
|
+
# AGENTS.md (repo workflow & invariants)
|
|
4
|
+
|
|
5
|
+
This repo builds a **mypy plugin** for **SQLModel**.
|
|
6
|
+
|
|
7
|
+
## Non-negotiable quality gates (every session / every PR)
|
|
8
|
+
|
|
9
|
+
Before you consider work "done", you **must** have a green:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
make check
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
This includes **tests** and a **coverage gate**: **90%+** for the `sqlmodel_mypy` package.
|
|
16
|
+
|
|
17
|
+
If your change affects mypy diagnostics or generated signatures, you **must** update and commit golden outputs:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
make update-mypy
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Toolchain / stack
|
|
24
|
+
|
|
25
|
+
- **Language**: Python (plugin code + tests)
|
|
26
|
+
- **Runtime**: Python **>= 3.10** (CI tests multiple versions)
|
|
27
|
+
- **Package manager / runner**: `uv` (lockfile-driven, reproducible)
|
|
28
|
+
- **Lint/format**: `ruff`
|
|
29
|
+
- **Typecheck**: `mypy` (plugin uses mypy internal APIs)
|
|
30
|
+
- **Tests**: `pytest`
|
|
31
|
+
- **Build backend**: `hatchling`
|
|
32
|
+
|
|
33
|
+
`uv.lock` is the source of truth for dependency resolution in CI and development.
|
|
34
|
+
|
|
35
|
+
## Quick commands (canonical)
|
|
36
|
+
|
|
37
|
+
- **Help**: `make help`
|
|
38
|
+
- **Install (locked dev env)**: `make install`
|
|
39
|
+
- **Format**: `make fmt`
|
|
40
|
+
- **Lint**: `make lint`
|
|
41
|
+
- **Typecheck**: `make typecheck`
|
|
42
|
+
- **Tests**: `make test`
|
|
43
|
+
- **All gates (what CI runs)**: `make check`
|
|
44
|
+
- **Update golden mypy outputs**: `make update-mypy`
|
|
45
|
+
- **Build artifacts**: `make build`
|
|
46
|
+
|
|
47
|
+
## What the plugin does (scope)
|
|
48
|
+
|
|
49
|
+
- Collects SQLModel fields and determines required/optional kwargs based on `sqlmodel.Field(...)`.
|
|
50
|
+
- **Ignores** `sqlmodel.Relationship(...)` when generating constructor kwargs.
|
|
51
|
+
- Synthesizes `__init__` and `model_construct` signatures for SQLModel subclasses during mypy semantic analysis.
|
|
52
|
+
|
|
53
|
+
## Repo layout (where things live)
|
|
54
|
+
|
|
55
|
+
- `src/sqlmodel_mypy/plugin.py`: mypy plugin entrypoint, config parsing, cache invalidation.
|
|
56
|
+
- `src/sqlmodel_mypy/transform.py`: SQLModel class transformation (collect fields, generate methods).
|
|
57
|
+
- `tests/mypy/`: integration tests that run mypy on small modules and commit merged outputs
|
|
58
|
+
- `tests/mypy/configs/`: mypy configs (`*.ini`, `*.toml`)
|
|
59
|
+
- `tests/mypy/modules/`: tiny python modules checked by mypy
|
|
60
|
+
- `tests/mypy/outputs/`: golden outputs produced by merging mypy output into sources
|
|
61
|
+
|
|
62
|
+
## Invariants / gotchas (do not break these)
|
|
63
|
+
|
|
64
|
+
- **Plugin ordering matters**: if a user enables both `sqlmodel_mypy.plugin` and `pydantic.mypy`, this plugin must come **first**, otherwise Pydantic's plugin may "claim" SQLModel classes (see `README.md`).
|
|
65
|
+
- **Incremental mode safety**:
|
|
66
|
+
- Avoid global state.
|
|
67
|
+
- Persist per-class data in `TypeInfo.metadata` under a **JSON-serializable** key.
|
|
68
|
+
- Current key: `sqlmodel-mypy-metadata` (see `src/sqlmodel_mypy/transform.py`).
|
|
69
|
+
- **Idempotency**: class hooks may run multiple times. Code must tolerate re-entry and partial analysis.
|
|
70
|
+
- When types/defs aren't ready, call `api.defer()` and exit early.
|
|
71
|
+
- **Cache invalidation is mandatory**:
|
|
72
|
+
- Mypy caches plugin-produced metadata. If you change plugin behavior, bump `__version__` in `src/sqlmodel_mypy/plugin.py`.
|
|
73
|
+
- This value is included in `report_config_data()` and forces mypy to invalidate its cache.
|
|
74
|
+
|
|
75
|
+
## Golden test policy (how we lock down behavior)
|
|
76
|
+
|
|
77
|
+
- If you change expected mypy messages/signatures, regenerate outputs with `make update-mypy` and commit the changes under `tests/mypy/outputs/**`.
|
|
78
|
+
- Keep test modules minimal and focused: add a new file in `tests/mypy/modules/` and wire it into `tests/mypy/test_mypy.py`.
|
|
79
|
+
|
|
80
|
+
## Release process (PyPI, trusted publishing)
|
|
81
|
+
|
|
82
|
+
- Version is defined in `pyproject.toml`.
|
|
83
|
+
- To release:
|
|
84
|
+
- Bump the version.
|
|
85
|
+
- Tag `vX.Y.Z` and push the tag.
|
|
86
|
+
- GitHub Actions publishes to PyPI via **trusted publishing** (requires configuring the PyPI project + GitHub environment `pypi`).
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Artem
|
|
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,208 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: sqlmodel-mypy-plugin
|
|
3
|
+
Version: 0.12.0
|
|
4
|
+
Summary: Mypy plugin for SQLModel (better constructor/Field typing).
|
|
5
|
+
Project-URL: Homepage, https://github.com/sertione/sqlmodel-mypy-plugin
|
|
6
|
+
Project-URL: Repository, https://github.com/sertione/sqlmodel-mypy-plugin
|
|
7
|
+
Project-URL: Issues, https://github.com/sertione/sqlmodel-mypy-plugin/issues
|
|
8
|
+
Project-URL: Changelog, https://github.com/sertione/sqlmodel-mypy-plugin/releases
|
|
9
|
+
Author: Artem
|
|
10
|
+
License: MIT License
|
|
11
|
+
|
|
12
|
+
Copyright (c) 2026 Artem
|
|
13
|
+
|
|
14
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
15
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
16
|
+
in the Software without restriction, including without limitation the rights
|
|
17
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
18
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
19
|
+
furnished to do so, subject to the following conditions:
|
|
20
|
+
|
|
21
|
+
The above copyright notice and this permission notice shall be included in all
|
|
22
|
+
copies or substantial portions of the Software.
|
|
23
|
+
|
|
24
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
25
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
26
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
27
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
28
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
29
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
30
|
+
SOFTWARE.
|
|
31
|
+
License-File: LICENSE
|
|
32
|
+
Keywords: mypy,plugin,pydantic,sqlalchemy,sqlmodel,typing
|
|
33
|
+
Classifier: Development Status :: 3 - Alpha
|
|
34
|
+
Classifier: Intended Audience :: Developers
|
|
35
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
36
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
37
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
38
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
39
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
40
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
41
|
+
Classifier: Typing :: Typed
|
|
42
|
+
Requires-Python: >=3.10
|
|
43
|
+
Requires-Dist: mypy>=1.19.1
|
|
44
|
+
Requires-Dist: typing-extensions>=4.10.0
|
|
45
|
+
Description-Content-Type: text/markdown
|
|
46
|
+
|
|
47
|
+
# SQLModel Mypy plugin
|
|
48
|
+
|
|
49
|
+
Mypy plugin that improves type checking for [`SQLModel`](https://github.com/fastapi/sqlmodel) models.
|
|
50
|
+
|
|
51
|
+
## Status
|
|
52
|
+
|
|
53
|
+
Early/experimental. Implemented scope (supported today):
|
|
54
|
+
|
|
55
|
+
Planned work is tracked in [`ROADMAP.md`](ROADMAP.md).
|
|
56
|
+
|
|
57
|
+
- Generate correct `__init__` / `model_construct` signatures for SQLModel models:
|
|
58
|
+
- treat `sqlmodel.Field(...)` required/optional correctly
|
|
59
|
+
- accept `sqlmodel.Relationship(...)` kwargs for `table=True` models
|
|
60
|
+
- accept common `Field(...)` alias kwargs (`alias` / `validation_alias`) when statically known
|
|
61
|
+
- Improve SQLAlchemy expression typing for **class** attribute access on `table=True` models (e.g. `User.id`,
|
|
62
|
+
`User.name`, `Team.heroes`).
|
|
63
|
+
- Common SQLAlchemy helpers that operate on expressions / selects (e.g. `.label(...)`, `selectinload(...)`,
|
|
64
|
+
`execution_options(...)`) work without spurious mypy errors.
|
|
65
|
+
- Expose SQLAlchemy table metadata on `table=True` models (e.g. `User.__table__`).
|
|
66
|
+
- Relationship comparator typing on relationship attributes (e.g. `Team.heroes.any(...)`, `Hero.team.has(...)`,
|
|
67
|
+
`Team.heroes.contains(...)`).
|
|
68
|
+
- Outer-join `None` propagation in `select(A, B).join(B, isouter=True)` result tuples.
|
|
69
|
+
- Broaden `Session.exec()` / `AsyncSession.exec()` typing to accept SQLAlchemy `Executable` statements (e.g.
|
|
70
|
+
`text(...)`), not just `select(...)`.
|
|
71
|
+
- Accept SQLAlchemy TypeEngine instances in `Field(sa_type=...)` (e.g. `DateTime(timezone=True)`, `String(50)`)
|
|
72
|
+
without `# type: ignore` in strict mode.
|
|
73
|
+
- Accept `model_config = ConfigDict(...)` overrides on SQLModel subclasses in strict mode.
|
|
74
|
+
- Compatible with `pydantic.mypy` in either plugin order.
|
|
75
|
+
|
|
76
|
+
## Install (dev)
|
|
77
|
+
|
|
78
|
+
Prereqs: **Python 3.10+** and [`uv`](https://docs.astral.sh/uv/).
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
make install
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Run the full quality gate suite (same as CI):
|
|
85
|
+
|
|
86
|
+
```bash
|
|
87
|
+
make check
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Enable in mypy
|
|
91
|
+
|
|
92
|
+
`mypy.ini`:
|
|
93
|
+
|
|
94
|
+
```ini
|
|
95
|
+
[mypy]
|
|
96
|
+
plugins = sqlmodel_mypy.plugin
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
If you also use `pydantic.mypy`, you can list plugins in either order:
|
|
100
|
+
|
|
101
|
+
```ini
|
|
102
|
+
[mypy]
|
|
103
|
+
plugins = sqlmodel_mypy.plugin, pydantic.mypy
|
|
104
|
+
# or:
|
|
105
|
+
# plugins = pydantic.mypy, sqlmodel_mypy.plugin
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Plugin configuration
|
|
109
|
+
|
|
110
|
+
`mypy.ini`:
|
|
111
|
+
|
|
112
|
+
```ini
|
|
113
|
+
[sqlmodel-mypy]
|
|
114
|
+
init_typed = false
|
|
115
|
+
init_forbid_extra = false
|
|
116
|
+
warn_untyped_fields = true
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
`pyproject.toml`:
|
|
120
|
+
|
|
121
|
+
```toml
|
|
122
|
+
[tool.sqlmodel-mypy]
|
|
123
|
+
init_typed = false
|
|
124
|
+
init_forbid_extra = false
|
|
125
|
+
warn_untyped_fields = true
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## Error codes
|
|
129
|
+
|
|
130
|
+
- `sqlmodel-field`: field-related plugin errors (e.g. untyped `x = Field(...)`).
|
|
131
|
+
|
|
132
|
+
## SQL expression typing
|
|
133
|
+
|
|
134
|
+
This plugin adjusts **class attribute** types on `table=True` SQLModel models to behave like SQLAlchemy
|
|
135
|
+
expressions, so you can write queries without `col()`:
|
|
136
|
+
|
|
137
|
+
```py
|
|
138
|
+
from sqlmodel import Field, SQLModel, select
|
|
139
|
+
|
|
140
|
+
class User(SQLModel, table=True):
|
|
141
|
+
id: int = Field(primary_key=True)
|
|
142
|
+
name: str = Field()
|
|
143
|
+
|
|
144
|
+
stmt = select(User).where(User.name.like("%x%"))
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
You can also use SQLAlchemy table metadata attributes without `attr-defined` noise:
|
|
148
|
+
|
|
149
|
+
```py
|
|
150
|
+
tbl = User.__table__
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Relationship comparator typing
|
|
154
|
+
|
|
155
|
+
Relationship attributes declared via `Relationship(...)` are typed as SQLAlchemy expressions at class level,
|
|
156
|
+
including common relationship comparators used in query filters:
|
|
157
|
+
|
|
158
|
+
```py
|
|
159
|
+
stmt = select(Team).where(Team.heroes.any(Hero.name == "x"))
|
|
160
|
+
stmt = select(Hero).where(Hero.team.has(Team.name == "t"))
|
|
161
|
+
stmt = select(Team).where(Team.heroes.contains(hero_obj))
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Out of scope
|
|
165
|
+
|
|
166
|
+
- **`column_property(...)` and similar SQLAlchemy helpers**: this plugin doesn’t try to “fix” the *declaration-site*
|
|
167
|
+
typing for SQLAlchemy-only APIs that aren’t part of SQLModel’s surface area. Prefer SQLAlchemy-typed patterns like
|
|
168
|
+
`Mapped[...]` / `InstrumentedAttribute[...]` (or local casts/`# type: ignore` if needed).
|
|
169
|
+
- **Pydantic `@computed_field`**: not touched by this plugin (it should type-check via normal property typing / Pydantic
|
|
170
|
+
typing).
|
|
171
|
+
|
|
172
|
+
## Typing strategy (defaults)
|
|
173
|
+
|
|
174
|
+
- **Plugin hooks**: SQLModel-specific behavior (constructor signatures, `table=True` expression typing, etc.).
|
|
175
|
+
- **SQLAlchemy typing**: relied upon for core SQL/ORM typing wherever possible.
|
|
176
|
+
- **Stub overlays**: only for upstream gaps that can’t be addressed cleanly via hooks (prefer upstream fixes first).
|
|
177
|
+
|
|
178
|
+
## Field aliases in constructor kwargs
|
|
179
|
+
|
|
180
|
+
If you use `Field(alias=...)` (or `validation_alias=...` / `schema_extra={"validation_alias": ...}`), the plugin
|
|
181
|
+
adds the alias name as an accepted keyword argument in generated `__init__` / `model_construct` signatures.
|
|
182
|
+
This avoids false-positive “unexpected keyword argument” errors when `init_forbid_extra=true`.
|
|
183
|
+
|
|
184
|
+
Limitations:
|
|
185
|
+
|
|
186
|
+
- Only **string-literal** aliases are recognized (e.g. `Field(alias=\"full_name\")`).
|
|
187
|
+
- Aliases that are not valid Python identifiers (or that collide with other parameter names) are ignored.
|
|
188
|
+
- Mypy can’t express “either field name or alias is required”, so aliased required fields may not be reported as
|
|
189
|
+
missing at type-check time.
|
|
190
|
+
|
|
191
|
+
## Development
|
|
192
|
+
|
|
193
|
+
- List available commands: `make help`
|
|
194
|
+
- Lint/format: `make lint` / `make fmt`
|
|
195
|
+
- Typecheck plugin code: `make typecheck`
|
|
196
|
+
- Run tests: `make test`
|
|
197
|
+
- Run all quality gates: `make check`
|
|
198
|
+
- Update mypy \u201cgolden\u201d outputs: `make update-mypy`
|
|
199
|
+
|
|
200
|
+
See [`AGENTS.md`](AGENTS.md) for repo conventions and workflow notes.
|
|
201
|
+
|
|
202
|
+
## Security
|
|
203
|
+
|
|
204
|
+
See [`SECURITY.md`](SECURITY.md).
|
|
205
|
+
|
|
206
|
+
## Code of conduct
|
|
207
|
+
|
|
208
|
+
See [`CODE_OF_CONDUCT.md`](CODE_OF_CONDUCT.md).
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# SQLModel Mypy plugin
|
|
2
|
+
|
|
3
|
+
Mypy plugin that improves type checking for [`SQLModel`](https://github.com/fastapi/sqlmodel) models.
|
|
4
|
+
|
|
5
|
+
## Status
|
|
6
|
+
|
|
7
|
+
Early/experimental. Implemented scope (supported today):
|
|
8
|
+
|
|
9
|
+
Planned work is tracked in [`ROADMAP.md`](ROADMAP.md).
|
|
10
|
+
|
|
11
|
+
- Generate correct `__init__` / `model_construct` signatures for SQLModel models:
|
|
12
|
+
- treat `sqlmodel.Field(...)` required/optional correctly
|
|
13
|
+
- accept `sqlmodel.Relationship(...)` kwargs for `table=True` models
|
|
14
|
+
- accept common `Field(...)` alias kwargs (`alias` / `validation_alias`) when statically known
|
|
15
|
+
- Improve SQLAlchemy expression typing for **class** attribute access on `table=True` models (e.g. `User.id`,
|
|
16
|
+
`User.name`, `Team.heroes`).
|
|
17
|
+
- Common SQLAlchemy helpers that operate on expressions / selects (e.g. `.label(...)`, `selectinload(...)`,
|
|
18
|
+
`execution_options(...)`) work without spurious mypy errors.
|
|
19
|
+
- Expose SQLAlchemy table metadata on `table=True` models (e.g. `User.__table__`).
|
|
20
|
+
- Relationship comparator typing on relationship attributes (e.g. `Team.heroes.any(...)`, `Hero.team.has(...)`,
|
|
21
|
+
`Team.heroes.contains(...)`).
|
|
22
|
+
- Outer-join `None` propagation in `select(A, B).join(B, isouter=True)` result tuples.
|
|
23
|
+
- Broaden `Session.exec()` / `AsyncSession.exec()` typing to accept SQLAlchemy `Executable` statements (e.g.
|
|
24
|
+
`text(...)`), not just `select(...)`.
|
|
25
|
+
- Accept SQLAlchemy TypeEngine instances in `Field(sa_type=...)` (e.g. `DateTime(timezone=True)`, `String(50)`)
|
|
26
|
+
without `# type: ignore` in strict mode.
|
|
27
|
+
- Accept `model_config = ConfigDict(...)` overrides on SQLModel subclasses in strict mode.
|
|
28
|
+
- Compatible with `pydantic.mypy` in either plugin order.
|
|
29
|
+
|
|
30
|
+
## Install (dev)
|
|
31
|
+
|
|
32
|
+
Prereqs: **Python 3.10+** and [`uv`](https://docs.astral.sh/uv/).
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
make install
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Run the full quality gate suite (same as CI):
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
make check
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Enable in mypy
|
|
45
|
+
|
|
46
|
+
`mypy.ini`:
|
|
47
|
+
|
|
48
|
+
```ini
|
|
49
|
+
[mypy]
|
|
50
|
+
plugins = sqlmodel_mypy.plugin
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
If you also use `pydantic.mypy`, you can list plugins in either order:
|
|
54
|
+
|
|
55
|
+
```ini
|
|
56
|
+
[mypy]
|
|
57
|
+
plugins = sqlmodel_mypy.plugin, pydantic.mypy
|
|
58
|
+
# or:
|
|
59
|
+
# plugins = pydantic.mypy, sqlmodel_mypy.plugin
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Plugin configuration
|
|
63
|
+
|
|
64
|
+
`mypy.ini`:
|
|
65
|
+
|
|
66
|
+
```ini
|
|
67
|
+
[sqlmodel-mypy]
|
|
68
|
+
init_typed = false
|
|
69
|
+
init_forbid_extra = false
|
|
70
|
+
warn_untyped_fields = true
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
`pyproject.toml`:
|
|
74
|
+
|
|
75
|
+
```toml
|
|
76
|
+
[tool.sqlmodel-mypy]
|
|
77
|
+
init_typed = false
|
|
78
|
+
init_forbid_extra = false
|
|
79
|
+
warn_untyped_fields = true
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Error codes
|
|
83
|
+
|
|
84
|
+
- `sqlmodel-field`: field-related plugin errors (e.g. untyped `x = Field(...)`).
|
|
85
|
+
|
|
86
|
+
## SQL expression typing
|
|
87
|
+
|
|
88
|
+
This plugin adjusts **class attribute** types on `table=True` SQLModel models to behave like SQLAlchemy
|
|
89
|
+
expressions, so you can write queries without `col()`:
|
|
90
|
+
|
|
91
|
+
```py
|
|
92
|
+
from sqlmodel import Field, SQLModel, select
|
|
93
|
+
|
|
94
|
+
class User(SQLModel, table=True):
|
|
95
|
+
id: int = Field(primary_key=True)
|
|
96
|
+
name: str = Field()
|
|
97
|
+
|
|
98
|
+
stmt = select(User).where(User.name.like("%x%"))
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
You can also use SQLAlchemy table metadata attributes without `attr-defined` noise:
|
|
102
|
+
|
|
103
|
+
```py
|
|
104
|
+
tbl = User.__table__
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Relationship comparator typing
|
|
108
|
+
|
|
109
|
+
Relationship attributes declared via `Relationship(...)` are typed as SQLAlchemy expressions at class level,
|
|
110
|
+
including common relationship comparators used in query filters:
|
|
111
|
+
|
|
112
|
+
```py
|
|
113
|
+
stmt = select(Team).where(Team.heroes.any(Hero.name == "x"))
|
|
114
|
+
stmt = select(Hero).where(Hero.team.has(Team.name == "t"))
|
|
115
|
+
stmt = select(Team).where(Team.heroes.contains(hero_obj))
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Out of scope
|
|
119
|
+
|
|
120
|
+
- **`column_property(...)` and similar SQLAlchemy helpers**: this plugin doesn’t try to “fix” the *declaration-site*
|
|
121
|
+
typing for SQLAlchemy-only APIs that aren’t part of SQLModel’s surface area. Prefer SQLAlchemy-typed patterns like
|
|
122
|
+
`Mapped[...]` / `InstrumentedAttribute[...]` (or local casts/`# type: ignore` if needed).
|
|
123
|
+
- **Pydantic `@computed_field`**: not touched by this plugin (it should type-check via normal property typing / Pydantic
|
|
124
|
+
typing).
|
|
125
|
+
|
|
126
|
+
## Typing strategy (defaults)
|
|
127
|
+
|
|
128
|
+
- **Plugin hooks**: SQLModel-specific behavior (constructor signatures, `table=True` expression typing, etc.).
|
|
129
|
+
- **SQLAlchemy typing**: relied upon for core SQL/ORM typing wherever possible.
|
|
130
|
+
- **Stub overlays**: only for upstream gaps that can’t be addressed cleanly via hooks (prefer upstream fixes first).
|
|
131
|
+
|
|
132
|
+
## Field aliases in constructor kwargs
|
|
133
|
+
|
|
134
|
+
If you use `Field(alias=...)` (or `validation_alias=...` / `schema_extra={"validation_alias": ...}`), the plugin
|
|
135
|
+
adds the alias name as an accepted keyword argument in generated `__init__` / `model_construct` signatures.
|
|
136
|
+
This avoids false-positive “unexpected keyword argument” errors when `init_forbid_extra=true`.
|
|
137
|
+
|
|
138
|
+
Limitations:
|
|
139
|
+
|
|
140
|
+
- Only **string-literal** aliases are recognized (e.g. `Field(alias=\"full_name\")`).
|
|
141
|
+
- Aliases that are not valid Python identifiers (or that collide with other parameter names) are ignored.
|
|
142
|
+
- Mypy can’t express “either field name or alias is required”, so aliased required fields may not be reported as
|
|
143
|
+
missing at type-check time.
|
|
144
|
+
|
|
145
|
+
## Development
|
|
146
|
+
|
|
147
|
+
- List available commands: `make help`
|
|
148
|
+
- Lint/format: `make lint` / `make fmt`
|
|
149
|
+
- Typecheck plugin code: `make typecheck`
|
|
150
|
+
- Run tests: `make test`
|
|
151
|
+
- Run all quality gates: `make check`
|
|
152
|
+
- Update mypy \u201cgolden\u201d outputs: `make update-mypy`
|
|
153
|
+
|
|
154
|
+
See [`AGENTS.md`](AGENTS.md) for repo conventions and workflow notes.
|
|
155
|
+
|
|
156
|
+
## Security
|
|
157
|
+
|
|
158
|
+
See [`SECURITY.md`](SECURITY.md).
|
|
159
|
+
|
|
160
|
+
## Code of conduct
|
|
161
|
+
|
|
162
|
+
See [`CODE_OF_CONDUCT.md`](CODE_OF_CONDUCT.md).
|