Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pep8_naming] Add fixes N804 and N805 #10215

Merged
merged 9 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 10 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,12 @@ 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
Loading