Skip to content

Commit 0370d8a

Browse files
authored
[flake8-boolean-trap] Stabilize lint bool suprtypes in boolean-type-hint-positional-argument (FBT001) (#18520)
Feel free to complain about the rephrasing in the docs!
1 parent 815e336 commit 0370d8a

File tree

5 files changed

+18
-156
lines changed

5 files changed

+18
-156
lines changed

crates/ruff_linter/src/preview.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ pub(crate) const fn is_suspicious_function_reference_enabled(settings: &LinterSe
2323
settings.preview.is_enabled()
2424
}
2525

26-
// https://github.com/astral-sh/ruff/pull/7501
27-
pub(crate) const fn is_bool_subtype_of_annotation_enabled(settings: &LinterSettings) -> bool {
28-
settings.preview.is_enabled()
29-
}
30-
3126
// https://github.com/astral-sh/ruff/pull/10759
3227
pub(crate) const fn is_comprehension_with_min_max_sum_enabled(settings: &LinterSettings) -> bool {
3328
settings.preview.is_enabled()

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

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ mod tests {
1212

1313
use crate::registry::Rule;
1414
use crate::settings::LinterSettings;
15-
use crate::settings::types::PreviewMode;
1615
use crate::test::test_path;
1716
use crate::{assert_messages, settings};
1817

@@ -29,24 +28,6 @@ mod tests {
2928
Ok(())
3029
}
3130

32-
#[test_case(Rule::BooleanTypeHintPositionalArgument, Path::new("FBT.py"))]
33-
fn preview_rules(rule_code: Rule, path: &Path) -> Result<()> {
34-
let snapshot = format!(
35-
"preview__{}_{}",
36-
rule_code.noqa_code(),
37-
path.to_string_lossy()
38-
);
39-
let diagnostics = test_path(
40-
Path::new("flake8_boolean_trap").join(path).as_path(),
41-
&settings::LinterSettings {
42-
preview: PreviewMode::Enabled,
43-
..settings::LinterSettings::for_rule(rule_code)
44-
},
45-
)?;
46-
assert_messages!(snapshot, diagnostics);
47-
Ok(())
48-
}
49-
5031
#[test]
5132
fn extend_allowed_callable() -> Result<()> {
5233
let diagnostics = test_path(

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

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ use ruff_python_semantic::analyze::visibility;
77

88
use crate::Violation;
99
use crate::checkers::ast::Checker;
10-
use crate::preview::is_bool_subtype_of_annotation_enabled;
1110
use crate::rules::flake8_boolean_trap::helpers::is_allowed_func_def;
1211

1312
/// ## What it does
1413
/// Checks for the use of boolean positional arguments in function definitions,
15-
/// as determined by the presence of a `bool` type hint.
14+
/// as determined by the presence of a type hint containing `bool` as an
15+
/// evident subtype - e.g. `bool`, `bool | int`, `typing.Optional[bool]`, etc.
1616
///
1717
/// ## Why is this bad?
1818
/// Calling a function with boolean positional arguments is confusing as the
@@ -30,9 +30,6 @@ use crate::rules::flake8_boolean_trap::helpers::is_allowed_func_def;
3030
/// Dunder methods that define operators are exempt from this rule, as are
3131
/// setters and `@override` definitions.
3232
///
33-
/// In [preview], this rule will also flag annotations that include boolean
34-
/// variants, like `bool | int`.
35-
///
3633
/// ## Example
3734
///
3835
/// ```python
@@ -96,8 +93,6 @@ use crate::rules::flake8_boolean_trap::helpers::is_allowed_func_def;
9693
/// ## References
9794
/// - [Python documentation: Calls](https://docs.python.org/3/reference/expressions.html#calls)
9895
/// - [_How to Avoid “The Boolean Trap”_ by Adam Johnson](https://adamj.eu/tech/2021/07/10/python-type-hints-how-to-avoid-the-boolean-trap/)
99-
///
100-
/// [preview]: https://docs.astral.sh/ruff/preview/
10196
#[derive(ViolationMetadata)]
10297
pub(crate) struct BooleanTypeHintPositionalArgument;
10398

@@ -128,14 +123,8 @@ pub(crate) fn boolean_type_hint_positional_argument(
128123
let Some(annotation) = parameter.annotation() else {
129124
continue;
130125
};
131-
if is_bool_subtype_of_annotation_enabled(checker.settings) {
132-
if !match_annotation_to_complex_bool(annotation, checker.semantic()) {
133-
continue;
134-
}
135-
} else {
136-
if !match_annotation_to_literal_bool(annotation) {
137-
continue;
138-
}
126+
if !match_annotation_to_complex_bool(annotation, checker.semantic()) {
127+
continue;
139128
}
140129

141130
// Allow Boolean type hints in setters.
@@ -161,17 +150,6 @@ pub(crate) fn boolean_type_hint_positional_argument(
161150
}
162151
}
163152

164-
/// Returns `true` if the annotation is a boolean type hint (e.g., `bool`).
165-
fn match_annotation_to_literal_bool(annotation: &Expr) -> bool {
166-
match annotation {
167-
// Ex) `True`
168-
Expr::Name(name) => &name.id == "bool",
169-
// Ex) `"True"`
170-
Expr::StringLiteral(ast::ExprStringLiteral { value, .. }) => value == "bool",
171-
_ => false,
172-
}
173-
}
174-
175153
/// Returns `true` if the annotation is a boolean type hint (e.g., `bool`), or a type hint that
176154
/// includes boolean as a variant (e.g., `bool | int`).
177155
fn match_annotation_to_complex_bool(annotation: &Expr, semantic: &SemanticModel) -> bool {

crates/ruff_linter/src/rules/flake8_boolean_trap/snapshots/ruff_linter__rules__flake8_boolean_trap__tests__FBT001_FBT.py.snap

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
---
22
source: crates/ruff_linter/src/rules/flake8_boolean_trap/mod.rs
3-
snapshot_kind: text
43
---
54
FBT.py:4:5: FBT001 Boolean-typed positional argument in function definition
65
|
@@ -89,3 +88,17 @@ FBT.py:90:19: FBT001 Boolean-typed positional argument in function definition
8988
| ^^^^^ FBT001
9089
91 | pass
9190
|
91+
92+
FBT.py:100:10: FBT001 Boolean-typed positional argument in function definition
93+
|
94+
100 | def func(x: Union[list, Optional[int | str | float | bool]]):
95+
| ^ FBT001
96+
101 | pass
97+
|
98+
99+
FBT.py:104:10: FBT001 Boolean-typed positional argument in function definition
100+
|
101+
104 | def func(x: bool | str):
102+
| ^ FBT001
103+
105 | pass
104+
|

crates/ruff_linter/src/rules/flake8_boolean_trap/snapshots/ruff_linter__rules__flake8_boolean_trap__tests__preview__FBT001_FBT.py.snap

Lines changed: 0 additions & 105 deletions
This file was deleted.

0 commit comments

Comments
 (0)