Skip to content

Commit 7f0e445

Browse files
maggiemossmeta-codesync[bot]
authored andcommitted
Rename SuppressableError -> SerializedError
Summary: This is re-used for unused ignores, and the name was a bit confusing. Attempt to make it's use more clear. Reviewed By: yangdanny97 Differential Revision: D91708274 fbshipit-source-id: 35c95915de9566e7bac01bf9f324e4988e260b0b
1 parent ac9cb56 commit 7f0e445

File tree

3 files changed

+40
-30
lines changed

3 files changed

+40
-30
lines changed

pyrefly/lib/commands/check.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use crate::error::legacy::LegacyErrors;
6262
use crate::error::legacy::severity_to_str;
6363
use crate::error::summarize::print_error_summary;
6464
use crate::error::suppress;
65-
use crate::error::suppress::SuppressableError;
65+
use crate::error::suppress::SerializedError;
6666
use crate::module::typeshed::stdlib_search_path;
6767
use crate::report;
6868
use crate::state::load::FileContents;
@@ -1011,12 +1011,13 @@ impl CheckArgs {
10111011
}
10121012
if self.behavior.suppress_errors {
10131013
// TODO: Move this into separate command
1014-
let suppressable_errors: Vec<SuppressableError> = shown_errors
1014+
let serialized_errors: Vec<SerializedError> = shown_errors
10151015
.iter()
10161016
.filter(|e| e.severity() >= Severity::Warn)
1017-
.filter_map(SuppressableError::from_error)
1017+
.filter_map(SerializedError::from_error)
1018+
.filter(|e| !e.is_unused_ignore())
10181019
.collect();
1019-
suppress::suppress_errors(suppressable_errors);
1020+
suppress::suppress_errors(serialized_errors);
10201021
}
10211022
if self.behavior.remove_unused_ignores {
10221023
// TODO: Move this into separate command

pyrefly/lib/commands/suppress.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use pyrefly_config::error_kind::Severity;
1414
use crate::commands::files::FilesArgs;
1515
use crate::commands::util::CommandExitStatus;
1616
use crate::error::suppress;
17-
use crate::error::suppress::SuppressableError;
17+
use crate::error::suppress::SerializedError;
1818

1919
/// Suppress type errors by adding ignore comments to source files.
2020
#[derive(Clone, Debug, Parser)]
@@ -28,17 +28,21 @@ pub struct SuppressArgs {
2828
config_override: ConfigOverrideArgs,
2929

3030
/// Path to a JSON file containing errors to suppress.
31-
/// The JSON should be an array of objects with "path", "line", and "name" fields.
31+
/// The JSON should be an array of objects with "path", "line", "name", and "message" fields.
3232
#[arg(long)]
3333
json: Option<PathBuf>,
3434
}
3535

3636
impl SuppressArgs {
3737
pub fn run(&self) -> anyhow::Result<CommandExitStatus> {
38-
let suppressable_errors = if let Some(json_path) = &self.json {
39-
// Parse errors from JSON file
38+
let suppressable_errors: Vec<SerializedError> = if let Some(json_path) = &self.json {
39+
// Parse errors from JSON file, filtering out UnusedIgnore errors
4040
let json_content = std::fs::read_to_string(json_path)?;
41-
serde_json::from_str(&json_content)?
41+
let errors: Vec<SerializedError> = serde_json::from_str(&json_content)?;
42+
errors
43+
.into_iter()
44+
.filter(|e| !e.is_unused_ignore())
45+
.collect()
4246
} else {
4347
// Run type checking to collect errors
4448
self.config_override.validate()?;
@@ -49,11 +53,12 @@ impl SuppressArgs {
4953
super::check::CheckArgs::parse_from(["check", "--output-format", "omit-errors"]);
5054
let (_, errors) = check_args.run_once(files_to_check, config_finder)?;
5155

52-
// Convert to SuppressableErrors, filtering by severity
56+
// Convert to SerializedErrors, filtering by severity and excluding UnusedIgnore
5357
errors
5458
.into_iter()
5559
.filter(|e| e.severity() >= Severity::Warn)
56-
.filter_map(|e| SuppressableError::from_error(&e))
60+
.filter_map(|e| SerializedError::from_error(&e))
61+
.filter(|e| !e.is_unused_ignore())
5762
.collect()
5863
};
5964

pyrefly/lib/error/suppress.rs

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,31 @@ use pyrefly_util::fs_anyhow;
1818
use regex::Regex;
1919
use ruff_python_ast::PySourceType;
2020
use serde::Deserialize;
21+
use serde::Serialize;
2122
use starlark_map::small_map::SmallMap;
2223
use starlark_map::small_set::SmallSet;
2324
use tracing::info;
2425

2526
use crate::error::error::Error;
2627

27-
/// A minimal representation of an error for suppression purposes.
28-
/// This struct holds only the fields needed to add or remove a suppression comment.
29-
#[derive(Deserialize)]
30-
pub struct SuppressableError {
28+
/// A serializable representation of an error for JSON input/output.
29+
/// This struct holds the fields needed to add or remove a suppression comment.
30+
#[derive(Deserialize, Serialize)]
31+
pub struct SerializedError {
3132
/// The file path where the error occurs.
3233
pub path: PathBuf,
3334
/// The 0-indexed line number where the error occurs.
3435
pub line: usize,
3536
/// The kebab-case name of the error kind (e.g., "bad-assignment").
3637
pub name: String,
38+
/// The error message. Used for UnusedIgnore errors to determine what to remove.
39+
pub message: String,
3740
}
3841

39-
impl SuppressableError {
40-
/// Creates a SuppressableError from an internal Error.
41-
/// Returns None if the error is not from a filesystem path or if the error
42-
/// is an UnusedIgnore (which cannot be suppressed).
42+
impl SerializedError {
43+
/// Creates a SerializedError from an internal Error.
44+
/// Returns None if the error is not from a filesystem path.
4345
pub fn from_error(error: &Error) -> Option<Self> {
44-
// UnusedIgnore errors cannot be suppressed
45-
if error.error_kind() == ErrorKind::UnusedIgnore {
46-
return None;
47-
}
4846
if let ModulePathDetails::FileSystem(path) = error.path().details() {
4947
Some(Self {
5048
path: (**path).clone(),
@@ -54,11 +52,17 @@ impl SuppressableError {
5452
.line_within_file()
5553
.to_zero_indexed() as usize,
5654
name: error.error_kind().to_name().to_owned(),
55+
message: error.msg().to_owned(),
5756
})
5857
} else {
5958
None
6059
}
6160
}
61+
62+
/// Returns true if this error is an UnusedIgnore error.
63+
pub fn is_unused_ignore(&self) -> bool {
64+
self.name == ErrorKind::UnusedIgnore.to_name()
65+
}
6266
}
6367

6468
/// Detects the line ending style used in a string.
@@ -73,7 +77,7 @@ fn detect_line_ending(content: &str) -> &'static str {
7377

7478
/// Combines all errors that affect one line into a single entry.
7579
/// The current format is: `# pyrefly: ignore [error1, error2, ...]`
76-
fn dedup_errors(errors: &[SuppressableError]) -> SmallMap<usize, String> {
80+
fn dedup_errors(errors: &[SerializedError]) -> SmallMap<usize, String> {
7781
let mut deduped_errors: SmallMap<usize, HashSet<String>> = SmallMap::new();
7882
for error in errors {
7983
deduped_errors
@@ -199,7 +203,7 @@ fn replace_ignore_comment(line: &str, merged_comment: &str) -> String {
199203
/// Returns a list of files that failed to be patched, and a list of files that were patched.
200204
/// The list of failures includes the error that occurred, which may be a read or write error.
201205
fn add_suppressions(
202-
path_errors: &SmallMap<PathBuf, Vec<SuppressableError>>,
206+
path_errors: &SmallMap<PathBuf, Vec<SerializedError>>,
203207
) -> (Vec<(&PathBuf, anyhow::Error)>, Vec<&PathBuf>) {
204208
let mut failures = vec![];
205209
let mut successes = vec![];
@@ -309,9 +313,9 @@ fn extract_error_codes(comment: &str) -> Vec<String> {
309313
}
310314

311315
/// Suppresses errors by adding ignore comments to source files.
312-
/// Takes a list of SuppressableErrors
313-
pub fn suppress_errors(errors: Vec<SuppressableError>) {
314-
let mut path_errors: SmallMap<PathBuf, Vec<SuppressableError>> = SmallMap::new();
316+
/// Takes a list of SerializedErrors
317+
pub fn suppress_errors(errors: Vec<SerializedError>) {
318+
let mut path_errors: SmallMap<PathBuf, Vec<SerializedError>> = SmallMap::new();
315319
for e in errors {
316320
path_errors.entry(e.path.clone()).or_default().push(e);
317321
}
@@ -512,12 +516,12 @@ mod tests {
512516

513517
fn assert_suppress_errors(before: &str, after: &str) {
514518
let (errors, tdir) = get_errors(before);
515-
let suppressable_errors: Vec<SuppressableError> = errors
519+
let suppressable_errors: Vec<SerializedError> = errors
516520
.collect_errors()
517521
.shown
518522
.iter()
519523
.filter(|e| e.severity() >= Severity::Warn)
520-
.filter_map(SuppressableError::from_error)
524+
.filter_map(SerializedError::from_error)
521525
.collect();
522526
suppress::suppress_errors(suppressable_errors);
523527
let got_file = fs_anyhow::read_to_string(&get_path(&tdir)).unwrap();

0 commit comments

Comments
 (0)