Ignore setters in flake8-boolean-trap (#3092)

This commit is contained in:
Charlie Marsh 2023-02-21 14:31:00 -05:00 committed by GitHub
parent 37df07d2e0
commit d9fd78d907
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 57 additions and 10 deletions

View file

@ -68,3 +68,11 @@ class Registry:
# FBT001: Boolean positional arg in function definition
def __setitem__(self, switch: Switch, value: bool) -> None:
self._switches[switch.value] = value
@foo.setter
def foo(self, value: bool) -> None:
pass
# FBT001: Boolean positional arg in function definition
def foo(self, value: bool) -> None:
pass

View file

@ -706,7 +706,12 @@ where
.rules
.enabled(&Rule::BooleanPositionalArgInFunctionDefinition)
{
flake8_boolean_trap::rules::check_positional_boolean_in_def(self, name, args);
flake8_boolean_trap::rules::check_positional_boolean_in_def(
self,
name,
decorator_list,
args,
);
}
if self
@ -715,7 +720,10 @@ where
.enabled(&Rule::BooleanDefaultValueInFunctionDefinition)
{
flake8_boolean_trap::rules::check_boolean_default_value_in_function_definition(
self, name, args,
self,
name,
decorator_list,
args,
);
}

View file

@ -1,6 +1,8 @@
use ruff_macros::{define_violation, derive_message_formats};
use rustpython_parser::ast::{Arguments, Constant, Expr, ExprKind};
use ruff_macros::{define_violation, derive_message_formats};
use crate::ast::helpers::collect_call_path;
use crate::ast::types::Range;
use crate::checkers::ast::Checker;
use crate::registry::{Diagnostic, DiagnosticKind};
@ -91,10 +93,23 @@ fn add_if_boolean(checker: &mut Checker, arg: &Expr, kind: DiagnosticKind) {
}
}
pub fn check_positional_boolean_in_def(checker: &mut Checker, name: &str, arguments: &Arguments) {
pub fn check_positional_boolean_in_def(
checker: &mut Checker,
name: &str,
decorator_list: &[Expr],
arguments: &Arguments,
) {
if FUNC_DEF_NAME_ALLOWLIST.contains(&name) {
return;
}
if decorator_list.iter().any(|expr| {
let call_path = collect_call_path(expr);
call_path.as_slice() == [name, "setter"]
}) {
return;
}
for arg in arguments.posonlyargs.iter().chain(arguments.args.iter()) {
if arg.node.annotation.is_none() {
continue;
@ -125,11 +140,20 @@ pub fn check_positional_boolean_in_def(checker: &mut Checker, name: &str, argume
pub fn check_boolean_default_value_in_function_definition(
checker: &mut Checker,
name: &str,
decorator_list: &[Expr],
arguments: &Arguments,
) {
if FUNC_DEF_NAME_ALLOWLIST.contains(&name) {
return;
}
if decorator_list.iter().any(|expr| {
let call_path = collect_call_path(expr);
call_path.as_slice() == [name, "setter"]
}) {
return;
}
for arg in &arguments.defaults {
add_if_boolean(checker, arg, BooleanDefaultValueInFunctionDefinition.into());
}

View file

@ -1,5 +1,5 @@
---
source: src/rules/flake8_boolean_trap/mod.rs
source: crates/ruff/src/rules/flake8_boolean_trap/mod.rs
expression: diagnostics
---
- kind:
@ -82,4 +82,14 @@ expression: diagnostics
column: 45
fix: ~
parent: ~
- kind:
BooleanPositionalArgInFunctionDefinition: ~
location:
row: 77
column: 18
end_location:
row: 77
column: 29
fix: ~
parent: ~

View file

@ -185,11 +185,8 @@ fn method_visibility(stmt: &Stmt) -> Visibility {
// Is this a setter or deleter?
if decorator_list.iter().any(|expr| {
let call_path = collect_call_path(expr);
if call_path.len() > 1 {
call_path[0] == name
} else {
false
}
call_path.as_slice() == [name, "setter"]
|| call_path.as_slice() == [name, "deleter"]
}) {
return Visibility::Private;
}