Skip to content

Commit f7802ad

Browse files
[pylint] Extend docs and test in invalid-str-return-type (E307) (#10400)
## Summary Added some docs, and a little of test cases in `invalid-str-return-type`, mentioned in #10377 (review) ## Test Plan On `invalid_return_type_str.py`. --------- Co-authored-by: Dhruv Manilawala <dhruvmanila@gmail.com>
1 parent e832327 commit f7802ad

File tree

3 files changed

+66
-43
lines changed

3 files changed

+66
-43
lines changed
Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,36 @@
1-
class Str:
2-
def __str__(self):
3-
return 1
1+
# These testcases should raise errors
42

53
class Float:
64
def __str__(self):
75
return 3.05
8-
6+
97
class Int:
8+
def __str__(self):
9+
return 1
10+
11+
class Int2:
1012
def __str__(self):
1113
return 0
12-
14+
1315
class Bool:
1416
def __str__(self):
1517
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
2320
def return_int():
2421
return 3
2522

2623
class ComplexReturn:
2724
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

crates/ruff_linter/src/rules/pylint/rules/invalid_str_return.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ use crate::checkers::ast::Checker;
1414
/// ## Why is this bad?
1515
/// The `__str__` method should return a `str` object. Returning a different
1616
/// 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__)
1734
#[violation]
1835
pub struct InvalidStrReturnType;
1936

Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
11
---
22
source: crates/ruff_linter/src/rules/pylint/mod.rs
33
---
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`
55
|
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
199
| ^^^^ PLE0307
20-
8 |
21-
9 | class Int:
10+
6 |
11+
7 | class Int:
2212
|
2313

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`
2515
|
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
2919
| ^ PLE0307
30-
12 |
31-
13 | class Bool:
20+
10 |
21+
11 | class Int2:
3222
|
3323

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`
3525
|
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:
4232
|
4333

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+
|

0 commit comments

Comments
 (0)