red_knot_python_semantic: update revealed type snapshots

This required a bit of surgery in the diagnostic matching and more
faffing about using a "concise" message from a diagnostic instead of
only printing the "primary" message.
This commit is contained in:
Andrew Gallant 2025-04-08 13:28:15 -04:00 committed by Andrew Gallant
parent 28b64064f5
commit ba408f4231
16 changed files with 75 additions and 77 deletions

View file

@ -40,13 +40,13 @@ error: lint:not-iterable
``` ```
``` ```
info: revealed-type info: revealed-type: Revealed type
--> /src/mdtest_snippet.py:11:5 --> /src/mdtest_snippet.py:11:5
| |
9 | # error: [not-iterable] 9 | # error: [not-iterable]
10 | for x in Iterable(): 10 | for x in Iterable():
11 | reveal_type(x) # revealed: int 11 | reveal_type(x) # revealed: int
| ^^^^^^^^^^^^^^ Revealed type is `int` | ^^^^^^^^^^^^^^ `int`
| |
``` ```

View file

@ -37,13 +37,13 @@ error: lint:not-iterable
``` ```
``` ```
info: revealed-type info: revealed-type: Revealed type
--> /src/mdtest_snippet.py:8:5 --> /src/mdtest_snippet.py:8:5
| |
6 | # error: [not-iterable] 6 | # error: [not-iterable]
7 | for x in Bad(): 7 | for x in Bad():
8 | reveal_type(x) # revealed: Unknown 8 | reveal_type(x) # revealed: Unknown
| ^^^^^^^^^^^^^^ Revealed type is `Unknown` | ^^^^^^^^^^^^^^ `Unknown`
| |
``` ```

View file

@ -59,13 +59,13 @@ error: lint:not-iterable
``` ```
``` ```
info: revealed-type info: revealed-type: Revealed type
--> /src/mdtest_snippet.py:24:9 --> /src/mdtest_snippet.py:24:9
| |
22 | for x in Iterable1(): 22 | for x in Iterable1():
23 | # TODO... `int` might be ideal here? 23 | # TODO... `int` might be ideal here?
24 | reveal_type(x) # revealed: int | Unknown 24 | reveal_type(x) # revealed: int | Unknown
| ^^^^^^^^^^^^^^ Revealed type is `int | Unknown` | ^^^^^^^^^^^^^^ `int | Unknown`
25 | 25 |
26 | # error: [not-iterable] 26 | # error: [not-iterable]
| |
@ -86,13 +86,13 @@ error: lint:not-iterable
``` ```
``` ```
info: revealed-type info: revealed-type: Revealed type
--> /src/mdtest_snippet.py:29:9 --> /src/mdtest_snippet.py:29:9
| |
27 | for y in Iterable2(): 27 | for y in Iterable2():
28 | # TODO... `int` might be ideal here? 28 | # TODO... `int` might be ideal here?
29 | reveal_type(y) # revealed: int | Unknown 29 | reveal_type(y) # revealed: int | Unknown
| ^^^^^^^^^^^^^^ Revealed type is `int | Unknown` | ^^^^^^^^^^^^^^ `int | Unknown`
| |
``` ```

View file

@ -56,13 +56,13 @@ error: lint:not-iterable
``` ```
``` ```
info: revealed-type info: revealed-type: Revealed type
--> /src/mdtest_snippet.py:22:9 --> /src/mdtest_snippet.py:22:9
| |
20 | for x in Iterable1(): 20 | for x in Iterable1():
21 | # TODO: `str` might be better 21 | # TODO: `str` might be better
22 | reveal_type(x) # revealed: str | Unknown 22 | reveal_type(x) # revealed: str | Unknown
| ^^^^^^^^^^^^^^ Revealed type is `str | Unknown` | ^^^^^^^^^^^^^^ `str | Unknown`
23 | 23 |
24 | # error: [not-iterable] 24 | # error: [not-iterable]
| |
@ -82,13 +82,13 @@ error: lint:not-iterable
``` ```
``` ```
info: revealed-type info: revealed-type: Revealed type
--> /src/mdtest_snippet.py:26:9 --> /src/mdtest_snippet.py:26:9
| |
24 | # error: [not-iterable] 24 | # error: [not-iterable]
25 | for y in Iterable2(): 25 | for y in Iterable2():
26 | reveal_type(y) # revealed: str | int 26 | reveal_type(y) # revealed: str | int
| ^^^^^^^^^^^^^^ Revealed type is `str | int` | ^^^^^^^^^^^^^^ `str | int`
| |
``` ```

