@@ -7,12 +7,12 @@ use ruff_python_semantic::analyze::visibility;
77
88use crate :: Violation ;
99use crate :: checkers:: ast:: Checker ;
10- use crate :: preview:: is_bool_subtype_of_annotation_enabled;
1110use 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 ) ]
10297pub ( 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`).
177155fn match_annotation_to_complex_bool ( annotation : & Expr , semantic : & SemanticModel ) -> bool {
0 commit comments