-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Description
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)) # TrueThanks in advance for looking into this!