View file

@ -59,13 +59,13 @@ error: lint:not-iterable
``` ```
``` ```
info: revealed-type info: revealed-type: Revealed type
--> /src/mdtest_snippet.py:18:9 --> /src/mdtest_snippet.py:18:9
| |
16 | # error: [not-iterable] 16 | # error: [not-iterable]
17 | for x in Iterable1(): 17 | for x in Iterable1():
18 | reveal_type(x) # revealed: int 18 | reveal_type(x) # revealed: int
| ^^^^^^^^^^^^^^ Revealed type is `int` | ^^^^^^^^^^^^^^ `int`
19 | 19 |
20 | class Iterable2: 20 | class Iterable2:
| |
@ -86,13 +86,13 @@ error: lint:not-iterable
``` ```
``` ```
info: revealed-type info: revealed-type: Revealed type
--> /src/mdtest_snippet.py:30:9 --> /src/mdtest_snippet.py:30:9
| |
28 | for x in Iterable2(): 28 | for x in Iterable2():
29 | # TODO: `int` would probably be better here: 29 | # TODO: `int` would probably be better here:
30 | reveal_type(x) # revealed: int | Unknown 30 | reveal_type(x) # revealed: int | Unknown
| ^^^^^^^^^^^^^^ Revealed type is `int | Unknown` | ^^^^^^^^^^^^^^ `int | Unknown`
| |
``` ```

View file

@ -63,13 +63,13 @@ error: lint:not-iterable
``` ```
``` ```
info: revealed-type info: revealed-type: Revealed type
--> /src/mdtest_snippet.py:29:9 --> /src/mdtest_snippet.py:29:9
| |
27 | # error: [not-iterable] 27 | # error: [not-iterable]
28 | for x in Iterable1(): 28 | for x in Iterable1():
29 | reveal_type(x) # revealed: int | str 29 | reveal_type(x) # revealed: int | str
| ^^^^^^^^^^^^^^ Revealed type is `int | str` | ^^^^^^^^^^^^^^ `int | str`
30 | 30 |
31 | # error: [not-iterable] 31 | # error: [not-iterable]
| |
@ -90,13 +90,13 @@ error: lint:not-iterable
``` ```
``` ```
info: revealed-type info: revealed-type: Revealed type
--> /src/mdtest_snippet.py:34:9 --> /src/mdtest_snippet.py:34:9
| |
32 | for y in Iterable2(): 32 | for y in Iterable2():
33 | # TODO: `int` would probably be better here: 33 | # TODO: `int` would probably be better here:
34 | reveal_type(y) # revealed: int | Unknown 34 | reveal_type(y) # revealed: int | Unknown
| ^^^^^^^^^^^^^^ Revealed type is `int | Unknown` | ^^^^^^^^^^^^^^ `int | Unknown`
| |
``` ```

View file

@ -48,13 +48,13 @@ error: lint:not-iterable
``` ```
``` ```
info: revealed-type info: revealed-type: Revealed type
--> /src/mdtest_snippet.py:19:9 --> /src/mdtest_snippet.py:19:9
| |
17 | # error: [not-iterable] 17 | # error: [not-iterable]
18 | for x in Iterable(): 18 | for x in Iterable():
19 | reveal_type(x) # revealed: int | bytes 19 | reveal_type(x) # revealed: int | bytes
| ^^^^^^^^^^^^^^ Revealed type is `int | bytes` | ^^^^^^^^^^^^^^ `int | bytes`
| |
``` ```

