Skip to content

Commit

Permalink
[pep8_naming] Add fixes N804 and N805 (astral-sh#10215)
Browse files Browse the repository at this point in the history
## 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`
  • Loading branch information
GtrMo authored and nkxxll committed Mar 10, 2024
1 parent dcb2459 commit 82f56a7
Show file tree
Hide file tree
Showing 14 changed files with 1,098 additions and 270 deletions.
23 changes: 23 additions & 0 deletions crates/ruff_linter/resources/test/fixtures/pep8_naming/N804.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,29 @@ def good_method(cls):
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
26 changes: 25 additions & 1 deletion crates/ruff_linter/resources/test/fixtures/pep8_naming/N805.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -93,3 +93,27 @@ def good(self):
@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
13 changes: 11 additions & 2 deletions crates/ruff_linter/src/checkers/ast/analyze/deferred_scopes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand All @@ -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,
Expand Down Expand Up @@ -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);
Expand Down
24 changes: 0 additions & 24 deletions crates/ruff_linter/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit 82f56a7

Please sign in to comment.