[ruff] Skip SQLModel base classes for mutable-class-default (RUF012) (#14949)

## Summary

Closes https://github.com/astral-sh/ruff/issues/14892, by adding
`sqlmodel.SQLModel` to the list of classes with default copy semantics.

## Test Plan

Added a test into `RUF012.py` containing the example from the original
issue.
This commit is contained in:
Krishnan Chandra 2024-12-12 23:19:21 -05:00 committed by GitHub
parent 657d26ff20
commit be4ce16735
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 36 additions and 0 deletions

View file

@ -79,3 +79,27 @@ class H(BaseModel):
without_annotation = []
class_variable: ClassVar[list[int]] = []
final_variable: Final[list[int]] = []
def sqlmodel_import_checker():
from sqlmodel.main import SQLModel
class I(SQLModel):
id: int
mutable_default: list[int] = []
from sqlmodel import SQLModel
class J(SQLModel):
id: int
name: str
class K(SQLModel):
id: int
i_s: list[J] = []
class L(SQLModel):
id: int
i_j: list[K] = list()

View file

@ -187,6 +187,7 @@ pub(super) fn has_default_copy_semantics(
["pydantic", "BaseModel" | "BaseSettings" | "BaseConfig"]
| ["pydantic_settings", "BaseSettings"]
| ["msgspec", "Struct"]
| ["sqlmodel", "SQLModel"]
)
})
}

View file

@ -1,5 +1,6 @@
---
source: crates/ruff_linter/src/rules/ruff/mod.rs
assertion_line: 82
snapshot_kind: text
---
RUF012.py:9:34: RUF012 Mutable class attributes should be annotated with `typing.ClassVar`
@ -31,3 +32,13 @@ RUF012.py:25:26: RUF012 Mutable class attributes should be annotated with `typin
26 | perfectly_fine: list[int] = field(default_factory=list)
27 | class_variable: ClassVar[list[int]] = []
|
RUF012.py:89:38: RUF012 Mutable class attributes should be annotated with `typing.ClassVar`
|
87 | class I(SQLModel):
88 | id: int
89 | mutable_default: list[int] = []
| ^^ RUF012
90 |
91 | from sqlmodel import SQLModel
|