File tree Expand file tree Collapse file tree 3 files changed +66
-43
lines changed
resources/test/fixtures/pylint Expand file tree Collapse file tree 3 files changed +66
-43
lines changed Original file line number Diff line number Diff line change 1
- class Str :
2
- def __str__ (self ):
3
- return 1
1
+ # These testcases should raise errors
4
2
5
3
class Float :
6
4
def __str__ (self ):
7
5
return 3.05
8
-
6
+
9
7
class Int :
8
+ def __str__ (self ):
9
+ return 1
10
+
11
+ class Int2 :
10
12
def __str__ (self ):
11
13
return 0
12
-
14
+
13
15
class Bool :
14
16
def __str__ (self ):
15
17
return False
16
-
17
- class Str2 :
18
- def __str__ (self ):
19
- x = "ruff"
20
- return x
21
-
22
- # TODO fixme once Ruff has better type checking
18
+
19
+ # TODO: Once Ruff has better type checking
23
20
def return_int ():
24
21
return 3
25
22
26
23
class ComplexReturn :
27
24
def __str__ (self ):
28
- return return_int ()
25
+ return return_int ()
26
+
27
+ # These testcases should NOT raise errors
28
+
29
+ class Str :
30
+ def __str__ (self ):
31
+ return "ruff"
32
+
33
+ class Str2 :
34
+ def __str__ (self ):
35
+ x = "ruff"
36
+ return x
Original file line number Diff line number Diff line change @@ -14,6 +14,23 @@ use crate::checkers::ast::Checker;
14
14
/// ## Why is this bad?
15
15
/// The `__str__` method should return a `str` object. Returning a different
16
16
/// type may cause unexpected behavior.
17
+ ///
18
+ /// ## Example
19
+ /// ```python
20
+ /// class Foo:
21
+ /// def __str__(self):
22
+ /// return True
23
+ /// ```
24
+ ///
25
+ /// Use instead:
26
+ /// ```python
27
+ /// class Foo:
28
+ /// def __str__(self):
29
+ /// return "Foo"
30
+ /// ```
31
+ ///
32
+ /// ## References
33
+ /// - [Python documentation: The `__str__` method](https://docs.python.org/3/reference/datamodel.html#object.__str__)
17
34
#[ violation]
18
35
pub struct InvalidStrReturnType ;
19
36
Original file line number Diff line number Diff line change 1
1
-- -
2
2
source : crates / ruff_linter / src / rules / pylint / mod .rs
3
3
-- -
4
- invalid_return_type_str .py :3 :16 : PLE0307 ` __str__` does not return ` str`
4
+ invalid_return_type_str .py :5 :16 : PLE0307 ` __str__` does not return ` str`
5
5
|
6
- 1 | class Str :
7
- 2 | def __str__ (self ):
8
- 3 | return 1
9
- | ^ PLE0307
10
- 4 |
11
- 5 | class Float :
12
- |
13
-
14
- invalid_return_type_str .py :7:16: PLE0307 `__str__ ` does not return `str `
15
- |
16
- 5 | class Float :
17
- 6 | def __str__ (self ):
18
- 7 | return 3.05
6
+ 3 | class Float :
7
+ 4 | def __str__ (self ):
8
+ 5 | return 3.05
19
9
| ^^^^ PLE0307
20
- 8 |
21
- 9 | class Int :
10
+ 6 |
11
+ 7 | class Int :
22
12
|
23
13
24
- invalid_return_type_str .py :11 :16: PLE0307 `__str__ ` does not return `str `
14
+ invalid_return_type_str .py :9 :16: PLE0307 `__str__ ` does not return `str `
25
15
|
26
- 9 | class Int :
27
- 10 | def __str__ (self ):
28
- 11 | return 0
16
+ 7 | class Int :
17
+ 8 | def __str__ (self ):
18
+ 9 | return 1
29
19
| ^ PLE0307
30
- 12 |
31
- 13 | class Bool :
20
+ 10 |
21
+ 11 | class Int2 :
32
22
|
33
23
34
- invalid_return_type_str .py :15 :16: PLE0307 `__str__ ` does not return `str `
24
+ invalid_return_type_str .py :13 :16: PLE0307 `__str__ ` does not return `str `
35
25
|
36
- 13 | class Bool :
37
- 14 | def __str__ (self ):
38
- 15 | return False
39
- | ^^^^^ PLE0307
40
- 16 |
41
- 17 | class Str2 :
26
+ 11 | class Int2 :
27
+ 12 | def __str__ (self ):
28
+ 13 | return 0
29
+ | ^ PLE0307
30
+ 14 |
31
+ 15 | class Bool :
42
32
|
43
33
44
-
34
+ invalid_return_type_str .py :17:16: PLE0307 `__str__ ` does not return `str `
35
+ |
36
+ 15 | class Bool :
37
+ 16 | def __str__ (self ):
38
+ 17 | return False
39
+ | ^^^^^ PLE0307
40
+ 18 |
41
+ 19 | # TODO : Once Ruff has better type checking
42
+ |
You can’t perform that action at this time.
0 commit comments