forked from astral-sh/ruff
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Summary Implements `redundant-bool-literal` ## Test Plan <!-- How was it tested? --> `cargo test` The ecosystem results are all correct, but for `Airflow` the rule is not relevant due to the use of overloading (and is marked as unsafe correctly). --------- Co-authored-by: Charlie Marsh <[email protected]>
- Loading branch information
Showing
11 changed files
with
421 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from typing import Literal | ||
|
||
|
||
def func1(arg1: Literal[True, False]): | ||
... | ||
|
||
|
||
def func2(arg1: Literal[True, False, True]): | ||
... | ||
|
||
|
||
def func3() -> Literal[True, False]: | ||
... | ||
|
||
|
||
def func4(arg1: Literal[True, False] | bool): | ||
... | ||
|
||
|
||
def func5(arg1: Literal[False, True]): | ||
... | ||
|
||
|
||
def func6(arg1: Literal[True, False, "hello", "world"]): | ||
... | ||
|
||
# ok | ||
def good_func1(arg1: bool): | ||
... | ||
|
||
|
||
def good_func2(arg1: Literal[True]): | ||
... |
20 changes: 20 additions & 0 deletions
20
crates/ruff_linter/resources/test/fixtures/ruff/RUF038.pyi
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from typing import Literal | ||
|
||
|
||
def func1(arg1: Literal[True, False]): ... | ||
|
||
def func2(arg1: Literal[True, False, True]): ... | ||
|
||
def func3() -> Literal[True, False]: ... | ||
|
||
def func4(arg1: Literal[True, False] | bool): ... | ||
|
||
def func5(arg1: Literal[False, True]): ... | ||
|
||
def func6(arg1: Literal[True, False, "hello", "world"]): ... | ||
|
||
# ok | ||
def good_func1(arg1: bool): ... | ||
|
||
def good_func2(arg1: Literal[True]): ... | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
crates/ruff_linter/src/rules/ruff/rules/redundant_bool_literal.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
use ruff_diagnostics::{Diagnostic, Edit, Fix, FixAvailability, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::Expr; | ||
use ruff_python_semantic::analyze::typing::traverse_literal; | ||
use ruff_text_size::Ranged; | ||
|
||
use bitflags::bitflags; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for `Literal[True, False]` type annotations. | ||
/// | ||
/// ## Why is this bad? | ||
/// `Literal[True, False]` can be replaced with `bool` in type annotations, | ||
/// which has the same semantic meaning but is more concise and readable. | ||
/// | ||
/// `bool` type has exactly two constant instances: `True` and `False`. Static | ||
/// type checkers such as [mypy] treat `Literal[True, False]` as equivalent to | ||
/// `bool` in a type annotation. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// from typing import Literal | ||
/// | ||
/// x: Literal[True, False] | ||
/// y: Literal[True, False, "hello", "world"] | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// from typing import Literal | ||
/// | ||
/// x: bool | ||
/// y: Literal["hello", "world"] | bool | ||
/// ``` | ||
/// | ||
/// ## Fix safety | ||
/// The fix for this rule is marked as unsafe, as it may change the semantics of the code. | ||
/// Specifically: | ||
/// | ||
/// - Type checkers may not treat `bool` as equivalent when overloading boolean arguments | ||
/// with `Literal[True]` and `Literal[False]` (see, e.g., [#14764] and [#5421]). | ||
/// - `bool` is not strictly equivalent to `Literal[True, False]`, as `bool` is | ||
/// a subclass of `int`, and this rule may not apply if the type annotations are used | ||
/// in a numeric context. | ||
/// | ||
/// Further, the `Literal` slice may contain trailing-line comments which the fix would remove. | ||
/// | ||
/// ## References | ||
/// - [Typing documentation: Legal parameters for `Literal` at type check time](https://typing.readthedocs.io/en/latest/spec/literal.html#legal-parameters-for-literal-at-type-check-time) | ||
/// - [Python documentation: Boolean type - `bool`](https://docs.python.org/3/library/stdtypes.html#boolean-type-bool) | ||
/// | ||
/// [mypy]: https://github.com/python/mypy/blob/master/mypy/typeops.py#L985 | ||
/// [#14764]: https://github.com/python/mypy/issues/14764 | ||
/// [#5421]: https://github.com/microsoft/pyright/issues/5421 | ||
#[violation] | ||
pub struct RedundantBoolLiteral { | ||
seen_others: bool, | ||
} | ||
|
||
impl Violation for RedundantBoolLiteral { | ||
const FIX_AVAILABILITY: FixAvailability = FixAvailability::Sometimes; | ||
|
||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
if self.seen_others { | ||
"`Literal[True, False, ...]` can be replaced with `Literal[...] | bool`".to_string() | ||
} else { | ||
"`Literal[True, False]` can be replaced with `bool`".to_string() | ||
} | ||
} | ||
|
||
fn fix_title(&self) -> Option<String> { | ||
Some(if self.seen_others { | ||
"Replace with `Literal[...] | bool`".to_string() | ||
} else { | ||
"Replace with `bool`".to_string() | ||
}) | ||
} | ||
} | ||
|
||
/// RUF038 | ||
pub(crate) fn redundant_bool_literal<'a>(checker: &mut Checker, literal_expr: &'a Expr) { | ||
if !checker.semantic().seen_typing() { | ||
return; | ||
} | ||
|
||
let mut seen_expr = BooleanLiteral::empty(); | ||
|
||
let mut find_bools = |expr: &'a Expr, _parent: &'a Expr| { | ||
let expr_type = match expr { | ||
Expr::BooleanLiteral(boolean_expr) => { | ||
if boolean_expr.value { | ||
BooleanLiteral::TRUE | ||
} else { | ||
BooleanLiteral::FALSE | ||
} | ||
} | ||
_ => BooleanLiteral::OTHER, | ||
}; | ||
seen_expr.insert(expr_type); | ||
}; | ||
|
||
traverse_literal(&mut find_bools, checker.semantic(), literal_expr); | ||
|
||
if !seen_expr.contains(BooleanLiteral::TRUE | BooleanLiteral::FALSE) { | ||
return; | ||
} | ||
|
||
let seen_others = seen_expr.contains(BooleanLiteral::OTHER); | ||
|
||
let mut diagnostic = | ||
Diagnostic::new(RedundantBoolLiteral { seen_others }, literal_expr.range()); | ||
|
||
// Provide a [`Fix`] when the complete `Literal` can be replaced. Applying the fix | ||
// can leave an unused import to be fixed by the `unused-import` rule. | ||
if !seen_others { | ||
if checker.semantic().has_builtin_binding("bool") { | ||
diagnostic.set_fix(Fix::unsafe_edit(Edit::range_replacement( | ||
"bool".to_string(), | ||
literal_expr.range(), | ||
))); | ||
} | ||
} | ||
|
||
checker.diagnostics.push(diagnostic); | ||
} | ||
|
||
bitflags! { | ||
#[derive(Default, Debug)] | ||
struct BooleanLiteral: u8 { | ||
const TRUE = 1 << 0; | ||
const FALSE = 1 << 1; | ||
const OTHER = 1 << 2; | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
...ff_linter/src/rules/ruff/snapshots/ruff_linter__rules__ruff__tests__RUF038_RUF038.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/ruff/mod.rs | ||
--- | ||
RUF038.py:4:17: RUF038 [*] `Literal[True, False]` can be replaced with `bool` | ||
| | ||
4 | def func1(arg1: Literal[True, False]): | ||
| ^^^^^^^^^^^^^^^^^^^^ RUF038 | ||
5 | ... | ||
| | ||
= help: Replace with `bool` | ||
|
||
ℹ Unsafe fix | ||
1 1 | from typing import Literal | ||
2 2 | | ||
3 3 | | ||
4 |-def func1(arg1: Literal[True, False]): | ||
4 |+def func1(arg1: bool): | ||
5 5 | ... | ||
6 6 | | ||
7 7 | | ||
|
||
RUF038.py:8:17: RUF038 [*] `Literal[True, False]` can be replaced with `bool` | ||
| | ||
8 | def func2(arg1: Literal[True, False, True]): | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ RUF038 | ||
9 | ... | ||
| | ||
= help: Replace with `bool` | ||
|
||
ℹ Unsafe fix | ||
5 5 | ... | ||
6 6 | | ||
7 7 | | ||
8 |-def func2(arg1: Literal[True, False, True]): | ||
8 |+def func2(arg1: bool): | ||
9 9 | ... | ||
10 10 | | ||
11 11 | | ||
|
||
RUF038.py:12:16: RUF038 [*] `Literal[True, False]` can be replaced with `bool` | ||
| | ||
12 | def func3() -> Literal[True, False]: | ||
| ^^^^^^^^^^^^^^^^^^^^ RUF038 | ||
13 | ... | ||
| | ||
= help: Replace with `bool` | ||
|
||
ℹ Unsafe fix | ||
9 9 | ... | ||
10 10 | | ||
11 11 | | ||
12 |-def func3() -> Literal[True, False]: | ||
12 |+def func3() -> bool: | ||
13 13 | ... | ||
14 14 | | ||
15 15 | | ||
|
||
RUF038.py:16:17: RUF038 [*] `Literal[True, False]` can be replaced with `bool` | ||
| | ||
16 | def func4(arg1: Literal[True, False] | bool): | ||
| ^^^^^^^^^^^^^^^^^^^^ RUF038 | ||
17 | ... | ||
| | ||
= help: Replace with `bool` | ||
|
||
ℹ Unsafe fix | ||
13 13 | ... | ||
14 14 | | ||
15 15 | | ||
16 |-def func4(arg1: Literal[True, False] | bool): | ||
16 |+def func4(arg1: bool | bool): | ||
17 17 | ... | ||
18 18 | | ||
19 19 | | ||
|
||
RUF038.py:20:17: RUF038 [*] `Literal[True, False]` can be replaced with `bool` | ||
| | ||
20 | def func5(arg1: Literal[False, True]): | ||
| ^^^^^^^^^^^^^^^^^^^^ RUF038 | ||
21 | ... | ||
| | ||
= help: Replace with `bool` | ||
|
||
ℹ Unsafe fix | ||
17 17 | ... | ||
18 18 | | ||
19 19 | | ||
20 |-def func5(arg1: Literal[False, True]): | ||
20 |+def func5(arg1: bool): | ||
21 21 | ... | ||
22 22 | | ||
23 23 | | ||
|
||
RUF038.py:24:17: RUF038 `Literal[True, False, ...]` can be replaced with `Literal[...] | bool` | ||
| | ||
24 | def func6(arg1: Literal[True, False, "hello", "world"]): | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RUF038 | ||
25 | ... | ||
| | ||
= help: Replace with `Literal[...] | bool` |
Oops, something went wrong.