Skip to content

Commit 9a54ee3

Browse files
committed
red_knot_python_semantic: add snapshot tests for unsupported boolean conversions
This just captures the status quo before we try to improve them.
1 parent 25c3be5 commit 9a54ee3

6 files changed

+216
-6
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<!-- snapshot-diagnostics -->
2+
3+
# Different ways that `UNSUPPORTED_BOOL_CONVERSION` can occur
4+
5+
## Has a `__bool__` method, but has incorrect parameters
6+
7+
```py
8+
class NotBoolable:
9+
def __bool__(self, foo):
10+
return False
11+
12+
a = NotBoolable()
13+
14+
# error: [unsupported-bool-conversion]
15+
10 and a and True
16+
```
17+
18+
## Has a `__bool__` method, but has an incorrect return type
19+
20+
```py
21+
class NotBoolable:
22+
def __bool__(self) -> str:
23+
return "wat"
24+
25+
a = NotBoolable()
26+
27+
# error: [unsupported-bool-conversion]
28+
10 and a and True
29+
```
30+
31+
## Has a `__bool__` attribute, but it's not callable
32+
33+
```py
34+
class NotBoolable:
35+
__bool__: int = 3
36+
37+
a = NotBoolable()
38+
39+
# error: [unsupported-bool-conversion]
40+
10 and a and True
41+
```
42+
43+
## Part of a union where at least one member has incorrect `__bool__` method
44+
45+
```py
46+
class NotBoolable1:
47+
def __bool__(self) -> str:
48+
return "wat"
49+
50+
class NotBoolable2:
51+
pass
52+
53+
class NotBoolable3:
54+
__bool__: int = 3
55+
56+
def get() -> NotBoolable1 | NotBoolable2 | NotBoolable3:
57+
return NotBoolable2()
58+
59+
# error: [unsupported-bool-conversion]
60+
10 and get() and True
61+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
source: crates/red_knot_test/src/lib.rs
3+
expression: snapshot
4+
---
5+
---
6+
mdtest name: unsupported_bool_conversion.md - Different ways that `UNSUPPORTED_BOOL_CONVERSION` can occur - Has a `__bool__` attribute, but it's not callable
7+
mdtest path: crates/red_knot_python_semantic/resources/mdtest/diagnostics/unsupported_bool_conversion.md
8+
---
9+
10+
# Python source files
11+
12+
## mdtest_snippet.py
13+
14+
```
15+
1 | class NotBoolable:
16+
2 | __bool__: int = 3
17+
3 |
18+
4 | a = NotBoolable()
19+
5 |
20+
6 | # error: [unsupported-bool-conversion]
21+
7 | 10 and a and True
22+
```
23+
24+
# Diagnostics
25+
26+
```
27+
error: lint:unsupported-bool-conversion: Boolean conversion is unsupported for type `NotBoolable`; its `__bool__` method isn't callable
28+
--> /src/mdtest_snippet.py:7:8
29+
|
30+
6 | # error: [unsupported-bool-conversion]
31+
7 | 10 and a and True
32+
| ^
33+
|
34+
35+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
source: crates/red_knot_test/src/lib.rs
3+
expression: snapshot
4+
---
5+
---
6+
mdtest name: unsupported_bool_conversion.md - Different ways that `UNSUPPORTED_BOOL_CONVERSION` can occur - Has a `__bool__` method, but has an incorrect return type
7+
mdtest path: crates/red_knot_python_semantic/resources/mdtest/diagnostics/unsupported_bool_conversion.md
8+
---
9+
10+
# Python source files
11+
12+
## mdtest_snippet.py
13+
14+
```
15+
1 | class NotBoolable:
16+
2 | def __bool__(self) -> str:
17+
3 | return "wat"
18+
4 |
19+
5 | a = NotBoolable()
20+
6 |
21+
7 | # error: [unsupported-bool-conversion]
22+
8 | 10 and a and True
23+
```
24+
25+
# Diagnostics
26+
27+
```
28+
error: lint:unsupported-bool-conversion: Boolean conversion is unsupported for type `NotBoolable`; the return type of its bool method (`str`) isn't assignable to `bool
29+
--> /src/mdtest_snippet.py:8:8
30+
|
31+
7 | # error: [unsupported-bool-conversion]
32+
8 | 10 and a and True
33+
| ^
34+
|
35+
36+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
source: crates/red_knot_test/src/lib.rs
3+
expression: snapshot
4+
---
5+
---
6+
mdtest name: unsupported_bool_conversion.md - Different ways that `UNSUPPORTED_BOOL_CONVERSION` can occur - Has a `__bool__` method, but has incorrect parameters
7+
mdtest path: crates/red_knot_python_semantic/resources/mdtest/diagnostics/unsupported_bool_conversion.md
8+
---
9+
10+
# Python source files
11+
12+
## mdtest_snippet.py
13+
14+
```
15+
1 | class NotBoolable:
16+
2 | def __bool__(self, foo):
17+
3 | return False
18+
4 |
19+
5 | a = NotBoolable()
20+
6 |
21+
7 | # error: [unsupported-bool-conversion]
22+
8 | 10 and a and True
23+
```
24+
25+
# Diagnostics
26+
27+
```
28+
error: lint:unsupported-bool-conversion: Boolean conversion is unsupported for type `NotBoolable`; it incorrectly implements `__bool__`
29+
--> /src/mdtest_snippet.py:8:8
30+
|
31+
7 | # error: [unsupported-bool-conversion]
32+
8 | 10 and a and True
33+
| ^
34+
|
35+
36+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
source: crates/red_knot_test/src/lib.rs
3+
expression: snapshot
4+
---
5+
---
6+
mdtest name: unsupported_bool_conversion.md - Different ways that `UNSUPPORTED_BOOL_CONVERSION` can occur - Part of a union where at least one member has incorrect `__bool__` method
7+
mdtest path: crates/red_knot_python_semantic/resources/mdtest/diagnostics/unsupported_bool_conversion.md
8+
---
9+
10+
# Python source files
11+
12+
## mdtest_snippet.py
13+
14+
```
15+
1 | class NotBoolable1:
16+
2 | def __bool__(self) -> str:
17+
3 | return "wat"
18+
4 |
19+
5 | class NotBoolable2:
20+
6 | pass
21+
7 |
22+
8 | class NotBoolable3:
23+
9 | __bool__: int = 3
24+
10 |
25+
11 | def get() -> NotBoolable1 | NotBoolable2 | NotBoolable3:
26+
12 | return NotBoolable2()
27+
13 |
28+
14 | # error: [unsupported-bool-conversion]
29+
15 | 10 and get() and True
30+
```
31+
32+
# Diagnostics
33+
34+
```
35+
error: lint:unsupported-bool-conversion: Boolean conversion is unsupported for union `NotBoolable1 | NotBoolable2 | NotBoolable3` because `NotBoolable1` doesn't implement `__bool__` correctly
36+
--> /src/mdtest_snippet.py:15:8
37+
|
38+
14 | # error: [unsupported-bool-conversion]
39+
15 | 10 and get() and True
40+
| ^^^^^
41+
|
42+
43+
```

crates/red_knot_python_semantic/src/types.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -5670,12 +5670,11 @@ impl<'db> BoolError<'db> {
56705670
Self::IncorrectArguments {
56715671
not_boolable_type, ..
56725672
} => {
5673-
builder.into_diagnostic(
5674-
format_args!(
5675-
"Boolean conversion is unsupported for type `{}`; it incorrectly implements `__bool__`",
5676-
not_boolable_type.display(context.db())
5677-
),
5678-
);
5673+
builder.into_diagnostic(format_args!(
5674+
"Boolean conversion is unsupported for type `{}`; \
5675+
it incorrectly implements `__bool__`",
5676+
not_boolable_type.display(context.db())
5677+
));
56795678
}
56805679
Self::IncorrectReturnType {
56815680
not_boolable_type,

0 commit comments

Comments
 (0)