Skip to content

Commit

Permalink
Avoid applying ignore-names to self and cls function names (#12497
Browse files Browse the repository at this point in the history
)

## Summary

Closes #12465.
  • Loading branch information
charliermarsh authored Jul 24, 2024
1 parent 928ffd6 commit 2a64ccc
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,7 @@ pub(crate) fn invalid_first_argument_name(
function_type::FunctionType::Method => FunctionType::Method,
function_type::FunctionType::ClassMethod => FunctionType::ClassMethod,
};
if !checker.enabled(function_type.rule())
|| checker.settings.pep8_naming.ignore_names.matches(name)
{
if !checker.enabled(function_type.rule()) {
return;
}

Expand All @@ -225,7 +223,13 @@ pub(crate) fn invalid_first_argument_name(
return;
};

if &self_or_cls.name == function_type.valid_first_argument_name() {
if &self_or_cls.name == function_type.valid_first_argument_name()
|| checker
.settings
.pep8_naming
.ignore_names
.matches(&self_or_cls.name)
{
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ N804.py:5:27: N804 [*] First argument of a class method should be named `cls`
7 7 |
8 8 | @classmethod

N804.py:9:20: N804 [*] First argument of a class method should be named `cls`
|
8 | @classmethod
9 | def badAllowed(self, x, /, other):
| ^^^^ N804
10 | ...
|
= help: Rename `self` to `cls`

Unsafe fix
6 6 | ...
7 7 |
8 8 | @classmethod
9 |- def badAllowed(self, x, /, other):
9 |+ def badAllowed(cls, x, /, other):
10 10 | ...
11 11 |
12 12 | @classmethod

N804.py:13:18: N804 [*] First argument of a class method should be named `cls`
|
12 | @classmethod
Expand All @@ -39,6 +58,25 @@ N804.py:13:18: N804 [*] First argument of a class method should be named `cls`
15 15 |
16 16 |

N804.py:18:20: N804 [*] First argument of a class method should be named `cls`
|
17 | class MetaClass(ABCMeta):
18 | def badAllowed(self):
| ^^^^ N804
19 | pass
|
= help: Rename `self` to `cls`

Unsafe fix
15 15 |
16 16 |
17 17 | class MetaClass(ABCMeta):
18 |- def badAllowed(self):
18 |+ def badAllowed(cls):
19 19 | pass
20 20 |
21 21 | def stillBad(self):

N804.py:21:18: N804 [*] First argument of a class method should be named `cls`
|
19 | pass
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
---
source: crates/ruff_linter/src/rules/pep8_naming/mod.rs
---
N805.py:7:20: N805 [*] First argument of a method should be named `self`
|
6 | class Class:
7 | def badAllowed(this):
| ^^^^ N805
8 | pass
|
= help: Rename `this` to `self`

Unsafe fix
4 4 |
5 5 |
6 6 | class Class:
7 |- def badAllowed(this):
7 |+ def badAllowed(self):
8 8 | pass
9 9 |
10 10 | def stillBad(this):

N805.py:10:18: N805 [*] First argument of a method should be named `self`
|
8 | pass
Expand All @@ -21,6 +40,26 @@ N805.py:10:18: N805 [*] First argument of a method should be named `self`
12 12 |
13 13 | if False:

N805.py:15:24: N805 [*] First argument of a method should be named `self`
|
13 | if False:
14 |
15 | def badAllowed(this):
| ^^^^ N805
16 | pass
|
= help: Rename `this` to `self`

Unsafe fix
12 12 |
13 13 | if False:
14 14 |
15 |- def badAllowed(this):
15 |+ def badAllowed(self):
16 16 | pass
17 17 |
18 18 | def stillBad(this):

N805.py:18:22: N805 [*] First argument of a method should be named `self`
|
16 | pass
Expand All @@ -41,6 +80,25 @@ N805.py:18:22: N805 [*] First argument of a method should be named `self`
20 20 |
21 21 | @pydantic.validator

N805.py:22:20: N805 [*] First argument of a method should be named `self`
|
21 | @pydantic.validator
22 | def badAllowed(cls, my_field: str) -> str:
| ^^^ N805
23 | pass
|
= help: Rename `cls` to `self`

Unsafe fix
19 19 | pass
20 20 |
21 21 | @pydantic.validator
22 |- def badAllowed(cls, my_field: str) -> str:
22 |+ def badAllowed(self, my_field: str) -> str:
23 23 | pass
24 24 |
25 25 | @pydantic.validator

N805.py:26:18: N805 [*] First argument of a method should be named `self`
|
25 | @pydantic.validator
Expand All @@ -60,6 +118,25 @@ N805.py:26:18: N805 [*] First argument of a method should be named `self`
28 28 |
29 29 | @pydantic.validator("my_field")

N805.py:30:20: N805 [*] First argument of a method should be named `self`
|
29 | @pydantic.validator("my_field")
30 | def badAllowed(cls, my_field: str) -> str:
| ^^^ N805
31 | pass
|
= help: Rename `cls` to `self`

Unsafe fix
27 27 | pass
28 28 |
29 29 | @pydantic.validator("my_field")
30 |- def badAllowed(cls, my_field: str) -> str:
30 |+ def badAllowed(self, my_field: str) -> str:
31 31 | pass
32 32 |
33 33 | @pydantic.validator("my_field")

N805.py:34:18: N805 [*] First argument of a method should be named `self`
|
33 | @pydantic.validator("my_field")
Expand All @@ -79,6 +156,15 @@ N805.py:34:18: N805 [*] First argument of a method should be named `self`
36 36 |
37 37 | @classmethod

N805.py:55:20: N805 First argument of a method should be named `self`
|
54 | class PosOnlyClass:
55 | def badAllowed(this, blah, /, self, something: str):
| ^^^^ N805
56 | pass
|
= help: Rename `this` to `self`

N805.py:58:18: N805 First argument of a method should be named `self`
|
56 | pass
Expand Down

0 comments on commit 2a64ccc

Please sign in to comment.