View file

@ -67,13 +67,13 @@ error: lint:not-iterable
``` ```
``` ```
info: revealed-type info: revealed-type: Revealed type
--> /src/mdtest_snippet.py:33:9 --> /src/mdtest_snippet.py:33:9
| |
31 | for x in Iterable1(): 31 | for x in Iterable1():
32 | # TODO: `bytes | str` might be better 32 | # TODO: `bytes | str` might be better
33 | reveal_type(x) # revealed: bytes | str | Unknown 33 | reveal_type(x) # revealed: bytes | str | Unknown
| ^^^^^^^^^^^^^^ Revealed type is `bytes | str | Unknown` | ^^^^^^^^^^^^^^ `bytes | str | Unknown`
34 | 34 |
35 | # error: [not-iterable] 35 | # error: [not-iterable]
| |
@ -93,13 +93,13 @@ error: lint:not-iterable
``` ```
``` ```
info: revealed-type info: revealed-type: Revealed type
--> /src/mdtest_snippet.py:37:9 --> /src/mdtest_snippet.py:37:9
| |
35 | # error: [not-iterable] 35 | # error: [not-iterable]
36 | for y in Iterable2(): 36 | for y in Iterable2():
37 | reveal_type(y) # revealed: bytes | str | int 37 | reveal_type(y) # revealed: bytes | str | int
| ^^^^^^^^^^^^^^ Revealed type is `bytes | str | int` | ^^^^^^^^^^^^^^ `bytes | str | int`
| |
``` ```

View file

@ -47,13 +47,13 @@ error: lint:not-iterable
``` ```
``` ```
info: revealed-type info: revealed-type: Revealed type
--> /src/mdtest_snippet.py:18:9 --> /src/mdtest_snippet.py:18:9
| |
16 | # error: [not-iterable] 16 | # error: [not-iterable]
17 | for x in Iterable(): 17 | for x in Iterable():
18 | reveal_type(x) # revealed: int | bytes 18 | reveal_type(x) # revealed: int | bytes
| ^^^^^^^^^^^^^^ Revealed type is `int | bytes` | ^^^^^^^^^^^^^^ `int | bytes`
| |
``` ```

View file

@ -49,13 +49,13 @@ error: lint:not-iterable
``` ```
``` ```
info: revealed-type info: revealed-type: Revealed type
--> /src/mdtest_snippet.py:19:9 --> /src/mdtest_snippet.py:19:9
| |
17 | # error: [not-iterable] 17 | # error: [not-iterable]
18 | for x in Test() if flag else Test2(): 18 | for x in Test() if flag else Test2():
19 | reveal_type(x) # revealed: int 19 | reveal_type(x) # revealed: int
| ^^^^^^^^^^^^^^ Revealed type is `int` | ^^^^^^^^^^^^^^ `int`
| |
``` ```

View file

@ -44,13 +44,13 @@ error: lint:not-iterable
``` ```
``` ```
info: revealed-type info: revealed-type: Revealed type
--> /src/mdtest_snippet.py:14:9 --> /src/mdtest_snippet.py:14:9
| |
12 | # error: [not-iterable] 12 | # error: [not-iterable]
13 | for x in Test() if flag else 42: 13 | for x in Test() if flag else 42:
14 | reveal_type(x) # revealed: int 14 | reveal_type(x) # revealed: int
| ^^^^^^^^^^^^^^ Revealed type is `int` | ^^^^^^^^^^^^^^ `int`
| |
``` ```

View file

@ -57,13 +57,13 @@ warning: lint:possibly-unresolved-reference
``` ```
``` ```
info: revealed-type info: revealed-type: Revealed type
--> /src/mdtest_snippet.py:16:5 --> /src/mdtest_snippet.py:16:5
| |
14 | # revealed: Unknown 14 | # revealed: Unknown
15 | # error: [possibly-unresolved-reference] 15 | # error: [possibly-unresolved-reference]
16 | reveal_type(x) 16 | reveal_type(x)
| ^^^^^^^^^^^^^^ Revealed type is `Unknown` | ^^^^^^^^^^^^^^ `Unknown`
| |
``` ```

