flake8 plugin for SQLAlchemy 2.0
Project description
flake8-sqlalchemy2
flake8 plugin to enforce modern, typed SQLAlchemy 2.0.
Installation
Use uvx for a one-time check of your code base:
uvx --with flake8-sqlalchemy2 flake8 --select SA2
Install via pip for using as "permanent" flake8 plugin:
pip install flake8-sqlalchemy2
Rules
missing-mapped-type-annotation (SA201)
What it does
Checks for existence of Mapped or other ORM container class type annotations in SQLAlchemy
models.
Why is this bad?
If an annotation is missing, type checkers will treat the corresponding field as type Any.
Example
from sqlalchemy import Integer
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
class Base(DeclarativeBase):
pass
class MyModel(Base):
__tablename__ = "my_model"
id: Mapped[int] = mapped_column(primary_key=True)
count = mapped_column(Integer)
m = MyModel()
reveal_type(m.count) # note: Revealed type is "Any"
Use instead:
from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import mapped_column
class Base(DeclarativeBase):
pass
class MyModel(Base):
__tablename__ = "my_model"
id: Mapped[int] = mapped_column(primary_key=True)
count: Mapped[int]
m = MyModel()
reveal_type(m.count) # note: Revealed type is "builtins.int"
Note on ruff
Q: Why still use flake8 when there is ruff!?
A: For rules not supported by ruff. There is a proposed merge request to bring the first SQLAlchemy linting rule (SA201) to ruff ("needs-decision" tagged).
Note on flake8-sqlalchemy
Q: Why not integrate these rules into flake8-sqlalchemy?
A: The focus of this package are rules for modern, typed SQLAlchemy. Furthermore, I wanted to learn something new.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flake8_sqlalchemy2-0.1.0.tar.gz.
File metadata
- Download URL: flake8_sqlalchemy2-0.1.0.tar.gz
- Upload date:
- Size: 27.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bde45fe40c3c54549d80e7c68dd1ec4c16aae187558663df6f2990df53182834
|
|
| MD5 |
c2ac0c0f32f6a19b9f8ab317e185d3e3
|
|
| BLAKE2b-256 |
306f960a2ae68684686631232c1e1bdeb8ade9053be1929a2b29f55c41d4c922
|
Provenance
The following attestation bundles were made for flake8_sqlalchemy2-0.1.0.tar.gz:
Publisher:
main.yml on kreathon/flake8-sqlalchemy2
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
flake8_sqlalchemy2-0.1.0.tar.gz -
Subject digest:
bde45fe40c3c54549d80e7c68dd1ec4c16aae187558663df6f2990df53182834 - Sigstore transparency entry: 955534017
- Sigstore integration time:
-
Permalink:
kreathon/flake8-sqlalchemy2@d0fa9b7167484f263d32f7541c19db955794b0b6 -
Branch / Tag:
refs/tags/0.1 - Owner: https://github.com/kreathon
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
main.yml@d0fa9b7167484f263d32f7541c19db955794b0b6 -
Trigger Event:
push
-
Statement type:
File details
Details for the file flake8_sqlalchemy2-0.1.0-py3-none-any.whl.
File metadata
- Download URL: flake8_sqlalchemy2-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a0fc5f89c1f273e59ea939ce2098e0703a2def65fc6385d92ec7beb2107d9859
|
|
| MD5 |
0b40ea566be1c1156662c8b1906c769a
|
|
| BLAKE2b-256 |
5f49bdb4de221c62e19d4f5cdaaf8c8601e164b42d5b7529ddc72beaa3dacdd7
|
Provenance
The following attestation bundles were made for flake8_sqlalchemy2-0.1.0-py3-none-any.whl:
Publisher:
main.yml on kreathon/flake8-sqlalchemy2
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
flake8_sqlalchemy2-0.1.0-py3-none-any.whl -
Subject digest:
a0fc5f89c1f273e59ea939ce2098e0703a2def65fc6385d92ec7beb2107d9859 - Sigstore transparency entry: 955534025
- Sigstore integration time:
-
Permalink:
kreathon/flake8-sqlalchemy2@d0fa9b7167484f263d32f7541c19db955794b0b6 -
Branch / Tag:
refs/tags/0.1 - Owner: https://github.com/kreathon
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
main.yml@d0fa9b7167484f263d32f7541c19db955794b0b6 -
Trigger Event:
push
-
Statement type: