Skip to content

Commit f424f00

Browse files
use is_known_dunder_method instead
1 parent 1be1ffa commit f424f00

File tree

4 files changed

+18
-29
lines changed

4 files changed

+18
-29
lines changed

crates/ruff_linter/resources/test/fixtures/flake8_boolean_trap/FBT.py

+14-12
Original file line numberDiff line numberDiff line change
@@ -93,18 +93,6 @@ def foo(self, value: bool) -> None:
9393
def foo(self) -> None:
9494
object.__setattr__(self, "flag", True)
9595

96-
def __and__(self, other: bool) -> bool: ...
97-
def __rand__(self, other: bool) -> bool: ...
98-
def __iand__(self, other: bool) -> bool: ...
99-
def __or__(self, other: bool) -> bool: ...
100-
def __ror__(self, other: bool) -> bool: ...
101-
def __ior__(self, other: bool) -> bool: ...
102-
def __xor__(self, other: bool) -> bool: ...
103-
def __rxor__(self, other: bool) -> bool: ...
104-
def __ixor__(self, other: bool) -> bool: ...
105-
106-
107-
10896

10997
from typing import Optional, Union
11098

@@ -166,3 +154,17 @@ def __post_init__(self, force: bool) -> None:
166154

167155
class Settings(BaseSettings):
168156
foo: bool = Field(True, exclude=True)
157+
158+
159+
# https://github.com/astral-sh/ruff/issues/14202
160+
class SupportsOperators:
161+
162+
def __and__(self, other: bool) -> bool: ...
163+
def __rand__(self, other: bool) -> bool: ...
164+
def __iand__(self, other: bool) -> bool: ...
165+
def __or__(self, other: bool) -> bool: ...
166+
def __ror__(self, other: bool) -> bool: ...
167+
def __ior__(self, other: bool) -> bool: ...
168+
def __xor__(self, other: bool) -> bool: ...
169+
def __rxor__(self, other: bool) -> bool: ...
170+
def __ixor__(self, other: bool) -> bool: ...

crates/ruff_linter/src/rules/flake8_boolean_trap/helpers.rs

+2-15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use ruff_python_ast::{self as ast, Expr};
33
use ruff_python_semantic::SemanticModel;
44

55
use crate::checkers::ast::Checker;
6+
use crate::rules::pylint::helpers::is_known_dunder_method;
67
use crate::settings::LinterSettings;
78

89
/// Returns `true` if a function call is allowed to use a boolean trap.
@@ -68,21 +69,7 @@ pub(super) fn is_user_allowed_func_call(
6869

6970
/// Returns `true` if a function definition is allowed to use a boolean trap.
7071
pub(super) fn is_allowed_func_def(name: &str) -> bool {
71-
matches!(
72-
name,
73-
"__setitem__"
74-
| "__post_init__"
75-
// exclude boolean operators
76-
| "__and__"
77-
| "__rand__"
78-
| "__iand__"
79-
| "__or__"
80-
| "__ror__"
81-
| "__ior__"
82-
| "__xor__"
83-
| "__rxor__"
84-
| "__ixor__"
85-
)
72+
is_known_dunder_method(name)
8673
}
8774

8875
/// Returns `true` if an argument is allowed to use a boolean trap. To return

crates/ruff_linter/src/rules/pylint/helpers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl<'a> Visitor<'_> for SequenceIndexVisitor<'a> {
167167
}
168168

169169
/// Returns `true` if a method is a known dunder method.
170-
pub(super) fn is_known_dunder_method(method: &str) -> bool {
170+
pub(crate) fn is_known_dunder_method(method: &str) -> bool {
171171
matches!(
172172
method,
173173
"__abs__"

crates/ruff_linter/src/rules/pylint/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//! Rules from [Pylint](https://pypi.org/project/pylint/).
2-
mod helpers;
2+
pub(crate) mod helpers;
33
pub(crate) mod rules;
44
pub mod settings;
55

0 commit comments

Comments
 (0)