Skip to content

Commit 2a64ccc

Browse files
Avoid applying ignore-names to self and cls function names (#12497)
## Summary Closes #12465.
1 parent 928ffd6 commit 2a64ccc

3 files changed

+132
-4
lines changed

crates/ruff_linter/src/rules/pep8_naming/rules/invalid_first_argument_name.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,7 @@ pub(crate) fn invalid_first_argument_name(
208208
function_type::FunctionType::Method => FunctionType::Method,
209209
function_type::FunctionType::ClassMethod => FunctionType::ClassMethod,
210210
};
211-
if !checker.enabled(function_type.rule())
212-
|| checker.settings.pep8_naming.ignore_names.matches(name)
213-
{
211+
if !checker.enabled(function_type.rule()) {
214212
return;
215213
}
216214

@@ -225,7 +223,13 @@ pub(crate) fn invalid_first_argument_name(
225223
return;
226224
};
227225

228-
if &self_or_cls.name == function_type.valid_first_argument_name() {
226+
if &self_or_cls.name == function_type.valid_first_argument_name()
227+
|| checker
228+
.settings
229+
.pep8_naming
230+
.ignore_names
231+
.matches(&self_or_cls.name)
232+
{
229233
return;
230234
}
231235

crates/ruff_linter/src/rules/pep8_naming/snapshots/ruff_linter__rules__pep8_naming__tests__ignore_names_N804_N804.py.snap

+38
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,25 @@ N804.py:5:27: N804 [*] First argument of a class method should be named `cls`
2020
7 7 |
2121
8 8 | @classmethod
2222

23+
N804.py:9:20: N804 [*] First argument of a class method should be named `cls`
24+
|
25+
8 | @classmethod
26+
9 | def badAllowed(self, x, /, other):
27+
| ^^^^ N804
28+
10 | ...
29+
|
30+
= help: Rename `self` to `cls`
31+
32+
Unsafe fix
33+
6 6 | ...
34+
7 7 |
35+
8 8 | @classmethod
36+
9 |- def badAllowed(self, x, /, other):
37+
9 |+ def badAllowed(cls, x, /, other):
38+
10 10 | ...
39+
11 11 |
40+
12 12 | @classmethod
41+
2342
N804.py:13:18: N804 [*] First argument of a class method should be named `cls`
2443
|
2544
12 | @classmethod
@@ -39,6 +58,25 @@ N804.py:13:18: N804 [*] First argument of a class method should be named `cls`
3958
15 15 |
4059
16 16 |
4160

61+
N804.py:18:20: N804 [*] First argument of a class method should be named `cls`
62+
|
63+
17 | class MetaClass(ABCMeta):
64+
18 | def badAllowed(self):
65+
| ^^^^ N804
66+
19 | pass
67+
|
68+
= help: Rename `self` to `cls`
69+
70+
Unsafe fix
71+
15 15 |
72+
16 16 |
73+
17 17 | class MetaClass(ABCMeta):
74+
18 |- def badAllowed(self):
75+
18 |+ def badAllowed(cls):
76+
19 19 | pass
77+
20 20 |
78+
21 21 | def stillBad(self):
79+
4280
N804.py:21:18: N804 [*] First argument of a class method should be named `cls`
4381
|
4482
19 | pass

crates/ruff_linter/src/rules/pep8_naming/snapshots/ruff_linter__rules__pep8_naming__tests__ignore_names_N805_N805.py.snap

+86
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
---
22
source: crates/ruff_linter/src/rules/pep8_naming/mod.rs
33
---
4+
N805.py:7:20: N805 [*] First argument of a method should be named `self`
5+
|
6+
6 | class Class:
7+
7 | def badAllowed(this):
8+
| ^^^^ N805
9+
8 | pass
10+
|
11+
= help: Rename `this` to `self`
12+
13+
Unsafe fix
14+
4 4 |
15+
5 5 |
16+
6 6 | class Class:
17+
7 |- def badAllowed(this):
18+
7 |+ def badAllowed(self):
19+
8 8 | pass
20+
9 9 |
21+
10 10 | def stillBad(this):
22+
423
N805.py:10:18: N805 [*] First argument of a method should be named `self`
524
|
625
8 | pass
@@ -21,6 +40,26 @@ N805.py:10:18: N805 [*] First argument of a method should be named `self`
2140
12 12 |
2241
13 13 | if False:
2342

43+
N805.py:15:24: N805 [*] First argument of a method should be named `self`
44+
|
45+
13 | if False:
46+
14 |
47+
15 | def badAllowed(this):
48+
| ^^^^ N805
49+
16 | pass
50+
|
51+
= help: Rename `this` to `self`
52+
53+
Unsafe fix
54+
12 12 |
55+
13 13 | if False:
56+
14 14 |
57+
15 |- def badAllowed(this):
58+
15 |+ def badAllowed(self):
59+
16 16 | pass
60+
17 17 |
61+
18 18 | def stillBad(this):
62+
2463
N805.py:18:22: N805 [*] First argument of a method should be named `self`
2564
|
2665
16 | pass
@@ -41,6 +80,25 @@ N805.py:18:22: N805 [*] First argument of a method should be named `self`
4180
20 20 |
4281
21 21 | @pydantic.validator
4382

83+
N805.py:22:20: N805 [*] First argument of a method should be named `self`
84+
|
85+
21 | @pydantic.validator
86+
22 | def badAllowed(cls, my_field: str) -> str:
87+
| ^^^ N805
88+
23 | pass
89+
|
90+
= help: Rename `cls` to `self`
91+
92+
Unsafe fix
93+
19 19 | pass
94+
20 20 |
95+
21 21 | @pydantic.validator
96+
22 |- def badAllowed(cls, my_field: str) -> str:
97+
22 |+ def badAllowed(self, my_field: str) -> str:
98+
23 23 | pass
99+
24 24 |
100+
25 25 | @pydantic.validator
101+
44102
N805.py:26:18: N805 [*] First argument of a method should be named `self`
45103
|
46104
25 | @pydantic.validator
@@ -60,6 +118,25 @@ N805.py:26:18: N805 [*] First argument of a method should be named `self`
60118
28 28 |
61119
29 29 | @pydantic.validator("my_field")
62120

121+
N805.py:30:20: N805 [*] First argument of a method should be named `self`
122+
|
123+
29 | @pydantic.validator("my_field")
124+
30 | def badAllowed(cls, my_field: str) -> str:
125+
| ^^^ N805
126+
31 | pass
127+
|
128+
= help: Rename `cls` to `self`
129+
130+
Unsafe fix
131+
27 27 | pass
132+
28 28 |
133+
29 29 | @pydantic.validator("my_field")
134+
30 |- def badAllowed(cls, my_field: str) -> str:
135+
30 |+ def badAllowed(self, my_field: str) -> str:
136+
31 31 | pass
137+
32 32 |
138+
33 33 | @pydantic.validator("my_field")
139+
63140
N805.py:34:18: N805 [*] First argument of a method should be named `self`
64141
|
65142
33 | @pydantic.validator("my_field")
@@ -79,6 +156,15 @@ N805.py:34:18: N805 [*] First argument of a method should be named `self`
79156
36 36 |
80157
37 37 | @classmethod
81158

159+
N805.py:55:20: N805 First argument of a method should be named `self`
160+
|
161+
54 | class PosOnlyClass:
162+
55 | def badAllowed(this, blah, /, self, something: str):
163+
| ^^^^ N805
164+
56 | pass
165+
|
166+
= help: Rename `this` to `self`
167+
82168
N805.py:58:18: N805 First argument of a method should be named `self`
83169
|
84170
56 | pass

0 commit comments

Comments
 (0)