View file

@ -38,13 +38,13 @@ error: lint:not-iterable
``` ```
``` ```
info: revealed-type info: revealed-type: Revealed type
--> /src/mdtest_snippet.py:9:5 --> /src/mdtest_snippet.py:9:5
| |
7 | # error: [not-iterable] 7 | # error: [not-iterable]
8 | for x in Bad(): 8 | for x in Bad():
9 | reveal_type(x) # revealed: Unknown 9 | reveal_type(x) # revealed: Unknown
| ^^^^^^^^^^^^^^ Revealed type is `Unknown` | ^^^^^^^^^^^^^^ `Unknown`
| |
``` ```

View file

@ -42,13 +42,13 @@ error: lint:not-iterable
``` ```
``` ```
info: revealed-type info: revealed-type: Revealed type
--> /src/mdtest_snippet.py:13:5 --> /src/mdtest_snippet.py:13:5
| |
11 | # error: [not-iterable] 11 | # error: [not-iterable]
12 | for x in Iterable(): 12 | for x in Iterable():
13 | reveal_type(x) # revealed: int 13 | reveal_type(x) # revealed: int
| ^^^^^^^^^^^^^^ Revealed type is `int` | ^^^^^^^^^^^^^^ `int`
| |
``` ```

View file

@ -53,13 +53,13 @@ error: lint:not-iterable
``` ```
``` ```
info: revealed-type info: revealed-type: Revealed type
--> /src/mdtest_snippet.py:20:5 --> /src/mdtest_snippet.py:20:5
| |
18 | # error: [not-iterable] 18 | # error: [not-iterable]
19 | for x in Iterable1(): 19 | for x in Iterable1():
20 | reveal_type(x) # revealed: int 20 | reveal_type(x) # revealed: int
| ^^^^^^^^^^^^^^ Revealed type is `int` | ^^^^^^^^^^^^^^ `int`
21 | 21 |
22 | # error: [not-iterable] 22 | # error: [not-iterable]
| |
@ -79,13 +79,13 @@ error: lint:not-iterable
``` ```
``` ```
info: revealed-type info: revealed-type: Revealed type
--> /src/mdtest_snippet.py:24:5 --> /src/mdtest_snippet.py:24:5
| |
22 | # error: [not-iterable] 22 | # error: [not-iterable]
23 | for y in Iterable2(): 23 | for y in Iterable2():
24 | reveal_type(y) # revealed: Unknown 24 | reveal_type(y) # revealed: Unknown
| ^^^^^^^^^^^^^^ Revealed type is `Unknown` | ^^^^^^^^^^^^^^ `Unknown`
| |
``` ```

View file

