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"

legacy-collection (SA202)

What it does

Checks for existence of DynamicMapped.

Why is this bad?

DynamicMapped is considered legacy and exposes the legacy query API.

Example

from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import DynamicMapped
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import relationship

class Base(DeclarativeBase):
    pass


class MyModel(Base):
    __tablename__ = "my_model"
    id: Mapped[int] = mapped_column(primary_key=True)

    children: DynamicMapped["Child"] = relationship()

Use instead:

from sqlalchemy.orm import DeclarativeBase
from sqlalchemy.orm import mapped_column
from sqlalchemy.orm import Mapped
from sqlalchemy.orm import relationship
from sqlalchemy.orm import WriteOnlyMapped

class Base(DeclarativeBase):
  pass


class MyModel(Base):
  __tablename__ = "my_model"
  id: Mapped[int] = mapped_column(primary_key=True)

  children: WriteOnlyMapped["Child"] = relationship()

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.2.0.tar.gz (27.8 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.2.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file flake8_sqlalchemy2-0.2.0.tar.gz.

File metadata

  • Download URL: flake8_sqlalchemy2-0.2.0.tar.gz
  • Upload date:
  • Size: 27.8 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.2.0.tar.gz
Algorithm Hash digest
SHA256 2327e48e7c5726a1368c3c6ea6ab441913c7f3e9f07d26e551e775860ae590cf
MD5 ebd41ed23817a2aeb59a7585308ec133
BLAKE2b-256 1b719e6843b6625662cd5787fde5bc992e446aa799d20f2d2aee0ce3cd917525

See more details on using hashes here.

Provenance

The following attestation bundles were made for flake8_sqlalchemy2-0.2.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.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flake8_sqlalchemy2-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 24bbed523372682344497745f4b22cb40e822575d6a98b46504be62ebba372e7
MD5 6dd2d98e25d3bdc2aa9d88e7d13862dc
BLAKE2b-256 327e0ae5e37d7710a69163822d71dff5799103dd56ffdbee69d5c60ab91506ce

See more details on using hashes here.

Provenance

The following attestation bundles were made for flake8_sqlalchemy2-0.2.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