From a0d8f7fddc3db758cf2f330488f8e8df99d9c813 Mon Sep 17 00:00:00 2001 From: InSyncWithFoo Date: Wed, 8 Jan 2025 21:14:59 +0000 Subject: [PATCH] [`flake8-django`] Recognize other magic methods (`DJ012`) --- .../resources/test/fixtures/flake8_django/DJ012.py | 8 ++++++++ .../rules/unordered_body_content_in_model.rs | 7 ++++--- ...nter__rules__flake8_django__tests__DJ012_DJ012.py.snap | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/crates/ruff_linter/resources/test/fixtures/flake8_django/DJ012.py b/crates/ruff_linter/resources/test/fixtures/flake8_django/DJ012.py index 20d3e4078ba96..6a0079ed5b50d 100644 --- a/crates/ruff_linter/resources/test/fixtures/flake8_django/DJ012.py +++ b/crates/ruff_linter/resources/test/fixtures/flake8_django/DJ012.py @@ -145,3 +145,11 @@ def __str__(self): first_name = models.CharField(max_length=32) + +# https://github.com/astral-sh/ruff/issues/13892 +class DunderMethodOtherThanStrBeforeSave(models.Model): + name = models.CharField() + + def __init__(self, *args, **kwargs): ... + + def save(*args, **kwargs): ... diff --git a/crates/ruff_linter/src/rules/flake8_django/rules/unordered_body_content_in_model.rs b/crates/ruff_linter/src/rules/flake8_django/rules/unordered_body_content_in_model.rs index 14adaf6277fb0..f5ac17c4837df 100644 --- a/crates/ruff_linter/src/rules/flake8_django/rules/unordered_body_content_in_model.rs +++ b/crates/ruff_linter/src/rules/flake8_django/rules/unordered_body_content_in_model.rs @@ -2,6 +2,7 @@ use std::fmt; use ruff_diagnostics::{Diagnostic, Violation}; use ruff_macros::{derive_message_formats, ViolationMetadata}; +use ruff_python_ast::helpers::is_dunder; use ruff_python_ast::{self as ast, Expr, Stmt}; use ruff_python_semantic::{Modules, SemanticModel}; use ruff_text_size::Ranged; @@ -131,7 +132,7 @@ enum ContentType { FieldDeclaration, ManagerDeclaration, MetaClass, - StrMethod, + MagicMethod, SaveMethod, GetAbsoluteUrlMethod, CustomMethod, @@ -143,7 +144,7 @@ impl fmt::Display for ContentType { ContentType::FieldDeclaration => f.write_str("field declaration"), ContentType::ManagerDeclaration => f.write_str("manager declaration"), ContentType::MetaClass => f.write_str("`Meta` class"), - ContentType::StrMethod => f.write_str("`__str__` method"), + ContentType::MagicMethod => f.write_str("Magic method"), ContentType::SaveMethod => f.write_str("`save` method"), ContentType::GetAbsoluteUrlMethod => f.write_str("`get_absolute_url` method"), ContentType::CustomMethod => f.write_str("custom method"), @@ -177,7 +178,7 @@ fn get_element_type(element: &Stmt, semantic: &SemanticModel) -> Option match name.as_str() { - "__str__" => Some(ContentType::StrMethod), + name if is_dunder(name) => Some(ContentType::MagicMethod), "save" => Some(ContentType::SaveMethod), "get_absolute_url" => Some(ContentType::GetAbsoluteUrlMethod), _ => Some(ContentType::CustomMethod), diff --git a/crates/ruff_linter/src/rules/flake8_django/snapshots/ruff_linter__rules__flake8_django__tests__DJ012_DJ012.py.snap b/crates/ruff_linter/src/rules/flake8_django/snapshots/ruff_linter__rules__flake8_django__tests__DJ012_DJ012.py.snap index c0910c2e2d58e..5729bd6701af1 100644 --- a/crates/ruff_linter/src/rules/flake8_django/snapshots/ruff_linter__rules__flake8_django__tests__DJ012_DJ012.py.snap +++ b/crates/ruff_linter/src/rules/flake8_django/snapshots/ruff_linter__rules__flake8_django__tests__DJ012_DJ012.py.snap @@ -18,7 +18,7 @@ DJ012.py:43:5: DJ012 Order of model's inner classes, methods, and fields does no | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ DJ012 | -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 +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 | 54 | pass 55 |