Skip to content

Commit

Permalink
due to ruff type checking constraints, do a best effort for E0307 for…
Browse files Browse the repository at this point in the history
… checking return results of __str__ of constants
  • Loading branch information
Ryang20718 committed Jun 5, 2023
1 parent 78eaefb commit 339a382
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
@@ -1,5 +1,28 @@
class Str:
"""__str__ returns int"""
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 1
return return_int()
4 changes: 2 additions & 2 deletions crates/ruff/src/rules/pylint/rules/invalid_str_return.rs
Expand Up @@ -29,9 +29,9 @@ fn is_str_returning(body: &[Stmt]) -> Option<Diagnostic> {
let mut visitor = ReturnStatementVisitor::default();
visitor.visit_body(body);
for expr in visitor.returns.into_iter().flatten() {
if !matches!(
if matches!(
expr,
Expr::Constant(ref constant) if constant.value.is_str()
Expr::Constant(ref constant) if constant.value.is_bool() || constant.value.is_int() || constant.value.is_tuple() || constant.value.is_float()
) {
return Some(Diagnostic::new(InvalidStrReturnType, expr.range()));
}
Expand Down
@@ -1,11 +1,44 @@
---
source: crates/ruff/src/rules/pylint/mod.rs
---
invalid_return_type_str.py:5:16: PLE0307 __str__ does not return str
invalid_return_type_str.py:3:16: PLE0307 __str__ does not return str
|
5 | def __str__(self):
6 | return 1
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:
|


0 comments on commit 339a382

Please sign in to comment.