From 2a64cccb61f1c84e5a58f907f46111ab36321466 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 24 Jul 2024 18:08:23 -0400 Subject: [PATCH] Avoid applying `ignore-names` to `self` and `cls` function names (#12497) ## Summary Closes https://github.com/astral-sh/ruff/issues/12465. --- .../rules/invalid_first_argument_name.rs | 12 ++- ...ing__tests__ignore_names_N804_N804.py.snap | 38 ++++++++ ...ing__tests__ignore_names_N805_N805.py.snap | 86 +++++++++++++++++++ 3 files changed, 132 insertions(+), 4 deletions(-) diff --git a/crates/ruff_linter/src/rules/pep8_naming/rules/invalid_first_argument_name.rs b/crates/ruff_linter/src/rules/pep8_naming/rules/invalid_first_argument_name.rs index f503796e277af..999bb151a172b 100644 --- a/crates/ruff_linter/src/rules/pep8_naming/rules/invalid_first_argument_name.rs +++ b/crates/ruff_linter/src/rules/pep8_naming/rules/invalid_first_argument_name.rs @@ -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; } @@ -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; } diff --git a/crates/ruff_linter/src/rules/pep8_naming/snapshots/ruff_linter__rules__pep8_naming__tests__ignore_names_N804_N804.py.snap b/crates/ruff_linter/src/rules/pep8_naming/snapshots/ruff_linter__rules__pep8_naming__tests__ignore_names_N804_N804.py.snap index 4434e4b603717..678a2358c49ed 100644 --- a/crates/ruff_linter/src/rules/pep8_naming/snapshots/ruff_linter__rules__pep8_naming__tests__ignore_names_N804_N804.py.snap +++ b/crates/ruff_linter/src/rules/pep8_naming/snapshots/ruff_linter__rules__pep8_naming__tests__ignore_names_N804_N804.py.snap @@ -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 @@ -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 diff --git a/crates/ruff_linter/src/rules/pep8_naming/snapshots/ruff_linter__rules__pep8_naming__tests__ignore_names_N805_N805.py.snap b/crates/ruff_linter/src/rules/pep8_naming/snapshots/ruff_linter__rules__pep8_naming__tests__ignore_names_N805_N805.py.snap index 4596583f3fe0a..0d5d100f42e22 100644 --- a/crates/ruff_linter/src/rules/pep8_naming/snapshots/ruff_linter__rules__pep8_naming__tests__ignore_names_N805_N805.py.snap +++ b/crates/ruff_linter/src/rules/pep8_naming/snapshots/ruff_linter__rules__pep8_naming__tests__ignore_names_N805_N805.py.snap @@ -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 @@ -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 @@ -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 @@ -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") @@ -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