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()

legacy-relationship (SA203)

What it does

Checks for existence of relationship definition with backref keyword argument.

Why is this bad?

backref is considered legacy. It adds dynamic attributes that type checkers and code completion cannot understand.

Example

from typing import List

from sqlalchemy import ForeignKey

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

class Base(DeclarativeBase):
    pass


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

    children: Mapped[List["Child"]] = relationship(backref="parent")


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

    parent_id: Mapped[int] = mapped_column(ForeignKey("parent.id"))


c = Child()
p = Parent(children=[c])
c.parent  # error: "Child" has no attribute "parent"; maybe "parent_id"?  [attr-defined]

Use instead:

from typing import List

from sqlalchemy import ForeignKey

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

class Base(DeclarativeBase):
  pass


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

    children: Mapped[List["Child"]] = relationship(back_populates="parent")


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

    parent_id: Mapped[int] = mapped_column(ForeignKey("parent.id"))
    parent: Mapped["Parent"] = relationship(back_populates="children")

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.3.0.tar.gz (39.2 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.3.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flake8_sqlalchemy2-0.3.0.tar.gz
  • Upload date:
  • Size: 39.2 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.3.0.tar.gz
Algorithm Hash digest
SHA256 61131d516babf548bd2ddcc6fb8660a8dba5595d600588c24ec8d31f1d9e4b97
MD5 8df5dcadeb3e6888294b2de180ed9f18
BLAKE2b-256 5f3b9587369b77a925c634961b4465b5b7273a09f712c884a4c28bbcc828bba3

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for flake8_sqlalchemy2-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6da33bbc132c6d5f5a0f195e43dd8b2db92d0017c2f9251be01d3a6bd696258b
MD5 9839afd4ba213ea2d646625f0f8d05c1
BLAKE2b-256 624835c1e8d3bb73d5172a6c027d6b326bd1c7c8914e2e82ab42b6af0b4f9978

See more details on using hashes here.

Provenance

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