Skip to content

Commit 8bc11c4

Browse files
[flake8-django] Recognize other magic methods (DJ012) (#15365)
1 parent bf5b0c2 commit 8bc11c4

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

crates/ruff_linter/resources/test/fixtures/flake8_django/DJ012.py

+8
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,11 @@ def __str__(self):
145145

146146
first_name = models.CharField(max_length=32)
147147

148+
149+
# https://github.com/astral-sh/ruff/issues/13892
150+
class DunderMethodOtherThanStrBeforeSave(models.Model):
151+
name = models.CharField()
152+
153+
def __init__(self, *args, **kwargs): ...
154+
155+
def save(*args, **kwargs): ...

crates/ruff_linter/src/rules/flake8_django/rules/unordered_body_content_in_model.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::fmt;
22

33
use ruff_diagnostics::{Diagnostic, Violation};
44
use ruff_macros::{derive_message_formats, ViolationMetadata};
5+
use ruff_python_ast::helpers::is_dunder;
56
use ruff_python_ast::{self as ast, Expr, Stmt};
67
use ruff_python_semantic::{Modules, SemanticModel};
78
use ruff_text_size::Ranged;
@@ -131,7 +132,7 @@ enum ContentType {
131132
FieldDeclaration,
132133
ManagerDeclaration,
133134
MetaClass,
134-
StrMethod,
135+
MagicMethod,
135136
SaveMethod,
136137
GetAbsoluteUrlMethod,
137138
CustomMethod,
@@ -143,7 +144,7 @@ impl fmt::Display for ContentType {
143144
ContentType::FieldDeclaration => f.write_str("field declaration"),
144145
ContentType::ManagerDeclaration => f.write_str("manager declaration"),
145146
ContentType::MetaClass => f.write_str("`Meta` class"),
146-
ContentType::StrMethod => f.write_str("`__str__` method"),
147+
ContentType::MagicMethod => f.write_str("Magic method"),
147148
ContentType::SaveMethod => f.write_str("`save` method"),
148149
ContentType::GetAbsoluteUrlMethod => f.write_str("`get_absolute_url` method"),
149150
ContentType::CustomMethod => f.write_str("custom method"),
@@ -177,7 +178,7 @@ fn get_element_type(element: &Stmt, semantic: &SemanticModel) -> Option<ContentT
177178
}
178179
}
179180
Stmt::FunctionDef(ast::StmtFunctionDef { name, .. }) => match name.as_str() {
180-
"__str__" => Some(ContentType::StrMethod),
181+
name if is_dunder(name) => Some(ContentType::MagicMethod),
181182
"save" => Some(ContentType::SaveMethod),
182183
"get_absolute_url" => Some(ContentType::GetAbsoluteUrlMethod),
183184
_ => Some(ContentType::CustomMethod),

crates/ruff_linter/src/rules/flake8_django/snapshots/ruff_linter__rules__flake8_django__tests__DJ012_DJ012.py.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ DJ012.py:43:5: DJ012 Order of model's inner classes, methods, and fields does no
1818
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ DJ012
1919
|
2020

21-
DJ012.py:56:5: DJ012 Order of model's inner classes, methods, and fields does not follow the Django Style Guide: `__str__` method should come before custom method
21+
DJ012.py:56:5: DJ012 Order of model's inner classes, methods, and fields does not follow the Django Style Guide: Magic method should come before custom method
2222
|
2323
54 | pass
2424
55 |

0 commit comments

Comments
 (0)