Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement E307 for pylint invalid str return type (#4854)
- Loading branch information
1 parent
e6b00f0
commit 7224596
Showing
10 changed files
with
166 additions
and
11 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
crates/ruff/resources/test/fixtures/pylint/invalid_return_type_str.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,28 @@ | ||
class Str: | ||
def __str__(self): | ||
return 1 | ||
|
||
class Float: | ||
def __str__(self): | ||
return 3.05 | ||
|
||
class Int: | ||
def __str__(self): | ||
return 0 | ||
|
||
class Bool: | ||
def __str__(self): | ||
return False | ||
|
||
class Str2: | ||
def __str__(self): | ||
x = "ruff" | ||
return x | ||
|
||
# TODO fixme once Ruff has better type checking | ||
def return_int(): | ||
return 3 | ||
|
||
class ComplexReturn: | ||
def __str__(self): | ||
return return_int() |
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
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,75 @@ | ||
use rustpython_parser::ast::{self, Constant, Expr, Ranged, Stmt}; | ||
|
||
use ruff_diagnostics::{Diagnostic, Violation}; | ||
use ruff_macros::{derive_message_formats, violation}; | ||
use ruff_python_ast::{helpers::ReturnStatementVisitor, statement_visitor::StatementVisitor}; | ||
|
||
use crate::checkers::ast::Checker; | ||
|
||
/// ## What it does | ||
/// Checks for `__str__` implementations that return a type other than `str`. | ||
/// | ||
/// ## Why is this bad? | ||
/// The `__str__` method should return a `str` object. Returning a different | ||
/// type may cause unexpected behavior. | ||
#[violation] | ||
pub struct InvalidStrReturnType; | ||
|
||
impl Violation for InvalidStrReturnType { | ||
#[derive_message_formats] | ||
fn message(&self) -> String { | ||
format!("`__str__` does not return `str`") | ||
} | ||
} | ||
|
||
/// E0307 | ||
pub(crate) fn invalid_str_return(checker: &mut Checker, name: &str, body: &[Stmt]) { | ||
if name != "__str__" { | ||
return; | ||
} | ||
|
||
if !checker.semantic_model().scope().kind.is_class() { | ||
return; | ||
} | ||
|
||
let returns = { | ||
let mut visitor = ReturnStatementVisitor::default(); | ||
visitor.visit_body(body); | ||
visitor.returns | ||
}; | ||
|
||
for stmt in returns { | ||
// Disallow implicit `None`. | ||
let Some(value) = stmt.value.as_deref() else { | ||
checker.diagnostics.push(Diagnostic::new(InvalidStrReturnType, stmt.range())); | ||
continue; | ||
}; | ||
|
||
// Disallow other constants. | ||
if matches!( | ||
value, | ||
Expr::List(_) | ||
| Expr::Dict(_) | ||
| Expr::Set(_) | ||
| Expr::ListComp(_) | ||
| Expr::DictComp(_) | ||
| Expr::SetComp(_) | ||
| Expr::GeneratorExp(_) | ||
| Expr::Constant(ast::ExprConstant { | ||
value: Constant::None | ||
| Constant::Bool(_) | ||
| Constant::Bytes(_) | ||
| Constant::Int(_) | ||
| Constant::Tuple(_) | ||
| Constant::Float(_) | ||
| Constant::Complex { .. } | ||
| Constant::Ellipsis, | ||
.. | ||
}) | ||
) { | ||
checker | ||
.diagnostics | ||
.push(Diagnostic::new(InvalidStrReturnType, value.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
44 changes: 44 additions & 0 deletions
44
...ules/pylint/snapshots/ruff__rules__pylint__tests__PLE0307_invalid_return_type_str.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,44 @@ | ||
--- | ||
source: crates/ruff/src/rules/pylint/mod.rs | ||
--- | ||
invalid_return_type_str.py:3:16: PLE0307 `__str__` does not return `str` | ||
| | ||
3 | class Str: | ||
4 | def __str__(self): | ||
5 | return 1 | ||
| ^ PLE0307 | ||
6 | | ||
7 | class Float: | ||
| | ||
|
||
invalid_return_type_str.py:7:16: PLE0307 `__str__` does not return `str` | ||
| | ||
7 | class Float: | ||
8 | def __str__(self): | ||
9 | return 3.05 | ||
| ^^^^ PLE0307 | ||
10 | | ||
11 | class Int: | ||
| | ||
|
||
invalid_return_type_str.py:11:16: PLE0307 `__str__` does not return `str` | ||
| | ||
11 | class Int: | ||
12 | def __str__(self): | ||
13 | return 0 | ||
| ^ PLE0307 | ||
14 | | ||
15 | class Bool: | ||
| | ||
|
||
invalid_return_type_str.py:15:16: PLE0307 `__str__` does not return `str` | ||
| | ||
15 | class Bool: | ||
16 | def __str__(self): | ||
17 | return False | ||
| ^^^^^ PLE0307 | ||
18 | | ||
19 | class Str2: | ||
| | ||
|
||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.