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

FBT001: exclude boolean operators #14203

Merged
merged 6 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
randolf-scholz marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,17 @@ def __post_init__(self, force: bool) -> None:

class Settings(BaseSettings):
foo: bool = Field(True, exclude=True)


# https://github.com/astral-sh/ruff/issues/14202
class SupportsOperators:

def __and__(self, other: bool) -> bool: ...
def __rand__(self, other: bool) -> bool: ...
def __iand__(self, other: bool) -> bool: ...
def __or__(self, other: bool) -> bool: ...
def __ror__(self, other: bool) -> bool: ...
def __ior__(self, other: bool) -> bool: ...
def __xor__(self, other: bool) -> bool: ...
def __rxor__(self, other: bool) -> bool: ...
def __ixor__(self, other: bool) -> bool: ...
74 changes: 73 additions & 1 deletion crates/ruff_linter/src/rules/flake8_boolean_trap/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,81 @@ pub(super) fn is_user_allowed_func_call(
})
}

/// Returns `true` if a function defines a binary operator.
/// This only includes operators, i.e. functions that are usually not called directly.
pub(super) fn is_binary_operator_method(name: &str) -> bool {
matches!(
name,
"__contains__" // in
// item access ([])
| "__getitem__" // []
| "__setitem__" // []=
| "__delitem__" // del []
// addition (+)
| "__add__" // +
| "__radd__" // +
| "__iadd__" // +=
// subtraction (-)
| "__sub__" // -
| "__rsub__" // -
| "__isub__" // -=
// multiplication (*)
| "__mul__" // *
| "__rmul__" // *
| "__imul__" // *=
// division (/)
| "__truediv__" // /
| "__rtruediv__" // /
| "__itruediv__" // /=
// floor division (//)
| "__floordiv__" // //
| "__rfloordiv__" // //
| "__ifloordiv__" // //=
// remainder (%)
| "__mod__" // %
| "__rmod__" // %
| "__imod__" // %=
// exponentiation (**)
| "__pow__" // **
| "__rpow__" // **
| "__ipow__" // **=
// left shift (<<)
| "__lshift__" // <<
| "__rlshift__" // <<
| "__ilshift__" // <<=
// right shift (>>)
| "__rshift__" // >>
| "__rrshift__" // >>
| "__irshift__" // >>=
// matrix multiplication (@)
| "__matmul__" // @
| "__rmatmul__" // @
| "__imatmul__" // @=
// meet (&)
| "__and__" // &
| "__rand__" // &
| "__iand__" // &=
// join (|)
| "__or__" // |
| "__ror__" // |
| "__ior__" // |=
// xor (^)
| "__xor__" // ^
| "__rxor__" // ^
| "__ixor__" // ^=
// comparison (>, <, >=, <=, ==, !=)
| "__gt__" // >
| "__lt__" // <
| "__ge__" // >=
| "__le__" // <=
| "__eq__" // ==
| "__ne__" // !=
)
}

/// Returns `true` if a function definition is allowed to use a boolean trap.
pub(super) fn is_allowed_func_def(name: &str) -> bool {
matches!(name, "__setitem__" | "__post_init__")
matches!(name, "__post_init__") || is_binary_operator_method(name)
}

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