[`pep8_naming`] Add fixes `N804` and `N805` (#10215)

## Summary

This PR fixes for `invalid-first-argument` rules.
The fixes rename the first argument of methods and class methods to the
valid one. References to this argument are also renamed.
Fixes are skipped when another argument is named as the valid first
argument.
The fix is marked as unsafe due

The functions for the `N804` and `N805` rules are now merged, as they
only differ by the name of the valid first argument.
The rules were moved from the AST iteration to the deferred scopes to be
in the function scope while creating the fix.

## Test Plan

`cargo test`
This commit is contained in:
Gautier Moin 2024-03-04 03:22:54 +01:00 committed by GitHub
parent c27e048ff2
commit 4eac9baf43
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 1098 additions and 270 deletions

View File

@ -50,6 +50,29 @@ class MetaClass(ABCMeta):
def static_method(not_cls) -> bool:
return False
class ClsInArgsClass(ABCMeta):
def cls_as_argument(this, cls):
pass
def cls_as_pos_only_argument(this, cls, /):
pass
def cls_as_kw_only_argument(this, *, cls):
pass
def cls_as_varags(this, *cls):
pass
def cls_as_kwargs(this, **cls):
pass
class RenamingInMethodBodyClass(ABCMeta):
def bad_method(this):
this = this
this
def bad_method(this):
self = this
def func(x):
return x

View File

@ -61,7 +61,7 @@ class PosOnlyClass:
def good_method_pos_only(self, blah, /, something: str):
pass
def bad_method_pos_only(this, blah, /, self, something: str):
def bad_method_pos_only(this, blah, /, something: str):
pass
@ -93,3 +93,27 @@ class ModelClass:
@foobar.thisisstatic
def badstatic(foo):
pass
class SelfInArgsClass:
def self_as_argument(this, self):
pass
def self_as_pos_only_argument(this, self, /):
pass
def self_as_kw_only_argument(this, *, self):
pass
def self_as_varags(this, *self):
pass
def self_as_kwargs(this, **self):
pass
class RenamingInMethodBodyClass:
def bad_method(this):
this = this
this
def bad_method(this):
self = this

View File

@ -7,8 +7,8 @@ use crate::checkers::ast::Checker;
use crate::codes::Rule;
use crate::fix;
use crate::rules::{
flake8_builtins, flake8_pyi, flake8_type_checking, flake8_unused_arguments, pyflakes, pylint,
ruff,
flake8_builtins, flake8_pyi, flake8_type_checking, flake8_unused_arguments, pep8_naming,
pyflakes, pylint, ruff,
};
/// Run lint rules over all deferred scopes in the [`SemanticModel`].
@ -18,6 +18,8 @@ pub(crate) fn deferred_scopes(checker: &mut Checker) {
Rule::GlobalVariableNotAssigned,
Rule::ImportPrivateName,
Rule::ImportShadowedByLoopVar,
Rule::InvalidFirstArgumentNameForMethod,
Rule::InvalidFirstArgumentNameForClassMethod,
Rule::NoSelfUse,
Rule::RedefinedArgumentFromLocal,
Rule::RedefinedWhileUnused,
@ -394,6 +396,13 @@ pub(crate) fn deferred_scopes(checker: &mut Checker) {
if checker.enabled(Rule::SingledispatchMethod) {
pylint::rules::singledispatch_method(checker, scope, &mut diagnostics);
}
if checker.any_enabled(&[
Rule::InvalidFirstArgumentNameForClassMethod,
Rule::InvalidFirstArgumentNameForMethod,
]) {
pep8_naming::rules::invalid_first_argument_name(checker, scope, &mut diagnostics);
}
}
}
checker.diagnostics.extend(diagnostics);

View File

@ -105,30 +105,6 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
checker.diagnostics.push(diagnostic);
}
}
if checker.enabled(Rule::InvalidFirstArgumentNameForClassMethod) {
if let Some(diagnostic) =
pep8_naming::rules::invalid_first_argument_name_for_class_method(
checker,
checker.semantic.current_scope(),
name,
decorator_list,
parameters,
)
{
checker.diagnostics.push(diagnostic);
}
}
if checker.enabled(Rule::InvalidFirstArgumentNameForMethod) {
if let Some(diagnostic) = pep8_naming::rules::invalid_first_argument_name_for_method(
checker,
checker.semantic.current_scope(),
name,
decorator_list,
parameters,
) {
checker.diagnostics.push(diagnostic);
}
}
if checker.source_type.is_stub() {
if checker.enabled(Rule::PassStatementStubBody) {
flake8_pyi::rules::pass_statement_stub_body(checker, body);

View File

@ -0,0 +1,277 @@
use anyhow::Result;
use ruff_diagnostics::{Diagnostic, DiagnosticKind, Fix, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_ast as ast;
use ruff_python_ast::ParameterWithDefault;
use ruff_python_semantic::analyze::function_type;
use ruff_python_semantic::{Scope, ScopeKind, SemanticModel};
use ruff_text_size::Ranged;
use crate::checkers::ast::Checker;
use crate::registry::Rule;
use crate::renamer::Renamer;
/// ## What it does
/// Checks for instance methods that use a name other than `self` for their
/// first argument.
///
/// ## Why is this bad?
/// [PEP 8] recommends the use of `self` as first argument for all instance
/// methods:
///
/// > Always use self for the first argument to instance methods.
/// >
/// > If a function arguments name clashes with a reserved keyword, it is generally better to
/// > append a single trailing underscore rather than use an abbreviation or spelling corruption.
/// > Thus `class_` is better than `clss`. (Perhaps better is to avoid such clashes by using a synonym.)
///
/// Names can be excluded from this rule using the [`lint.pep8-naming.ignore-names`]
/// or [`lint.pep8-naming.extend-ignore-names`] configuration options. For example,
/// to allow the use of `this` as the first argument to instance methods, set
/// the [`lint.pep8-naming.extend-ignore-names`] option to `["this"]`.
///
/// ## Example
/// ```python
/// class Example:
/// def function(cls, data):
/// ...
/// ```
///
/// Use instead:
/// ```python
/// class Example:
/// def function(self, data):
/// ...
/// ```
///
/// ## Fix safety
/// This rule's fix is marked as unsafe, as renaming a method parameter
/// can change the behavior of the program.
///
/// ## Options
/// - `lint.pep8-naming.classmethod-decorators`
/// - `lint.pep8-naming.staticmethod-decorators`
/// - `lint.pep8-naming.ignore-names`
/// - `lint.pep8-naming.extend-ignore-names`
///
/// [PEP 8]: https://peps.python.org/pep-0008/#function-and-method-arguments
#[violation]
pub struct InvalidFirstArgumentNameForMethod {
argument_name: String,
}
impl Violation for InvalidFirstArgumentNameForMethod {
const FIX_AVAILABILITY: ruff_diagnostics::FixAvailability =
ruff_diagnostics::FixAvailability::Sometimes;
#[derive_message_formats]
fn message(&self) -> String {
format!("First argument of a method should be named `self`")
}
fn fix_title(&self) -> Option<String> {
let Self { argument_name } = self;
Some(format!("Rename `{argument_name}` to `self`"))
}
}
/// ## What it does
/// Checks for class methods that use a name other than `cls` for their
/// first argument.
///
/// ## Why is this bad?
/// [PEP 8] recommends the use of `cls` as the first argument for all class
/// methods:
///
/// > Always use `cls` for the first argument to class methods.
/// >
/// > If a function arguments name clashes with a reserved keyword, it is generally better to
/// > append a single trailing underscore rather than use an abbreviation or spelling corruption.
/// > Thus `class_` is better than `clss`. (Perhaps better is to avoid such clashes by using a synonym.)
///
/// Names can be excluded from this rule using the [`lint.pep8-naming.ignore-names`]
/// or [`lint.pep8-naming.extend-ignore-names`] configuration options. For example,
/// to allow the use of `klass` as the first argument to class methods, set
/// the [`lint.pep8-naming.extend-ignore-names`] option to `["klass"]`.
///
/// ## Example
/// ```python
/// class Example:
/// @classmethod
/// def function(self, data):
/// ...
/// ```
///
/// Use instead:
/// ```python
/// class Example:
/// @classmethod
/// def function(cls, data):
/// ...
/// ```
///
/// ## Fix safety
/// This rule's fix is marked as unsafe, as renaming a method parameter
/// can change the behavior of the program.
///
/// ## Options
/// - `lint.pep8-naming.classmethod-decorators`
/// - `lint.pep8-naming.staticmethod-decorators`
/// - `lint.pep8-naming.ignore-names`
/// - `lint.pep8-naming.extend-ignore-names`
///
/// [PEP 8]: https://peps.python.org/pep-0008/#function-and-method-arguments
#[violation]
pub struct InvalidFirstArgumentNameForClassMethod {
argument_name: String,
}
impl Violation for InvalidFirstArgumentNameForClassMethod {
const FIX_AVAILABILITY: ruff_diagnostics::FixAvailability =
ruff_diagnostics::FixAvailability::Sometimes;
#[derive_message_formats]
fn message(&self) -> String {
format!("First argument of a class method should be named `cls`")
}
fn fix_title(&self) -> Option<String> {
let Self { argument_name } = self;
Some(format!("Rename `{argument_name}` to `cls`"))
}
}
#[derive(Debug, Copy, Clone)]
enum FunctionType {
/// The function is an instance method.
Method,
/// The function is a class method.
ClassMethod,
}
impl FunctionType {
fn diagnostic_kind(self, argument_name: String) -> DiagnosticKind {
match self {
Self::Method => InvalidFirstArgumentNameForMethod { argument_name }.into(),
Self::ClassMethod => InvalidFirstArgumentNameForClassMethod { argument_name }.into(),
}
}
const fn valid_first_argument_name(self) -> &'static str {
match self {
Self::Method => "self",
Self::ClassMethod => "cls",
}
}
const fn rule(self) -> Rule {
match self {
Self::Method => Rule::InvalidFirstArgumentNameForMethod,
Self::ClassMethod => Rule::InvalidFirstArgumentNameForClassMethod,
}
}
}
/// N804, N805
pub(crate) fn invalid_first_argument_name(
checker: &Checker,
scope: &Scope,
diagnostics: &mut Vec<Diagnostic>,
) {
let ScopeKind::Function(ast::StmtFunctionDef {
name,
parameters,
decorator_list,
..
}) = &scope.kind
else {
panic!("Expected ScopeKind::Function")
};
let Some(parent) = &checker.semantic().first_non_type_parent_scope(scope) else {
return;
};
let function_type = match function_type::classify(
name,
decorator_list,
parent,
checker.semantic(),
&checker.settings.pep8_naming.classmethod_decorators,
&checker.settings.pep8_naming.staticmethod_decorators,
) {
function_type::FunctionType::Function | function_type::FunctionType::StaticMethod => {
return;
}
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)
{
return;
}
let Some(ParameterWithDefault {
parameter: self_or_cls,
..
}) = parameters
.posonlyargs
.first()
.or_else(|| parameters.args.first())
else {
return;
};
if &self_or_cls.name == function_type.valid_first_argument_name() {
return;
}
let mut diagnostic = Diagnostic::new(
function_type.diagnostic_kind(self_or_cls.name.to_string()),
self_or_cls.range(),
);
diagnostic.try_set_optional_fix(|| {
rename_parameter(
scope,
self_or_cls,
parameters,
checker.semantic(),
function_type,
)
});
diagnostics.push(diagnostic);
}
/// Rename the first parameter to `self` or `cls`, if no other parameter has the target name.
fn rename_parameter(
scope: &Scope<'_>,
self_or_cls: &ast::Parameter,
parameters: &ast::Parameters,
semantic: &SemanticModel<'_>,
function_type: FunctionType,
) -> Result<Option<Fix>> {
// Don't fix if another parameter has the valid name.
if parameters
.posonlyargs
.iter()
.chain(&parameters.args)
.chain(&parameters.kwonlyargs)
.skip(1)
.map(|parameter_with_default| &parameter_with_default.parameter)
.chain(parameters.vararg.as_deref())
.chain(parameters.kwarg.as_deref())
.any(|parameter| &parameter.name == function_type.valid_first_argument_name())
{
return Ok(None);
}
let (edit, rest) = Renamer::rename(
&self_or_cls.name,
function_type.valid_first_argument_name(),
scope,
semantic,
)?;
Ok(Some(Fix::unsafe_edits(edit, rest)))
}

View File

@ -1,104 +0,0 @@
use ruff_python_ast::{Decorator, ParameterWithDefault, Parameters};
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_semantic::analyze::function_type;
use ruff_python_semantic::Scope;
use ruff_text_size::Ranged;
use crate::checkers::ast::Checker;
/// ## What it does
/// Checks for class methods that use a name other than `cls` for their
/// first argument.
///
/// ## Why is this bad?
/// [PEP 8] recommends the use of `cls` as the first argument for all class
/// methods:
///
/// > Always use `cls` for the first argument to class methods.
/// >
/// > If a function arguments name clashes with a reserved keyword, it is generally better to
/// > append a single trailing underscore rather than use an abbreviation or spelling corruption.
/// > Thus `class_` is better than `clss`. (Perhaps better is to avoid such clashes by using a synonym.)
///
/// Names can be excluded from this rule using the [`lint.pep8-naming.ignore-names`]
/// or [`lint.pep8-naming.extend-ignore-names`] configuration options. For example,
/// to allow the use of `klass` as the first argument to class methods, set
/// the [`lint.pep8-naming.extend-ignore-names`] option to `["klass"]`.
///
/// ## Example
/// ```python
/// class Example:
/// @classmethod
/// def function(self, data):
/// ...
/// ```
///
/// Use instead:
/// ```python
/// class Example:
/// @classmethod
/// def function(cls, data):
/// ...
/// ```
///
/// ## Options
/// - `lint.pep8-naming.classmethod-decorators`
/// - `lint.pep8-naming.staticmethod-decorators`
/// - `lint.pep8-naming.ignore-names`
/// - `lint.pep8-naming.extend-ignore-names`
///
/// [PEP 8]: https://peps.python.org/pep-0008/#function-and-method-arguments
#[violation]
pub struct InvalidFirstArgumentNameForClassMethod;
impl Violation for InvalidFirstArgumentNameForClassMethod {
#[derive_message_formats]
fn message(&self) -> String {
format!("First argument of a class method should be named `cls`")
}
}
/// N804
pub(crate) fn invalid_first_argument_name_for_class_method(
checker: &Checker,
scope: &Scope,
name: &str,
decorator_list: &[Decorator],
parameters: &Parameters,
) -> Option<Diagnostic> {
if !matches!(
function_type::classify(
name,
decorator_list,
scope,
checker.semantic(),
&checker.settings.pep8_naming.classmethod_decorators,
&checker.settings.pep8_naming.staticmethod_decorators,
),
function_type::FunctionType::ClassMethod
) {
return None;
}
if let Some(ParameterWithDefault {
parameter,
default: _,
range: _,
}) = parameters
.posonlyargs
.first()
.or_else(|| parameters.args.first())
{
if &parameter.name != "cls" {
if checker.settings.pep8_naming.ignore_names.matches(name) {
return None;
}
return Some(Diagnostic::new(
InvalidFirstArgumentNameForClassMethod,
parameter.range(),
));
}
}
None
}

View File

@ -1,96 +0,0 @@
use ruff_python_ast::{Decorator, Parameters};
use ruff_diagnostics::{Diagnostic, Violation};
use ruff_macros::{derive_message_formats, violation};
use ruff_python_semantic::analyze::function_type;
use ruff_python_semantic::Scope;
use ruff_text_size::Ranged;
use crate::checkers::ast::Checker;
/// ## What it does
/// Checks for instance methods that use a name other than `self` for their
/// first argument.
///
/// ## Why is this bad?
/// [PEP 8] recommends the use of `self` as first argument for all instance
/// methods:
///
/// > Always use self for the first argument to instance methods.
/// >
/// > If a function arguments name clashes with a reserved keyword, it is generally better to
/// > append a single trailing underscore rather than use an abbreviation or spelling corruption.
/// > Thus `class_` is better than `clss`. (Perhaps better is to avoid such clashes by using a synonym.)
///
/// Names can be excluded from this rule using the [`lint.pep8-naming.ignore-names`]
/// or [`lint.pep8-naming.extend-ignore-names`] configuration options. For example,
/// to allow the use of `this` as the first argument to instance methods, set
/// the [`lint.pep8-naming.extend-ignore-names`] option to `["this"]`.
///
/// ## Example
/// ```python
/// class Example:
/// def function(cls, data):
/// ...
/// ```
///
/// Use instead:
/// ```python
/// class Example:
/// def function(self, data):
/// ...
/// ```
///
/// ## Options
/// - `lint.pep8-naming.classmethod-decorators`
/// - `lint.pep8-naming.staticmethod-decorators`
/// - `lint.pep8-naming.ignore-names`
/// - `lint.pep8-naming.extend-ignore-names`
///
/// [PEP 8]: https://peps.python.org/pep-0008/#function-and-method-arguments
#[violation]
pub struct InvalidFirstArgumentNameForMethod;
impl Violation for InvalidFirstArgumentNameForMethod {
#[derive_message_formats]
fn message(&self) -> String {
format!("First argument of a method should be named `self`")
}
}
/// N805
pub(crate) fn invalid_first_argument_name_for_method(
checker: &Checker,
scope: &Scope,
name: &str,
decorator_list: &[Decorator],
parameters: &Parameters,
) -> Option<Diagnostic> {
if !matches!(
function_type::classify(
name,
decorator_list,
scope,
checker.semantic(),
&checker.settings.pep8_naming.classmethod_decorators,
&checker.settings.pep8_naming.staticmethod_decorators,
),
function_type::FunctionType::Method
) {
return None;
}
let arg = parameters
.posonlyargs
.first()
.or_else(|| parameters.args.first())?;
if &arg.parameter.name == "self" {
return None;
}
if checker.settings.pep8_naming.ignore_names.matches(name) {
return None;
}
Some(Diagnostic::new(
InvalidFirstArgumentNameForMethod,
arg.parameter.range(),
))
}

View File

@ -6,8 +6,7 @@ pub(crate) use dunder_function_name::*;
pub(crate) use error_suffix_on_exception_name::*;
pub(crate) use invalid_argument_name::*;
pub(crate) use invalid_class_name::*;
pub(crate) use invalid_first_argument_name_for_class_method::*;
pub(crate) use invalid_first_argument_name_for_method::*;
pub(crate) use invalid_first_argument_name::*;
pub(crate) use invalid_function_name::*;
pub(crate) use invalid_module_name::*;
pub(crate) use lowercase_imported_as_non_lowercase::*;
@ -23,8 +22,7 @@ mod dunder_function_name;
mod error_suffix_on_exception_name;
mod invalid_argument_name;
mod invalid_class_name;
mod invalid_first_argument_name_for_class_method;
mod invalid_first_argument_name_for_method;
mod invalid_first_argument_name;
mod invalid_function_name;
mod invalid_module_name;
mod lowercase_imported_as_non_lowercase;

View File

@ -1,7 +1,7 @@
---
source: crates/ruff_linter/src/rules/pep8_naming/mod.rs
---
N804.py:30:27: N804 First argument of a class method should be named `cls`
N804.py:30:27: N804 [*] First argument of a class method should be named `cls`
|
28 | ...
29 |
@ -9,21 +9,147 @@ N804.py:30:27: N804 First argument of a class method should be named `cls`
| ^^^^ N804
31 | ...
|
= help: Rename `self` to `cls`
N804.py:38:56: N804 First argument of a class method should be named `cls`
Unsafe fix
27 27 | def __new__(cls, *args, **kwargs):
28 28 | ...
29 29 |
30 |- def __init_subclass__(self, default_name, **kwargs):
30 |+ def __init_subclass__(cls, default_name, **kwargs):
31 31 | ...
32 32 |
33 33 | @classmethod
N804.py:38:56: N804 [*] First argument of a class method should be named `cls`
|
37 | @classmethod
38 | def bad_class_method_with_positional_only_argument(self, x, /, other):
| ^^^^ N804
39 | ...
|
= help: Rename `self` to `cls`
N804.py:43:20: N804 First argument of a class method should be named `cls`
Unsafe fix
35 35 | ...
36 36 |
37 37 | @classmethod
38 |- def bad_class_method_with_positional_only_argument(self, x, /, other):
38 |+ def bad_class_method_with_positional_only_argument(cls, x, /, other):
39 39 | ...
40 40 |
41 41 |
N804.py:43:20: N804 [*] First argument of a class method should be named `cls`
|
42 | class MetaClass(ABCMeta):
43 | def bad_method(self):
| ^^^^ N804
44 | pass
|
= help: Rename `self` to `cls`
Unsafe fix
40 40 |
41 41 |
42 42 | class MetaClass(ABCMeta):
43 |- def bad_method(self):
43 |+ def bad_method(cls):
44 44 | pass
45 45 |
46 46 | def good_method(cls):
N804.py:54:25: N804 First argument of a class method should be named `cls`
|
53 | class ClsInArgsClass(ABCMeta):
54 | def cls_as_argument(this, cls):
| ^^^^ N804
55 | pass
|
= help: Rename `this` to `cls`
N804.py:57:34: N804 First argument of a class method should be named `cls`
|
55 | pass
56 |
57 | def cls_as_pos_only_argument(this, cls, /):
| ^^^^ N804
58 | pass
|
= help: Rename `this` to `cls`
N804.py:60:33: N804 First argument of a class method should be named `cls`
|
58 | pass
59 |
60 | def cls_as_kw_only_argument(this, *, cls):
| ^^^^ N804
61 | pass
|
= help: Rename `this` to `cls`
N804.py:63:23: N804 First argument of a class method should be named `cls`
|
61 | pass
62 |
63 | def cls_as_varags(this, *cls):
| ^^^^ N804
64 | pass
|
= help: Rename `this` to `cls`
N804.py:66:23: N804 First argument of a class method should be named `cls`
|
64 | pass
65 |
66 | def cls_as_kwargs(this, **cls):
| ^^^^ N804
67 | pass
|
= help: Rename `this` to `cls`
N804.py:70:20: N804 [*] First argument of a class method should be named `cls`
|
69 | class RenamingInMethodBodyClass(ABCMeta):
70 | def bad_method(this):
| ^^^^ N804
71 | this = this
72 | this
|
= help: Rename `this` to `cls`
Unsafe fix
67 67 | pass
68 68 |
69 69 | class RenamingInMethodBodyClass(ABCMeta):
70 |- def bad_method(this):
71 |- this = this
72 |- this
70 |+ def bad_method(cls):
71 |+ cls = cls
72 |+ cls
73 73 |
74 74 | def bad_method(this):
75 75 | self = this
N804.py:74:20: N804 [*] First argument of a class method should be named `cls`
|
72 | this
73 |
74 | def bad_method(this):
| ^^^^ N804
75 | self = this
|
= help: Rename `this` to `cls`
Unsafe fix
71 71 | this = this
72 72 | this
73 73 |
74 |- def bad_method(this):
75 |- self = this
74 |+ def bad_method(cls):
75 |+ self = cls
76 76 |
77 77 | def func(x):
78 78 | return x

View File

@ -1,15 +1,26 @@
---
source: crates/ruff_linter/src/rules/pep8_naming/mod.rs
---
N805.py:7:20: N805 First argument of a method should be named `self`
N805.py:7:20: N805 [*] First argument of a method should be named `self`
|
6 | class Class:
7 | def bad_method(this):
| ^^^^ N805
8 | pass
|
= help: Rename `this` to `self`
N805.py:12:30: N805 First argument of a method should be named `self`
Unsafe fix
4 4 |
5 5 |
6 6 | class Class:
7 |- def bad_method(this):
7 |+ def bad_method(self):
8 8 | pass
9 9 |
10 10 | if False:
N805.py:12:30: N805 [*] First argument of a method should be named `self`
|
10 | if False:
11 |
@ -17,33 +28,77 @@ N805.py:12:30: N805 First argument of a method should be named `self`
| ^^^^ N805
13 | pass
|
= help: Rename `this` to `self`
N805.py:31:15: N805 First argument of a method should be named `self`
Unsafe fix
9 9 |
10 10 | if False:
11 11 |
12 |- def extra_bad_method(this):
12 |+ def extra_bad_method(self):
13 13 | pass
14 14 |
15 15 | def good_method(self):
N805.py:31:15: N805 [*] First argument of a method should be named `self`
|
30 | @pydantic.validator
31 | def lower(cls, my_field: str) -> str:
| ^^^ N805
32 | pass
|
= help: Rename `cls` to `self`
N805.py:35:15: N805 First argument of a method should be named `self`
Unsafe fix
28 28 | return x
29 29 |
30 30 | @pydantic.validator
31 |- def lower(cls, my_field: str) -> str:
31 |+ def lower(self, my_field: str) -> str:
32 32 | pass
33 33 |
34 34 | @pydantic.validator("my_field")
N805.py:35:15: N805 [*] First argument of a method should be named `self`
|
34 | @pydantic.validator("my_field")
35 | def lower(cls, my_field: str) -> str:
| ^^^ N805
36 | pass
|
= help: Rename `cls` to `self`
N805.py:64:29: N805 First argument of a method should be named `self`
Unsafe fix
32 32 | pass
33 33 |
34 34 | @pydantic.validator("my_field")
35 |- def lower(cls, my_field: str) -> str:
35 |+ def lower(self, my_field: str) -> str:
36 36 | pass
37 37 |
38 38 | def __init__(self):
N805.py:64:29: N805 [*] First argument of a method should be named `self`
|
62 | pass
63 |
64 | def bad_method_pos_only(this, blah, /, self, something: str):
64 | def bad_method_pos_only(this, blah, /, something: str):
| ^^^^ N805
65 | pass
|
= help: Rename `this` to `self`
N805.py:70:13: N805 First argument of a method should be named `self`
Unsafe fix
61 61 | def good_method_pos_only(self, blah, /, something: str):
62 62 | pass
63 63 |
64 |- def bad_method_pos_only(this, blah, /, something: str):
64 |+ def bad_method_pos_only(self, blah, /, something: str):
65 65 | pass
66 66 |
67 67 |
N805.py:70:13: N805 [*] First argument of a method should be named `self`
|
68 | class ModelClass:
69 | @hybrid_property
@ -51,29 +106,163 @@ N805.py:70:13: N805 First argument of a method should be named `self`
| ^^^ N805
71 | pass
|
= help: Rename `cls` to `self`
N805.py:78:13: N805 First argument of a method should be named `self`
Unsafe fix
67 67 |
68 68 | class ModelClass:
69 69 | @hybrid_property
70 |- def bad(cls):
70 |+ def bad(self):
71 71 | pass
72 72 |
73 73 | @bad.expression
N805.py:78:13: N805 [*] First argument of a method should be named `self`
|
77 | @bad.wtf
78 | def bad(cls):
| ^^^ N805
79 | pass
|
= help: Rename `cls` to `self`
N805.py:86:14: N805 First argument of a method should be named `self`
Unsafe fix
75 75 | pass
76 76 |
77 77 | @bad.wtf
78 |- def bad(cls):
78 |+ def bad(self):
79 79 | pass
80 80 |
81 81 | @hybrid_property
N805.py:86:14: N805 [*] First argument of a method should be named `self`
|
85 | @good.expression
86 | def good(cls):
| ^^^ N805
87 | pass
|
= help: Rename `cls` to `self`
N805.py:94:19: N805 First argument of a method should be named `self`
Unsafe fix
83 83 | pass
84 84 |
85 85 | @good.expression
86 |- def good(cls):
86 |+ def good(self):
87 87 | pass
88 88 |
89 89 | @good.wtf
N805.py:94:19: N805 [*] First argument of a method should be named `self`
|
93 | @foobar.thisisstatic
94 | def badstatic(foo):
| ^^^ N805
95 | pass
|
= help: Rename `foo` to `self`
Unsafe fix
91 91 | pass
92 92 |
93 93 | @foobar.thisisstatic
94 |- def badstatic(foo):
94 |+ def badstatic(self):
95 95 | pass
96 96 |
97 97 | class SelfInArgsClass:
N805.py:98:26: N805 First argument of a method should be named `self`
|
97 | class SelfInArgsClass:
98 | def self_as_argument(this, self):
| ^^^^ N805
99 | pass
|
= help: Rename `this` to `self`
N805.py:101:35: N805 First argument of a method should be named `self`
|
99 | pass
100 |
101 | def self_as_pos_only_argument(this, self, /):
| ^^^^ N805
102 | pass
|
= help: Rename `this` to `self`
N805.py:104:34: N805 First argument of a method should be named `self`
|
102 | pass
103 |
104 | def self_as_kw_only_argument(this, *, self):
| ^^^^ N805
105 | pass
|
= help: Rename `this` to `self`
N805.py:107:24: N805 First argument of a method should be named `self`
|
105 | pass
106 |
107 | def self_as_varags(this, *self):
| ^^^^ N805
108 | pass
|
= help: Rename `this` to `self`
N805.py:110:24: N805 First argument of a method should be named `self`
|
108 | pass
109 |
110 | def self_as_kwargs(this, **self):
| ^^^^ N805
111 | pass
|
= help: Rename `this` to `self`
N805.py:114:20: N805 [*] First argument of a method should be named `self`
|
113 | class RenamingInMethodBodyClass:
114 | def bad_method(this):
| ^^^^ N805
115 | this = this
116 | this
|
= help: Rename `this` to `self`
Unsafe fix
111 111 | pass
112 112 |
113 113 | class RenamingInMethodBodyClass:
114 |- def bad_method(this):
115 |- this = this
116 |- this
114 |+ def bad_method(self):
115 |+ self = self
116 |+ self
117 117 |
118 118 | def bad_method(this):
119 119 | self = this
N805.py:118:20: N805 [*] First argument of a method should be named `self`
|
116 | this
117 |
118 | def bad_method(this):
| ^^^^ N805
119 | self = this
|
= help: Rename `this` to `self`
Unsafe fix
115 115 | this = this
116 116 | this
117 117 |
118 |- def bad_method(this):
119 |- self = this
118 |+ def bad_method(self):
119 |+ self = self

View File

@ -1,15 +1,26 @@
---
source: crates/ruff_linter/src/rules/pep8_naming/mod.rs
---
N805.py:7:20: N805 First argument of a method should be named `self`
N805.py:7:20: N805 [*] First argument of a method should be named `self`
|
6 | class Class:
7 | def bad_method(this):
| ^^^^ N805
8 | pass
|
= help: Rename `this` to `self`
N805.py:12:30: N805 First argument of a method should be named `self`
Unsafe fix
4 4 |
5 5 |
6 6 | class Class:
7 |- def bad_method(this):
7 |+ def bad_method(self):
8 8 | pass
9 9 |
10 10 | if False:
N805.py:12:30: N805 [*] First argument of a method should be named `self`
|
10 | if False:
11 |
@ -17,17 +28,39 @@ N805.py:12:30: N805 First argument of a method should be named `self`
| ^^^^ N805
13 | pass
|
= help: Rename `this` to `self`
N805.py:64:29: N805 First argument of a method should be named `self`
Unsafe fix
9 9 |
10 10 | if False:
11 11 |
12 |- def extra_bad_method(this):
12 |+ def extra_bad_method(self):
13 13 | pass
14 14 |
15 15 | def good_method(self):
N805.py:64:29: N805 [*] First argument of a method should be named `self`
|
62 | pass
63 |
64 | def bad_method_pos_only(this, blah, /, self, something: str):
64 | def bad_method_pos_only(this, blah, /, something: str):
| ^^^^ N805
65 | pass
|
= help: Rename `this` to `self`
N805.py:70:13: N805 First argument of a method should be named `self`
Unsafe fix
61 61 | def good_method_pos_only(self, blah, /, something: str):
62 62 | pass
63 63 |
64 |- def bad_method_pos_only(this, blah, /, something: str):
64 |+ def bad_method_pos_only(self, blah, /, something: str):
65 65 | pass
66 66 |
67 67 |
N805.py:70:13: N805 [*] First argument of a method should be named `self`
|
68 | class ModelClass:
69 | @hybrid_property
@ -35,21 +68,144 @@ N805.py:70:13: N805 First argument of a method should be named `self`
| ^^^ N805
71 | pass
|
= help: Rename `cls` to `self`
N805.py:78:13: N805 First argument of a method should be named `self`
Unsafe fix
67 67 |
68 68 | class ModelClass:
69 69 | @hybrid_property
70 |- def bad(cls):
70 |+ def bad(self):
71 71 | pass
72 72 |
73 73 | @bad.expression
N805.py:78:13: N805 [*] First argument of a method should be named `self`
|
77 | @bad.wtf
78 | def bad(cls):
| ^^^ N805
79 | pass
|
= help: Rename `cls` to `self`
N805.py:94:19: N805 First argument of a method should be named `self`
Unsafe fix
75 75 | pass
76 76 |
77 77 | @bad.wtf
78 |- def bad(cls):
78 |+ def bad(self):
79 79 | pass
80 80 |
81 81 | @hybrid_property
N805.py:94:19: N805 [*] First argument of a method should be named `self`
|
93 | @foobar.thisisstatic
94 | def badstatic(foo):
| ^^^ N805
95 | pass
|
= help: Rename `foo` to `self`
Unsafe fix
91 91 | pass
92 92 |
93 93 | @foobar.thisisstatic
94 |- def badstatic(foo):
94 |+ def badstatic(self):
95 95 | pass
96 96 |
97 97 | class SelfInArgsClass:
N805.py:98:26: N805 First argument of a method should be named `self`
|
97 | class SelfInArgsClass:
98 | def self_as_argument(this, self):
| ^^^^ N805
99 | pass
|
= help: Rename `this` to `self`
N805.py:101:35: N805 First argument of a method should be named `self`
|
99 | pass
100 |
101 | def self_as_pos_only_argument(this, self, /):
| ^^^^ N805
102 | pass
|
= help: Rename `this` to `self`
N805.py:104:34: N805 First argument of a method should be named `self`
|
102 | pass
103 |
104 | def self_as_kw_only_argument(this, *, self):
| ^^^^ N805
105 | pass
|
= help: Rename `this` to `self`
N805.py:107:24: N805 First argument of a method should be named `self`
|
105 | pass
106 |
107 | def self_as_varags(this, *self):
| ^^^^ N805
108 | pass
|
= help: Rename `this` to `self`
N805.py:110:24: N805 First argument of a method should be named `self`
|
108 | pass
109 |
110 | def self_as_kwargs(this, **self):
| ^^^^ N805
111 | pass
|
= help: Rename `this` to `self`
N805.py:114:20: N805 [*] First argument of a method should be named `self`
|
113 | class RenamingInMethodBodyClass:
114 | def bad_method(this):
| ^^^^ N805
115 | this = this
116 | this
|
= help: Rename `this` to `self`
Unsafe fix
111 111 | pass
112 112 |
113 113 | class RenamingInMethodBodyClass:
114 |- def bad_method(this):
115 |- this = this
116 |- this
114 |+ def bad_method(self):
115 |+ self = self
116 |+ self
117 117 |
118 118 | def bad_method(this):
119 119 | self = this
N805.py:118:20: N805 [*] First argument of a method should be named `self`
|
116 | this
117 |
118 | def bad_method(this):
| ^^^^ N805
119 | self = this
|
= help: Rename `this` to `self`
Unsafe fix
115 115 | this = this
116 116 | this
117 117 |
118 |- def bad_method(this):
119 |- self = this
118 |+ def bad_method(self):
119 |+ self = self

View File

@ -1,23 +1,45 @@
---
source: crates/ruff_linter/src/rules/pep8_naming/mod.rs
---
N804.py:5:27: N804 First argument of a class method should be named `cls`
N804.py:5:27: N804 [*] First argument of a class method should be named `cls`
|
4 | class Class:
5 | def __init_subclass__(self, default_name, **kwargs):
| ^^^^ N804
6 | ...
|
= help: Rename `self` to `cls`
N804.py:13:18: N804 First argument of a class method should be named `cls`
Unsafe fix
2 2 |
3 3 |
4 4 | class Class:
5 |- def __init_subclass__(self, default_name, **kwargs):
5 |+ def __init_subclass__(cls, default_name, **kwargs):
6 6 | ...
7 7 |
8 8 | @classmethod
N804.py:13:18: N804 [*] First argument of a class method should be named `cls`
|
12 | @classmethod
13 | def stillBad(self, x, /, other):
| ^^^^ N804
14 | ...
|
= help: Rename `self` to `cls`
N804.py:21:18: N804 First argument of a class method should be named `cls`
Unsafe fix
10 10 | ...
11 11 |
12 12 | @classmethod
13 |- def stillBad(self, x, /, other):
13 |+ def stillBad(cls, x, /, other):
14 14 | ...
15 15 |
16 16 |
N804.py:21:18: N804 [*] First argument of a class method should be named `cls`
|
19 | pass
20 |
@ -25,5 +47,12 @@ N804.py:21:18: N804 First argument of a class method should be named `cls`
| ^^^^ N804
22 | pass
|
= help: Rename `self` to `cls`
Unsafe fix
18 18 | def badAllowed(self):
19 19 | pass
20 20 |
21 |- def stillBad(self):
21 |+ def stillBad(cls):
22 22 | pass

View File

@ -1,7 +1,7 @@
---
source: crates/ruff_linter/src/rules/pep8_naming/mod.rs
---
N805.py:10:18: N805 First argument of a method should be named `self`
N805.py:10:18: N805 [*] First argument of a method should be named `self`
|
8 | pass
9 |
@ -9,8 +9,19 @@ N805.py:10:18: N805 First argument of a method should be named `self`
| ^^^^ N805
11 | pass
|
= help: Rename `this` to `self`
N805.py:18:22: N805 First argument of a method should be named `self`
Unsafe fix
7 7 | def badAllowed(this):
8 8 | pass
9 9 |
10 |- def stillBad(this):
10 |+ def stillBad(self):
11 11 | pass
12 12 |
13 13 | if False:
N805.py:18:22: N805 [*] First argument of a method should be named `self`
|
16 | pass
17 |
@ -18,22 +29,55 @@ N805.py:18:22: N805 First argument of a method should be named `self`
| ^^^^ N805
19 | pass
|
= help: Rename `this` to `self`
N805.py:26:18: N805 First argument of a method should be named `self`
Unsafe fix
15 15 | def badAllowed(this):
16 16 | pass
17 17 |
18 |- def stillBad(this):
18 |+ def stillBad(self):
19 19 | pass
20 20 |
21 21 | @pydantic.validator
N805.py:26:18: N805 [*] First argument of a method should be named `self`
|
25 | @pydantic.validator
26 | def stillBad(cls, my_field: str) -> str:
| ^^^ N805
27 | pass
|
= help: Rename `cls` to `self`
N805.py:34:18: N805 First argument of a method should be named `self`
Unsafe fix
23 23 | pass
24 24 |
25 25 | @pydantic.validator
26 |- def stillBad(cls, my_field: str) -> str:
26 |+ def stillBad(self, my_field: str) -> str:
27 27 | pass
28 28 |
29 29 | @pydantic.validator("my_field")
N805.py:34:18: N805 [*] First argument of a method should be named `self`
|
33 | @pydantic.validator("my_field")
34 | def stillBad(cls, my_field: str) -> str:
| ^^^ N805
35 | pass
|
= help: Rename `cls` to `self`
Unsafe fix
31 31 | pass
32 32 |
33 33 | @pydantic.validator("my_field")
34 |- def stillBad(cls, my_field: str) -> str:
34 |+ def stillBad(self, my_field: str) -> str:
35 35 | pass
36 36 |
37 37 | @classmethod
N805.py:58:18: N805 First argument of a method should be named `self`
|
@ -43,5 +87,4 @@ N805.py:58:18: N805 First argument of a method should be named `self`
| ^^^^ N805
59 | pass
|
= help: Rename `this` to `self`

View File

@ -1,15 +1,26 @@
---
source: crates/ruff_linter/src/rules/pep8_naming/mod.rs
---
N805.py:7:20: N805 First argument of a method should be named `self`
N805.py:7:20: N805 [*] First argument of a method should be named `self`
|
6 | class Class:
7 | def bad_method(this):
| ^^^^ N805
8 | pass
|
= help: Rename `this` to `self`
N805.py:12:30: N805 First argument of a method should be named `self`
Unsafe fix
4 4 |
5 5 |
6 6 | class Class:
7 |- def bad_method(this):
7 |+ def bad_method(self):
8 8 | pass
9 9 |
10 10 | if False:
N805.py:12:30: N805 [*] First argument of a method should be named `self`
|
10 | if False:
11 |
@ -17,33 +28,77 @@ N805.py:12:30: N805 First argument of a method should be named `self`
| ^^^^ N805
13 | pass
|
= help: Rename `this` to `self`
N805.py:31:15: N805 First argument of a method should be named `self`
Unsafe fix
9 9 |
10 10 | if False:
11 11 |
12 |- def extra_bad_method(this):
12 |+ def extra_bad_method(self):
13 13 | pass
14 14 |
15 15 | def good_method(self):
N805.py:31:15: N805 [*] First argument of a method should be named `self`
|
30 | @pydantic.validator
31 | def lower(cls, my_field: str) -> str:
| ^^^ N805
32 | pass
|
= help: Rename `cls` to `self`
N805.py:35:15: N805 First argument of a method should be named `self`
Unsafe fix
28 28 | return x
29 29 |
30 30 | @pydantic.validator
31 |- def lower(cls, my_field: str) -> str:
31 |+ def lower(self, my_field: str) -> str:
32 32 | pass
33 33 |
34 34 | @pydantic.validator("my_field")
N805.py:35:15: N805 [*] First argument of a method should be named `self`
|
34 | @pydantic.validator("my_field")
35 | def lower(cls, my_field: str) -> str:
| ^^^ N805
36 | pass
|
= help: Rename `cls` to `self`
N805.py:64:29: N805 First argument of a method should be named `self`
Unsafe fix
32 32 | pass
33 33 |
34 34 | @pydantic.validator("my_field")
35 |- def lower(cls, my_field: str) -> str:
35 |+ def lower(self, my_field: str) -> str:
36 36 | pass
37 37 |
38 38 | def __init__(self):
N805.py:64:29: N805 [*] First argument of a method should be named `self`
|
62 | pass
63 |
64 | def bad_method_pos_only(this, blah, /, self, something: str):
64 | def bad_method_pos_only(this, blah, /, something: str):
| ^^^^ N805
65 | pass
|
= help: Rename `this` to `self`
N805.py:70:13: N805 First argument of a method should be named `self`
Unsafe fix
61 61 | def good_method_pos_only(self, blah, /, something: str):
62 62 | pass
63 63 |
64 |- def bad_method_pos_only(this, blah, /, something: str):
64 |+ def bad_method_pos_only(self, blah, /, something: str):
65 65 | pass
66 66 |
67 67 |
N805.py:70:13: N805 [*] First argument of a method should be named `self`
|
68 | class ModelClass:
69 | @hybrid_property
@ -51,21 +106,144 @@ N805.py:70:13: N805 First argument of a method should be named `self`
| ^^^ N805
71 | pass
|
= help: Rename `cls` to `self`
N805.py:78:13: N805 First argument of a method should be named `self`
Unsafe fix
67 67 |
68 68 | class ModelClass:
69 69 | @hybrid_property
70 |- def bad(cls):
70 |+ def bad(self):
71 71 | pass
72 72 |
73 73 | @bad.expression
N805.py:78:13: N805 [*] First argument of a method should be named `self`
|
77 | @bad.wtf
78 | def bad(cls):
| ^^^ N805
79 | pass
|
= help: Rename `cls` to `self`
N805.py:86:14: N805 First argument of a method should be named `self`
Unsafe fix
75 75 | pass
76 76 |
77 77 | @bad.wtf
78 |- def bad(cls):
78 |+ def bad(self):
79 79 | pass
80 80 |
81 81 | @hybrid_property
N805.py:86:14: N805 [*] First argument of a method should be named `self`
|
85 | @good.expression
86 | def good(cls):
| ^^^ N805
87 | pass
|
= help: Rename `cls` to `self`
Unsafe fix
83 83 | pass
84 84 |
85 85 | @good.expression
86 |- def good(cls):
86 |+ def good(self):
87 87 | pass
88 88 |
89 89 | @good.wtf
N805.py:98:26: N805 First argument of a method should be named `self`
|
97 | class SelfInArgsClass:
98 | def self_as_argument(this, self):
| ^^^^ N805
99 | pass
|
= help: Rename `this` to `self`
N805.py:101:35: N805 First argument of a method should be named `self`
|
99 | pass
100 |
101 | def self_as_pos_only_argument(this, self, /):
| ^^^^ N805
102 | pass
|
= help: Rename `this` to `self`
N805.py:104:34: N805 First argument of a method should be named `self`
|
102 | pass
103 |
104 | def self_as_kw_only_argument(this, *, self):
| ^^^^ N805
105 | pass
|
= help: Rename `this` to `self`
N805.py:107:24: N805 First argument of a method should be named `self`
|
105 | pass
106 |
107 | def self_as_varags(this, *self):
| ^^^^ N805
108 | pass
|
= help: Rename `this` to `self`
N805.py:110:24: N805 First argument of a method should be named `self`
|
108 | pass
109 |
110 | def self_as_kwargs(this, **self):
| ^^^^ N805
111 | pass
|
= help: Rename `this` to `self`
N805.py:114:20: N805 [*] First argument of a method should be named `self`
|
113 | class RenamingInMethodBodyClass:
114 | def bad_method(this):
| ^^^^ N805
115 | this = this
116 | this
|
= help: Rename `this` to `self`
Unsafe fix
111 111 | pass
112 112 |
113 113 | class RenamingInMethodBodyClass:
114 |- def bad_method(this):
115 |- this = this
116 |- this
114 |+ def bad_method(self):
115 |+ self = self
116 |+ self
117 117 |
118 118 | def bad_method(this):
119 119 | self = this
N805.py:118:20: N805 [*] First argument of a method should be named `self`
|
116 | this
117 |
118 | def bad_method(this):
| ^^^^ N805
119 | self = this
|
= help: Rename `this` to `self`
Unsafe fix
115 115 | this = this
116 116 | this
117 117 |
118 |- def bad_method(this):
119 |- self = this
118 |+ def bad_method(self):
119 |+ self = self