forked from rtk-ai/rtk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmypy_cmd.rs
More file actions
363 lines (326 loc) · 11.9 KB
/
mypy_cmd.rs
File metadata and controls
363 lines (326 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//! Filters mypy type-checking output, grouping errors by file.
use crate::core::runner;
use crate::core::utils::{resolved_command, strip_ansi, tool_exists, truncate};
use anyhow::Result;
use regex::Regex;
use std::collections::HashMap;
pub fn run(args: &[String], verbose: u8) -> Result<i32> {
let mut cmd = if tool_exists("mypy") {
resolved_command("mypy")
} else {
let mut c = resolved_command("python3");
c.arg("-m").arg("mypy");
c
};
for arg in args {
cmd.arg(arg);
}
if verbose > 0 {
eprintln!("Running: mypy {}", args.join(" "));
}
runner::run_filtered(
cmd,
"mypy",
&args.join(" "),
|raw| filter_mypy_output(&strip_ansi(raw)),
runner::RunOptions::default(),
)
}
struct MypyError {
file: String,
line: usize,
code: String,
message: String,
context_lines: Vec<String>,
}
pub fn filter_mypy_output(output: &str) -> String {
lazy_static::lazy_static! {
// file.py:12: error: Message [error-code]
// file.py:12:5: error: Message [error-code]
static ref MYPY_DIAG: Regex = Regex::new(
r"^(.+?):(\d+)(?::\d+)?: (error|warning|note): (.+?)(?:\s+\[(.+)\])?$"
).unwrap();
}
let lines: Vec<&str> = output.lines().collect();
let mut errors: Vec<MypyError> = Vec::new();
let mut fileless_lines: Vec<String> = Vec::new();
let mut i = 0;
while i < lines.len() {
let line = lines[i];
// Skip mypy's own summary line
if line.starts_with("Found ") && line.contains(" error") {
i += 1;
continue;
}
// Skip "Success: no issues found"
if line.starts_with("Success:") {
i += 1;
continue;
}
if let Some(caps) = MYPY_DIAG.captures(line) {
let severity = &caps[3];
let file = caps[1].to_string();
let line_num: usize = caps[2].parse().unwrap_or(0);
let message = caps[4].to_string();
let code = caps
.get(5)
.map(|m| m.as_str().to_string())
.unwrap_or_default();
if severity == "note" {
// Attach note to preceding error if same file and line
if let Some(last) = errors.last_mut() {
if last.file == file {
last.context_lines.push(message);
i += 1;
continue;
}
}
// Standalone note with no parent -- display as fileless
fileless_lines.push(line.to_string());
i += 1;
continue;
}
let mut err = MypyError {
file,
line: line_num,
code,
message,
context_lines: Vec::new(),
};
// Capture continuation note lines
i += 1;
while i < lines.len() {
if let Some(next_caps) = MYPY_DIAG.captures(lines[i]) {
if &next_caps[3] == "note" && next_caps[1] == err.file {
let note_msg = next_caps[4].to_string();
err.context_lines.push(note_msg);
i += 1;
continue;
}
}
break;
}
errors.push(err);
} else if line.contains("error:") && !line.trim().is_empty() {
// File-less error (config errors, import errors)
fileless_lines.push(line.to_string());
i += 1;
} else {
i += 1;
}
}
// No errors at all
if errors.is_empty() && fileless_lines.is_empty() {
if output.contains("Success: no issues found") || output.contains("no issues found") {
return "mypy: No issues found".to_string();
}
return "mypy: No issues found".to_string();
}
// Group by file
let mut by_file: HashMap<String, Vec<&MypyError>> = HashMap::new();
for err in &errors {
by_file.entry(err.file.clone()).or_default().push(err);
}
// Count by error code
let mut by_code: HashMap<String, usize> = HashMap::new();
for err in &errors {
if !err.code.is_empty() {
*by_code.entry(err.code.clone()).or_insert(0) += 1;
}
}
let mut result = String::new();
// File-less errors first
for line in &fileless_lines {
result.push_str(line);
result.push('\n');
}
if !fileless_lines.is_empty() && !errors.is_empty() {
result.push('\n');
}
if !errors.is_empty() {
result.push_str(&format!(
"mypy: {} errors in {} files\n",
errors.len(),
by_file.len()
));
result.push_str("═══════════════════════════════════════\n");
// Top error codes summary (only when 2+ distinct codes)
let mut code_counts: Vec<_> = by_code.iter().collect();
code_counts.sort_by(|a, b| b.1.cmp(a.1));
if code_counts.len() > 1 {
let codes_str: Vec<String> = code_counts
.iter()
.take(5)
.map(|(code, count)| format!("{} ({}x)", code, count))
.collect();
result.push_str(&format!("Top codes: {}\n\n", codes_str.join(", ")));
}
// Files sorted by error count (most errors first)
let mut files_sorted: Vec<_> = by_file.iter().collect();
files_sorted.sort_by(|a, b| b.1.len().cmp(&a.1.len()));
for (file, file_errors) in &files_sorted {
result.push_str(&format!("{} ({} errors)\n", file, file_errors.len()));
for err in *file_errors {
if err.code.is_empty() {
result.push_str(&format!(
" L{}: {}\n",
err.line,
truncate(&err.message, 120)
));
} else {
result.push_str(&format!(
" L{}: [{}] {}\n",
err.line,
err.code,
truncate(&err.message, 120)
));
}
for ctx in &err.context_lines {
result.push_str(&format!(" {}\n", truncate(ctx, 120)));
}
}
result.push('\n');
}
}
result.trim().to_string()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_filter_mypy_errors_grouped_by_file() {
let output = "\
src/server/auth.py:12: error: Incompatible return value type (got \"str\", expected \"int\") [return-value]
src/server/auth.py:15: error: Argument 1 has incompatible type \"int\"; expected \"str\" [arg-type]
src/models/user.py:8: error: Name \"foo\" is not defined [name-defined]
src/models/user.py:10: error: Incompatible types in assignment [assignment]
src/models/user.py:20: error: Missing return statement [return]
Found 5 errors in 2 files (checked 10 source files)
";
let result = filter_mypy_output(output);
assert!(result.contains("mypy: 5 errors in 2 files"));
// user.py has 3 errors, auth.py has 2 -- user.py should come first
let user_pos = result.find("user.py").unwrap();
let auth_pos = result.find("auth.py").unwrap();
assert!(
user_pos < auth_pos,
"user.py (3 errors) should appear before auth.py (2 errors)"
);
assert!(result.contains("user.py (3 errors)"));
assert!(result.contains("auth.py (2 errors)"));
}
#[test]
fn test_filter_mypy_with_column_numbers() {
let output = "\
src/api.py:10:5: error: Incompatible return value type [return-value]
";
let result = filter_mypy_output(output);
assert!(result.contains("L10:"));
assert!(result.contains("[return-value]"));
assert!(result.contains("Incompatible return value type"));
}
#[test]
fn test_filter_mypy_top_codes_summary() {
let output = "\
a.py:1: error: Error one [return-value]
a.py:2: error: Error two [return-value]
a.py:3: error: Error three [return-value]
b.py:1: error: Error four [name-defined]
c.py:1: error: Error five [arg-type]
Found 5 errors in 3 files
";
let result = filter_mypy_output(output);
assert!(result.contains("Top codes:"));
assert!(result.contains("return-value (3x)"));
assert!(result.contains("name-defined (1x)"));
assert!(result.contains("arg-type (1x)"));
}
#[test]
fn test_filter_mypy_single_code_no_summary() {
let output = "\
a.py:1: error: Error one [return-value]
a.py:2: error: Error two [return-value]
b.py:1: error: Error three [return-value]
Found 3 errors in 2 files
";
let result = filter_mypy_output(output);
assert!(
!result.contains("Top codes:"),
"Top codes should not appear with only one distinct code"
);
}
#[test]
fn test_filter_mypy_every_error_shown() {
let output = "\
src/api.py:10: error: Type \"str\" not assignable to \"int\" [assignment]
src/api.py:20: error: Missing return statement [return]
src/api.py:30: error: Name \"bar\" is not defined [name-defined]
";
let result = filter_mypy_output(output);
assert!(result.contains("Type \"str\" not assignable to \"int\""));
assert!(result.contains("Missing return statement"));
assert!(result.contains("Name \"bar\" is not defined"));
assert!(result.contains("L10:"));
assert!(result.contains("L20:"));
assert!(result.contains("L30:"));
}
#[test]
fn test_filter_mypy_note_continuation() {
let output = "\
src/app.py:10: error: Incompatible types in assignment [assignment]
src/app.py:10: note: Expected type \"int\"
src/app.py:10: note: Got type \"str\"
src/app.py:20: error: Missing return statement [return]
";
let result = filter_mypy_output(output);
assert!(result.contains("Incompatible types in assignment"));
assert!(result.contains("Expected type \"int\""));
assert!(result.contains("Got type \"str\""));
assert!(result.contains("L10:"));
assert!(result.contains("L20:"));
}
#[test]
fn test_filter_mypy_fileless_errors() {
let output = "\
mypy: error: No module named 'nonexistent'
src/api.py:10: error: Name \"foo\" is not defined [name-defined]
Found 1 error in 1 file
";
let result = filter_mypy_output(output);
// File-less error should appear verbatim before grouped output
assert!(result.contains("mypy: error: No module named 'nonexistent'"));
assert!(result.contains("api.py (1 error"));
let fileless_pos = result.find("No module named").unwrap();
let grouped_pos = result.find("api.py").unwrap();
assert!(
fileless_pos < grouped_pos,
"File-less errors should appear before grouped file errors"
);
}
#[test]
fn test_filter_mypy_no_errors() {
let output = "Success: no issues found in 5 source files\n";
let result = filter_mypy_output(output);
assert_eq!(result, "mypy: No issues found");
}
#[test]
fn test_filter_mypy_no_file_limit() {
let mut output = String::new();
for i in 1..=15 {
output.push_str(&format!(
"src/file{}.py:{}: error: Error in file {}. [assignment]\n",
i, i, i
));
}
output.push_str("Found 15 errors in 15 files\n");
let result = filter_mypy_output(&output);
assert!(result.contains("15 errors in 15 files"));
for i in 1..=15 {
assert!(
result.contains(&format!("file{}.py", i)),
"file{}.py missing from output",
i
);
}
}
}