Skip to content

RUF012 should not trigger on SQLModel models #14892

@scy

Description

@scy

SQLModel is a project by FastAPI creator Sebastián Ramírez that combines SQLAlchemy models with Pydantic models, allowing you to use Pydantic-validated models for database interactions without duplicating classes.

The base class for these models is SQLModel. As usual for Pydantic models, they can have mutable default values which will be copied when creating an actual instance. However, Ruff's RUF012 triggers on these models.

Afaict, Ruff already has exceptions for Pydantic's BaseModel and similar classes; I guess these should basically be extended to the SQLModel class…?

The SQLModel code says that it's a subclass of BaseModel, not sure why Ruff doesn't recognize it; Python does.

The following code demonstrates the behavior:

from __future__ import annotations

from pydantic import BaseModel
from sqlmodel import SQLModel


class Foo(SQLModel):
    id: int
    bars: list[Bar] = []


class Bar(SQLModel):
    id: int
    name: str


one = Foo(id=1)
two = Foo(id=2)

one.bars.append(Bar(id=1, name="one"))
two.bars.append(Bar(id=2, name="two"))

print(one.bars)  # [Bar(id=1, name='one')]
print(two.bars)  # [Bar(id=2, name='two')]
print(isinstance(one, BaseModel))  # True

Thanks in advance for looking into this!

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedReady for implementationhelp wantedContributions especially welcomeruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions