sqlmodel-mypy-plugin 0.12.0__py3-none-any.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.
- sqlmodel_mypy/__init__.py +3 -0
- sqlmodel_mypy/plugin.py +1140 -0
- sqlmodel_mypy/py.typed +1 -0
- sqlmodel_mypy/transform.py +836 -0
- sqlmodel_mypy_plugin-0.12.0.dist-info/METADATA +208 -0
- sqlmodel_mypy_plugin-0.12.0.dist-info/RECORD +8 -0
- sqlmodel_mypy_plugin-0.12.0.dist-info/WHEEL +4 -0
- sqlmodel_mypy_plugin-0.12.0.dist-info/licenses/LICENSE +21 -0
|
@@ -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,8 @@
|
|
|
1
|
+
sqlmodel_mypy/__init__.py,sha256=2JeqbMHTaN5gXh4SZSyewQh4BVssJaOjJAWvvPC2N58,50
|
|
2
|
+
sqlmodel_mypy/plugin.py,sha256=C35PWP1xbC6jmfnXc5o2y73py1y1XSGv-jNI0FjnOBI,45772
|
|
3
|
+
sqlmodel_mypy/py.typed,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
|
4
|
+
sqlmodel_mypy/transform.py,sha256=7826ZoIqgfPHPsOqJojxWREiE9W4IeGFko9rvp1Rufo,30607
|
|
5
|
+
sqlmodel_mypy_plugin-0.12.0.dist-info/METADATA,sha256=-QcqmPGwKPIDYT__Imi_8c9wR1-nLMlFLuHf9O918v8,7684
|
|
6
|
+
sqlmodel_mypy_plugin-0.12.0.dist-info/WHEEL,sha256=WLgqFyCfm_KASv4WHyYy0P3pM_m7J5L9k2skdKLirC8,87
|
|
7
|
+
sqlmodel_mypy_plugin-0.12.0.dist-info/licenses/LICENSE,sha256=2vTtTMBZTlDHJyeU3ehwS7VdCCnprwCnVvv2q52NUJ8,1062
|
|
8
|
+
sqlmodel_mypy_plugin-0.12.0.dist-info/RECORD,,
|
|
@@ -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.
|