|
| 1 | +use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation}; |
| 2 | +use ruff_macros::{derive_message_formats, violation}; |
| 3 | +use ruff_python_ast as ast; |
| 4 | +use ruff_python_semantic::analyze::function_type; |
| 5 | +use ruff_python_semantic::Scope; |
| 6 | +use ruff_text_size::Ranged; |
| 7 | + |
| 8 | +use crate::checkers::ast::Checker; |
| 9 | +use crate::importer::ImportRequest; |
| 10 | + |
| 11 | +/// ## What it does |
| 12 | +/// Checks for `@singledispatchmethod` decorators on functions or static methods. |
| 13 | +/// |
| 14 | +/// ## Why is this bad? |
| 15 | +/// The `@singledispatchmethod` decorator is intended for use with class and instance methods, not functions. |
| 16 | +/// |
| 17 | +/// Instead, use the `@singledispatch` decorator. |
| 18 | +/// |
| 19 | +/// ## Example |
| 20 | +/// ```python |
| 21 | +/// from functools import singledispatchmethod |
| 22 | +/// |
| 23 | +/// |
| 24 | +/// @singledispatchmethod |
| 25 | +/// def foo(arg): |
| 26 | +/// ... |
| 27 | +/// ``` |
| 28 | +/// |
| 29 | +/// Use instead: |
| 30 | +/// ```python |
| 31 | +/// from functools import singledispatchmethod |
| 32 | +/// |
| 33 | +/// |
| 34 | +/// @singledispatch |
| 35 | +/// def foo(arg): |
| 36 | +/// ... |
| 37 | +/// ``` |
| 38 | +/// |
| 39 | +/// ## Fix safety |
| 40 | +/// This rule's fix is marked as unsafe, as migrating from `@singledispatchmethod` to |
| 41 | +/// `@singledispatch` may change the behavior of the code. |
| 42 | +#[violation] |
| 43 | +pub struct SingledispatchmethodFunction; |
| 44 | + |
| 45 | +impl Violation for SingledispatchmethodFunction { |
| 46 | + const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes; |
| 47 | + |
| 48 | + #[derive_message_formats] |
| 49 | + fn message(&self) -> String { |
| 50 | + format!("`@singledispatchmethod` decorator should not be used on functions") |
| 51 | + } |
| 52 | + |
| 53 | + fn fix_title(&self) -> Option<String> { |
| 54 | + Some("Replace with `@singledispatch`".to_string()) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +/// E1520 |
| 59 | +pub(crate) fn singledispatchmethod_function( |
| 60 | + checker: &Checker, |
| 61 | + scope: &Scope, |
| 62 | + diagnostics: &mut Vec<Diagnostic>, |
| 63 | +) { |
| 64 | + let Some(func) = scope.kind.as_function() else { |
| 65 | + return; |
| 66 | + }; |
| 67 | + |
| 68 | + let ast::StmtFunctionDef { |
| 69 | + name, |
| 70 | + decorator_list, |
| 71 | + .. |
| 72 | + } = func; |
| 73 | + |
| 74 | + let Some(parent) = &checker.semantic().first_non_type_parent_scope(scope) else { |
| 75 | + return; |
| 76 | + }; |
| 77 | + |
| 78 | + let type_ = function_type::classify( |
| 79 | + name, |
| 80 | + decorator_list, |
| 81 | + parent, |
| 82 | + checker.semantic(), |
| 83 | + &checker.settings.pep8_naming.classmethod_decorators, |
| 84 | + &checker.settings.pep8_naming.staticmethod_decorators, |
| 85 | + ); |
| 86 | + if !matches!( |
| 87 | + type_, |
| 88 | + function_type::FunctionType::Function | function_type::FunctionType::StaticMethod |
| 89 | + ) { |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + for decorator in decorator_list { |
| 94 | + if checker |
| 95 | + .semantic() |
| 96 | + .resolve_qualified_name(&decorator.expression) |
| 97 | + .is_some_and(|qualified_name| { |
| 98 | + matches!( |
| 99 | + qualified_name.segments(), |
| 100 | + ["functools", "singledispatchmethod"] |
| 101 | + ) |
| 102 | + }) |
| 103 | + { |
| 104 | + let mut diagnostic = Diagnostic::new(SingledispatchmethodFunction, decorator.range()); |
| 105 | + diagnostic.try_set_fix(|| { |
| 106 | + let (import_edit, binding) = checker.importer().get_or_import_symbol( |
| 107 | + &ImportRequest::import("functools", "singledispatch"), |
| 108 | + decorator.start(), |
| 109 | + checker.semantic(), |
| 110 | + )?; |
| 111 | + Ok(Fix::unsafe_edits( |
| 112 | + Edit::range_replacement(binding, decorator.expression.range()), |
| 113 | + [import_edit], |
| 114 | + )) |
| 115 | + }); |
| 116 | + diagnostics.push(diagnostic); |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments