Skip to content

Commit 284a4ff

Browse files
maggiemossmeta-codesync[bot]
authored andcommitted
Fix pyrefly suppression syntax
Summary: We have been adding suppressions with error codes, but the current format will hide any pyrefly error on the same line or line below. This adjusts the suppression syntax to be the correct version. Reviewed By: stroxler Differential Revision: D85574579 fbshipit-source-id: 2ecbdd6315d4a3321cab15dcd086f6c9bd4297c2
1 parent 3a98cb3 commit 284a4ff

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

pyrefly/lib/error/suppress.rs

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,21 @@ use crate::state::errors::Errors;
2727
/// Combines all errors that affect one line into a single entry.
2828
// The current format is: `# pyrefly: ignore # error1, error2, ...`
2929
fn dedup_errors(errors: &[Error]) -> SmallMap<usize, String> {
30-
let mut deduped_errors = SmallMap::new();
30+
let mut deduped_errors: SmallMap<usize, Vec<String>> = SmallMap::new();
3131
for error in errors {
32-
let e: &mut String = deduped_errors
33-
.entry(error.display_range().start.line.to_zero_indexed() as usize)
34-
.or_default();
35-
let contains_error = e.contains(error.error_kind().to_name());
36-
if e.is_empty() {
37-
e.push_str("# pyrefly: ignore # ");
38-
} else if !contains_error {
39-
e.push_str(", ");
40-
}
41-
42-
if !contains_error {
43-
e.push_str(error.error_kind().to_name());
44-
}
32+
let line = error.display_range().start.line.to_zero_indexed() as usize;
33+
let error_name = error.error_kind().to_name().to_owned();
34+
deduped_errors.entry(line).or_default().push(error_name);
35+
}
36+
let mut formatted_errors = SmallMap::new();
37+
for (line, error_set) in deduped_errors {
38+
let mut error_codes: Vec<_> = error_set.into_iter().collect();
39+
error_codes.sort();
40+
let error_codes_str = error_codes.join(", ");
41+
let comment = format!("# pyrefly: ignore [{}]", error_codes_str);
42+
formatted_errors.insert(line, comment);
4543
}
46-
deduped_errors
44+
formatted_errors
4745
}
4846

4947
// TODO: In future have this return an ast as well as the string for comparison
@@ -326,18 +324,18 @@ f(x)
326324
327325
"#,
328326
r#"
329-
# pyrefly: ignore # bad-assignment
327+
# pyrefly: ignore [bad-assignment]
330328
x: str = 1
331329
332330
333331
def f(y: int) -> None:
334332
"""Doc comment"""
335-
# pyrefly: ignore # unsupported-operation
333+
# pyrefly: ignore [unsupported-operation]
336334
x = "one" + y
337335
return x
338336
339337
340-
# pyrefly: ignore # bad-argument-type
338+
# pyrefly: ignore [bad-argument-type]
341339
f(x)
342340
343341
"#,
@@ -355,7 +353,7 @@ def foo() -> int:
355353
r#"
356354
def foo() -> int:
357355
# comment
358-
# pyrefly: ignore # bad-return
356+
# pyrefly: ignore [bad-return]
359357
return ""
360358
"#,
361359
);
@@ -370,7 +368,7 @@ def foo() -> int: pass
370368
"#,
371369
r#"
372370
# comment
373-
# pyrefly: ignore # bad-return
371+
# pyrefly: ignore [bad-return]
374372
def foo() -> int: pass
375373
"#,
376374
);
@@ -389,7 +387,7 @@ x: int = foo("Hello")
389387
# comment
390388
def foo(x: int) -> str:
391389
return ""
392-
# pyrefly: ignore # bad-assignment, bad-argument-type
390+
# pyrefly: ignore [bad-argument-type, bad-assignment]
393391
x: int = foo("Hello")
394392
"#,
395393
);
@@ -434,7 +432,7 @@ pass
434432
fn test_remove_suppression_above() {
435433
let input = r#"
436434
def f() -> int:
437-
# pyrefly: ignore # bad-return
435+
# pyrefly: ignore [bad-return]
438436
return 1
439437
"#;
440438
let want = r#"
@@ -449,7 +447,7 @@ def f() -> int:
449447
fn test_remove_suppression_above_two() {
450448
let input = r#"
451449
def g() -> str:
452-
# pyrefly: ignore # bad-return
450+
# pyrefly: ignore [bad-return]
453451
return "hello"
454452
"#;
455453
let want = r#"
@@ -464,7 +462,7 @@ def g() -> str:
464462
fn test_remove_suppression_inline() {
465463
let input = r#"
466464
def g() -> str:
467-
return "hello" # pyrefly: ignore # bad-return
465+
return "hello" # pyrefly: ignore [bad-return]
468466
"#;
469467
let want = r#"
470468
def g() -> str:
@@ -477,7 +475,7 @@ def g() -> str:
477475
fn test_remove_suppression_multiple() {
478476
let input = r#"
479477
def g() -> str:
480-
return "hello" # pyrefly: ignore # bad-return
478+
return "hello" # pyrefly: ignore [bad-return]
481479
def f() -> int:
482480
# pyrefly: ignore
483481
return 1
@@ -507,7 +505,7 @@ def f() -> int:
507505
r#"
508506
{GENERATED_TOKEN}
509507
def g() -> str:
510-
return "hello" # pyrefly: ignore # bad-return
508+
return "hello" # pyrefly: ignore [bad-return]
511509
def f() -> int:
512510
# pyrefly: ignore
513511
return 1
@@ -520,15 +518,15 @@ def f() -> int:
520518
fn test_no_remove_suppression() {
521519
let input = r#"
522520
def g() -> int:
523-
return "hello" # pyrefly: ignore # bad-return
521+
return "hello" # pyrefly: ignore [bad-return]
524522
"#;
525523
assert_remove_ignores(input, input, false, 0);
526524
}
527525
#[test]
528526
fn test_remove_generic_suppression() {
529527
let before = r#"
530528
def g() -> str:
531-
return "hello" # type: ignore # bad-return
529+
return "hello" # type: ignore [bad-return]
532530
"#;
533531
let after = r#"
534532
def g() -> str:
@@ -556,7 +554,7 @@ x: str = 1
556554
557555
"#,
558556
r#"
559-
x: str = 1 # pyrefly: ignore # bad-assignment
557+
x: str = 1 # pyrefly: ignore [bad-assignment]
560558
561559
"#,
562560
);

0 commit comments

Comments
 (0)