Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flake8_sqlalchemy2-0.1.0.tar.gz (27.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flake8_sqlalchemy2-0.1.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

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

Hashes for flake8_sqlalchemy2-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bde45fe40c3c54549d80e7c68dd1ec4c16aae187558663df6f2990df53182834
MD5 c2ac0c0f32f6a19b9f8ab317e185d3e3
BLAKE2b-256 306f960a2ae68684686631232c1e1bdeb8ade9053be1929a2b29f55c41d4c922

See more details on using hashes here.

Provenance

The following attestation bundles were made for flake8_sqlalchemy2-0.1.0.tar.gz:

Publisher: main.yml on kreathon/flake8-sqlalchemy2

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file flake8_sqlalchemy2-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flake8_sqlalchemy2-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a0fc5f89c1f273e59ea939ce2098e0703a2def65fc6385d92ec7beb2107d9859
MD5 0b40ea566be1c1156662c8b1906c769a
BLAKE2b-256 5f49bdb4de221c62e19d4f5cdaaf8c8601e164b42d5b7529ddc72beaa3dacdd7

See more details on using hashes here.

Provenance

The following attestation bundles were made for flake8_sqlalchemy2-0.1.0-py3-none-any.whl:

Publisher: main.yml on kreathon/flake8-sqlalchemy2

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page