Skip to content

Commit b4419c3

Browse files
authored
Ignore @override method when enforcing bad-dunder-name rule (#7224)
## Summary Closes #6958. If a method has the `override` decorator, there is nothing you can do about incorrect dunder methods, so they should be ignored. ## Test Plan Overridden incorrect dunder method was added to the tests to verify ruff doesn't catch it when evaluating the file. Snapshot changes are all just line number changes
1 parent 08f1922 commit b4419c3

File tree

3 files changed

+50
-41
lines changed

3 files changed

+50
-41
lines changed

crates/ruff/resources/test/fixtures/pylint/bad_dunder_method_name.py

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from typing import override
2+
3+
14
class Apples:
25
def _init_(self): # [bad-dunder-name]
36
pass
@@ -21,6 +24,11 @@ def __inv__(self): # [bad-dunder-name]
2124
# author likely meant to call the invert dunder method
2225
pass
2326

27+
@override
28+
def _ignore__(self): # [bad-dunder-name]
29+
# overridden dunder methods should be ignored
30+
pass
31+
2432
def hello(self):
2533
print("hello")
2634

crates/ruff/src/rules/pylint/rules/bad_dunder_method_name.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@ use ruff_diagnostics::{Diagnostic, Violation};
22
use ruff_macros::{derive_message_formats, violation};
33
use ruff_python_ast::identifier::Identifier;
44
use ruff_python_ast::Stmt;
5+
use ruff_python_semantic::analyze::visibility;
56

67
use crate::checkers::ast::Checker;
78

89
/// ## What it does
9-
/// Checks for any misspelled dunder name method and for any method
10-
/// defined with `_..._` that's not one of the pre-defined methods
11-
///
12-
/// The pre-defined methods encompass all of Python's standard dunder
13-
/// methods.
14-
///
15-
/// Note this includes all methods starting and ending with at least
16-
/// one underscore to detect mistakes.
10+
/// Checks for misspelled and unknown dunder names in method definitions.
1711
///
1812
/// ## Why is this bad?
1913
/// Misspelled dunder name methods may cause your code to not function
@@ -24,6 +18,10 @@ use crate::checkers::ast::Checker;
2418
/// that diverges from standard Python dunder methods could potentially
2519
/// confuse someone reading the code.
2620
///
21+
/// This rule will detect all methods starting and ending with at least
22+
/// one underscore (e.g., `_str_`), but ignores known dunder methods (like
23+
/// `__init__`), as well as methods that are marked with `@override`.
24+
///
2725
/// ## Example
2826
/// ```python
2927
/// class Foo:
@@ -62,6 +60,9 @@ pub(crate) fn bad_dunder_method_name(checker: &mut Checker, class_body: &[Stmt])
6260
method.name.starts_with('_') && method.name.ends_with('_')
6361
})
6462
{
63+
if visibility::is_override(&method.decorator_list, checker.semantic()) {
64+
continue;
65+
}
6566
checker.diagnostics.push(Diagnostic::new(
6667
BadDunderMethodName {
6768
name: method.name.to_string(),
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,61 @@
11
---
22
source: crates/ruff/src/rules/pylint/mod.rs
33
---
4-
bad_dunder_method_name.py:2:9: PLW3201 Bad or misspelled dunder method name `_init_`. (bad-dunder-name)
4+
bad_dunder_method_name.py:5:9: PLW3201 Bad or misspelled dunder method name `_init_`. (bad-dunder-name)
55
|
6-
1 | class Apples:
7-
2 | def _init_(self): # [bad-dunder-name]
6+
4 | class Apples:
7+
5 | def _init_(self): # [bad-dunder-name]
88
| ^^^^^^ PLW3201
9-
3 | pass
9+
6 | pass
1010
|
1111

12-
bad_dunder_method_name.py:5:9: PLW3201 Bad or misspelled dunder method name `__hello__`. (bad-dunder-name)
12+
bad_dunder_method_name.py:8:9: PLW3201 Bad or misspelled dunder method name `__hello__`. (bad-dunder-name)
1313
|
14-
3 | pass
15-
4 |
16-
5 | def __hello__(self): # [bad-dunder-name]
14+
6 | pass
15+
7 |
16+
8 | def __hello__(self): # [bad-dunder-name]
1717
| ^^^^^^^^^ PLW3201
18-
6 | print("hello")
18+
9 | print("hello")
1919
|
2020

21-
bad_dunder_method_name.py:8:9: PLW3201 Bad or misspelled dunder method name `__init_`. (bad-dunder-name)
21+
bad_dunder_method_name.py:11:9: PLW3201 Bad or misspelled dunder method name `__init_`. (bad-dunder-name)
2222
|
23-
6 | print("hello")
24-
7 |
25-
8 | def __init_(self): # [bad-dunder-name]
23+
9 | print("hello")
24+
10 |
25+
11 | def __init_(self): # [bad-dunder-name]
2626
| ^^^^^^^ PLW3201
27-
9 | # author likely unintentionally misspelled the correct init dunder.
28-
10 | pass
27+
12 | # author likely unintentionally misspelled the correct init dunder.
28+
13 | pass
2929
|
3030

31-
bad_dunder_method_name.py:12:9: PLW3201 Bad or misspelled dunder method name `_init_`. (bad-dunder-name)
31+
bad_dunder_method_name.py:15:9: PLW3201 Bad or misspelled dunder method name `_init_`. (bad-dunder-name)
3232
|
33-
10 | pass
34-
11 |
35-
12 | def _init_(self): # [bad-dunder-name]
33+
13 | pass
34+
14 |
35+
15 | def _init_(self): # [bad-dunder-name]
3636
| ^^^^^^ PLW3201
37-
13 | # author likely unintentionally misspelled the correct init dunder.
38-
14 | pass
37+
16 | # author likely unintentionally misspelled the correct init dunder.
38+
17 | pass
3939
|
4040

41-
bad_dunder_method_name.py:16:9: PLW3201 Bad or misspelled dunder method name `___neg__`. (bad-dunder-name)
41+
bad_dunder_method_name.py:19:9: PLW3201 Bad or misspelled dunder method name `___neg__`. (bad-dunder-name)
4242
|
43-
14 | pass
44-
15 |
45-
16 | def ___neg__(self): # [bad-dunder-name]
43+
17 | pass
44+
18 |
45+
19 | def ___neg__(self): # [bad-dunder-name]
4646
| ^^^^^^^^ PLW3201
47-
17 | # author likely accidentally added an additional `_`
48-
18 | pass
47+
20 | # author likely accidentally added an additional `_`
48+
21 | pass
4949
|
5050

51-
bad_dunder_method_name.py:20:9: PLW3201 Bad or misspelled dunder method name `__inv__`. (bad-dunder-name)
51+
bad_dunder_method_name.py:23:9: PLW3201 Bad or misspelled dunder method name `__inv__`. (bad-dunder-name)
5252
|
53-
18 | pass
54-
19 |
55-
20 | def __inv__(self): # [bad-dunder-name]
53+
21 | pass
54+
22 |
55+
23 | def __inv__(self): # [bad-dunder-name]
5656
| ^^^^^^^ PLW3201
57-
21 | # author likely meant to call the invert dunder method
58-
22 | pass
57+
24 | # author likely meant to call the invert dunder method
58+
25 | pass
5959
|
6060

6161

0 commit comments

Comments
 (0)