@ -175,7 +175,7 @@ impl Unmatched for &Diagnostic {
maybe_add_undefined_reveal_clarification( maybe_add_undefined_reveal_clarification(
self, self,
format_args!(r#"[{id}] "{message}""#, message = self.primary_message()), format_args!(r#"[{id}] "{message}""#, message = self.concise_message()),
) )
} }
} }
@ -191,7 +191,7 @@ impl UnmatchedWithColumn for &Diagnostic {
self, self,
format_args!( format_args!(
r#"{column} [{id}] "{message}""#, r#"{column} [{id}] "{message}""#,
message = self.primary_message() message = self.concise_message()
), ),
) )
} }
@ -283,14 +283,16 @@ impl Matcher {
match assertion { match assertion {
ParsedAssertion::Error(error) => { ParsedAssertion::Error(error) => {
let position = unmatched.iter().position(|diagnostic| { let position = unmatched.iter().position(|diagnostic| {
!error.rule.is_some_and(|rule| { let lint_name_matches = !error.rule.is_some_and(|rule| {
!(diagnostic.id().is_lint_named(rule) || diagnostic.id().matches(rule)) !(diagnostic.id().is_lint_named(rule) || diagnostic.id().matches(rule))
}) && error });
let column_matches = error
.column .column
.is_none_or(|col| col == self.column(diagnostic)) .is_none_or(|col| col == self.column(diagnostic));
&& error let message_matches = error.message_contains.is_none_or(|needle| {
.message_contains diagnostic.concise_message().to_string().contains(needle)
.is_none_or(|needle| diagnostic.primary_message().contains(needle)) });
lint_name_matches && column_matches && message_matches
}); });
if let Some(position) = position { if let Some(position) = position {
unmatched.swap_remove(position); unmatched.swap_remove(position);
@ -305,11 +307,15 @@ impl Matcher {
let mut matched_revealed_type = None; let mut matched_revealed_type = None;
let mut matched_undefined_reveal = None; let mut matched_undefined_reveal = None;
let expected_reveal_type_message = format!("Revealed type is `{expected_type}`"); let expected_reveal_type_message = format!("`{expected_type}`");
for (index, diagnostic) in unmatched.iter().enumerate() { for (index, diagnostic) in unmatched.iter().enumerate() {
if matched_revealed_type.is_none() if matched_revealed_type.is_none()
&& diagnostic.id() == DiagnosticId::RevealedType && diagnostic.id() == DiagnosticId::RevealedType
&& diagnostic.primary_message() == expected_reveal_type_message && diagnostic
.primary_annotation()
.and_then(|a| a.get_message())
.unwrap_or_default()
== expected_reveal_type_message
{ {
matched_revealed_type = Some(index); matched_revealed_type = Some(index);
} else if matched_undefined_reveal.is_none() } else if matched_undefined_reveal.is_none()
@ -361,7 +367,11 @@ mod tests {
} }
fn into_diagnostic(self, file: File) -> Diagnostic { fn into_diagnostic(self, file: File) -> Diagnostic {
let mut diag = Diagnostic::new(self.id, Severity::Error, ""); let mut diag = if self.id == DiagnosticId::RevealedType {
Diagnostic::new(self.id, Severity::Error, "Revealed type")
} else {
Diagnostic::new(self.id, Severity::Error, "")
};
let span = Span::from(file).with_range(self.range); let span = Span::from(file).with_range(self.range);
diag.annotate(Annotation::primary(span).message(self.message)); diag.annotate(Annotation::primary(span).message(self.message));
diag diag
@ -417,7 +427,7 @@ mod tests {
"x # revealed: Foo", "x # revealed: Foo",
vec![ExpectedDiagnostic::new( vec![ExpectedDiagnostic::new(
DiagnosticId::RevealedType, DiagnosticId::RevealedType,
"Revealed type is `Foo`", "`Foo`",
0, 0,
)], )],
); );
@ -431,7 +441,7 @@ mod tests {
"x # revealed: Foo", "x # revealed: Foo",
vec![ExpectedDiagnostic::new( vec![ExpectedDiagnostic::new(
DiagnosticId::lint("not-revealed-type"), DiagnosticId::lint("not-revealed-type"),
"Revealed type is `Foo`", "`Foo`",
0, 0,
)], )],
); );
@ -442,7 +452,7 @@ mod tests {
0, 0,
&[ &[
"unmatched assertion: revealed: Foo", "unmatched assertion: revealed: Foo",
r#"unexpected error: 1 [not-revealed-type] "Revealed type is `Foo`""#, r#"unexpected error: 1 [not-revealed-type] "`Foo`""#,
], ],
)], )],
); );
@ -465,7 +475,7 @@ mod tests {
0, 0,
&[ &[
"unmatched assertion: revealed: Foo", "unmatched assertion: revealed: Foo",
r#"unexpected error: 1 [revealed-type] "Something else""#, r#"unexpected error: 1 [revealed-type] "Revealed type: Something else""#,
], ],
)], )],
); );
@ -483,7 +493,7 @@ mod tests {
let result = get_result( let result = get_result(
"x # revealed: Foo", "x # revealed: Foo",
vec![ vec![
ExpectedDiagnostic::new(DiagnosticId::RevealedType, "Revealed type is `Foo`", 0), ExpectedDiagnostic::new(DiagnosticId::RevealedType, "`Foo`", 0),
ExpectedDiagnostic::new( ExpectedDiagnostic::new(
DiagnosticId::lint("undefined-reveal"), DiagnosticId::lint("undefined-reveal"),
"Doesn't matter", "Doesn't matter",
@ -514,7 +524,7 @@ mod tests {
let result = get_result( let result = get_result(
"x # revealed: Foo", "x # revealed: Foo",
vec![ vec![
ExpectedDiagnostic::new(DiagnosticId::RevealedType, "Revealed type is `Bar`", 0), ExpectedDiagnostic::new(DiagnosticId::RevealedType, "`Bar`", 0),
ExpectedDiagnostic::new( ExpectedDiagnostic::new(
DiagnosticId::lint("undefined-reveal"), DiagnosticId::lint("undefined-reveal"),
"Doesn't matter", "Doesn't matter",
@ -529,7 +539,7 @@ mod tests {
0, 0,
&[ &[
"unmatched assertion: revealed: Foo", "unmatched assertion: revealed: Foo",
r#"unexpected error: 1 [revealed-type] "Revealed type is `Bar`""#, r#"unexpected error: 1 [revealed-type] "Revealed type: `Bar`""#,
], ],
)], )],
); );
@ -545,11 +555,7 @@ mod tests {
"undefined reveal message", "undefined reveal message",
0, 0,
), ),
ExpectedDiagnostic::new( ExpectedDiagnostic::new(DiagnosticId::RevealedType, "`Literal[1]`", 12),
DiagnosticId::RevealedType,
"Revealed type is `Literal[1]`",
12,
),
], ],
); );
@ -560,7 +566,7 @@ mod tests {
&[ &[
"used built-in `reveal_type`: add a `# revealed` assertion on this line (\ "used built-in `reveal_type`: add a `# revealed` assertion on this line (\
original diagnostic: [undefined-reveal] \"undefined reveal message\")", original diagnostic: [undefined-reveal] \"undefined reveal message\")",
r#"unexpected error: [revealed-type] "Revealed type is `Literal[1]`""#, r#"unexpected error: [revealed-type] "Revealed type: `Literal[1]`""#,
], ],
)], )],
); );
@ -576,11 +582,7 @@ mod tests {
"undefined reveal message", "undefined reveal message",
0, 0,
), ),
ExpectedDiagnostic::new( ExpectedDiagnostic::new(DiagnosticId::RevealedType, "`Literal[1]`", 12),
DiagnosticId::RevealedType,
"Revealed type is `Literal[1]`",
12,
),
], ],
); );
@ -592,7 +594,7 @@ mod tests {
"unmatched assertion: error: [something-else]", "unmatched assertion: error: [something-else]",
"used built-in `reveal_type`: add a `# revealed` assertion on this line (\ "used built-in `reveal_type`: add a `# revealed` assertion on this line (\
original diagnostic: 1 [undefined-reveal] \"undefined reveal message\")", original diagnostic: 1 [undefined-reveal] \"undefined reveal message\")",
r#"unexpected error: 13 [revealed-type] "Revealed type is `Literal[1]`""#, r#"unexpected error: 13 [revealed-type] "Revealed type: `Literal[1]`""#,
], ],
)], )],
); );
@ -1008,11 +1010,7 @@ mod tests {
&source, &source,
vec![ vec![
ExpectedDiagnostic::new(DiagnosticId::lint("undefined-reveal"), "msg", reveal), ExpectedDiagnostic::new(DiagnosticId::lint("undefined-reveal"), "msg", reveal),
ExpectedDiagnostic::new( ExpectedDiagnostic::new(DiagnosticId::RevealedType, "`Literal[5]`", reveal),
DiagnosticId::RevealedType,
"Revealed type is `Literal[5]`",
reveal,
),
], ],
); );