-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Summary Implement `E304` in the issue #970. Throws an error when the returning value of `__bool__` method is not boolean. Reference: https://pylint.readthedocs.io/en/stable/user_guide/messages/error/invalid-bool-returned.html ## Test Plan Add test cases and run `cargo test`
- Loading branch information
1 parent
32d6f84
commit c269c1a
Showing
8 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
crates/ruff_linter/resources/test/fixtures/pylint/invalid_return_type_bool.py
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,37 @@ | ||
# These testcases should raise errors | ||
|
||
class Float: | ||
def __bool__(self): | ||
return 3.05 # [invalid-bool-return] | ||
|
||
class Int: | ||
def __bool__(self): | ||
return 0 # [invalid-bool-return] | ||
|
||
|
||
class Str: | ||
def __bool__(self): | ||
x = "ruff" | ||
return x # [invalid-bool-return] | ||
|
||
# TODO: Once Ruff has better type checking | ||
def return_int(): | ||
return 3 | ||
|
||
class ComplexReturn: | ||
def __bool__(self): | ||
return return_int() # [invalid-bool-return] | ||
|
||
|
||
|
||
# These testcases should NOT raise errors | ||
|
||
class Bool: | ||
def __bool__(self): | ||
return True | ||
|
||
|
||
class Bool2: | ||
def __bool__(self): | ||
x = True | ||
return x |
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
78 changes: 78 additions & 0 deletions
78
crates/ruff_linter/src/rules/pylint/rules/invalid_bool_return.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,78 @@ | ||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::helpers::ReturnStatementVisitor; | ||
use ruff_python_ast::visitor::Visitor; | ||
use ruff_python_ast::Stmt; | ||
use ruff_python_semantic::analyze::type_inference::{NumberLike, PythonType, ResolvedPythonType}; | ||
use ruff_text_size::Ranged; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for `__bool__` implementations that return a type other than `bool`. | ||
/// | ||
/// ## Why is this bad? | ||
/// The `__bool__` method should return a `bool` object. Returning a different | ||
/// type may cause unexpected behavior. | ||
/// | ||
/// ## Example | ||
/// ```python | ||
/// class Foo: | ||
/// def __bool__(self): | ||
/// return 2 | ||
/// ``` | ||
/// | ||
/// Use instead: | ||
/// ```python | ||
/// class Foo: | ||
/// def __bool__(self): | ||
/// return True | ||
/// ``` | ||
/// | ||
/// ## References | ||
/// - [Python documentation: The `__bool__` method](https://docs.python.org/3/reference/datamodel.html#object.__bool__) | ||
#[violation] | ||
pub struct InvalidBoolReturnType; | ||
|
||
impl Violation for InvalidBoolReturnType { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("`__bool__` does not return `bool`") | ||
} | ||
} | ||
|
||
/// E0307 | ||
pub(crate) fn invalid_bool_return(checker: &mut Checker, name: &str, body: &[Stmt]) { | ||
if name != "__bool__" { | ||
return; | ||
} | ||
|
||
if !checker.semantic().current_scope().kind.is_class() { | ||
return; | ||
} | ||
|
||
let returns = { | ||
let mut visitor = ReturnStatementVisitor::default(); | ||
visitor.visit_body(body); | ||
visitor.returns | ||
}; | ||
|
||
for stmt in returns { | ||
if let Some(value) = stmt.value.as_deref() { | ||
if !matches!( | ||
ResolvedPythonType::from(value), | ||
ResolvedPythonType::Unknown | ||
| ResolvedPythonType::Atom(PythonType::Number(NumberLike::Bool)) | ||
) { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(InvalidBoolReturnType, value.range())); | ||
} | ||
} else { | ||
// Disallow implicit `None`. | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(InvalidBoolReturnType, stmt.range())); | ||
} | ||
} | ||
} |
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
20 changes: 20 additions & 0 deletions
20
...int/snapshots/ruff_linter__rules__pylint__tests__PLE0304_invalid_return_type_bool.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,20 @@ | ||
--- | ||
source: crates/ruff_linter/src/rules/pylint/mod.rs | ||
--- | ||
invalid_return_type_bool.py:5:16: PLE0304 `__bool__` does not return `bool` | ||
| | ||
3 | class Float: | ||
4 | def __bool__(self): | ||
5 | return 3.05 # [invalid-bool-return] | ||
| ^^^^ PLE0304 | ||
6 | | ||
7 | class Int: | ||
| | ||
|
||
invalid_return_type_bool.py:9:16: PLE0304 `__bool__` does not return `bool` | ||
| | ||
7 | class Int: | ||
8 | def __bool__(self): | ||
9 | return 0 # [invalid-bool-return] | ||
| ^ PLE0304 | ||
| |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.