Skip to content

Commit 06ad687

Browse files
Deduplicate deprecation warnings for v0.2.0 release (#9764)
## Summary Adds an additional warning macro (we should consolidate these later) that shows a warning once based on the content of the warning itself. This is less efficient than `warn_user_once!` and `warn_user_by_id!`, but this is so expensive that it doesn't matter at all. Applies this macro to the various warnings for the v0.2.0 release, and also includes the filename in said warnings, so the FastAPI case is now: ```text warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in /Users/crmarsh/workspace/fastapi/pyproject.toml: - 'ignore' -> 'lint.ignore' - 'select' -> 'lint.select' - 'isort' -> 'lint.isort' - 'pyupgrade' -> 'lint.pyupgrade' - 'per-file-ignores' -> 'lint.per-file-ignores' ``` --------- Co-authored-by: Zanie <[email protected]>
1 parent 148b64e commit 06ad687

File tree

7 files changed

+133
-66
lines changed

7 files changed

+133
-66
lines changed

crates/ruff/tests/format.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -508,11 +508,9 @@ if __name__ == '__main__':
508508
say_hy("dear Ruff contributor")
509509
510510
----- stderr -----
511-
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in your configuration:
511+
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `ruff.toml`:
512512
- 'extend-select' -> 'lint.extend-select'
513513
- 'ignore' -> 'lint.ignore'
514-
515-
516514
"###);
517515
Ok(())
518516
}
@@ -551,11 +549,9 @@ if __name__ == '__main__':
551549
say_hy("dear Ruff contributor")
552550
553551
----- stderr -----
554-
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in your configuration:
552+
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `ruff.toml`:
555553
- 'extend-select' -> 'lint.extend-select'
556554
- 'ignore' -> 'lint.ignore'
557-
558-
559555
"###);
560556
Ok(())
561557
}

crates/ruff/tests/lint.rs

+87-43
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
#![cfg(not(target_family = "wasm"))]
44

5+
use regex::escape;
56
use std::fs;
67
use std::process::Command;
78
use std::str;
@@ -13,6 +14,10 @@ use tempfile::TempDir;
1314
const BIN_NAME: &str = "ruff";
1415
const STDIN_BASE_OPTIONS: &[&str] = &["--no-cache", "--output-format", "concise"];
1516

17+
fn tempdir_filter(tempdir: &TempDir) -> String {
18+
format!(r"{}\\?/?", escape(tempdir.path().to_str().unwrap()))
19+
}
20+
1621
#[test]
1722
fn top_level_options() -> Result<()> {
1823
let tempdir = TempDir::new()?;
@@ -27,29 +32,32 @@ inline-quotes = "single"
2732
"#,
2833
)?;
2934

30-
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
31-
.args(STDIN_BASE_OPTIONS)
32-
.arg("--config")
33-
.arg(&ruff_toml)
34-
.args(["--stdin-filename", "test.py"])
35-
.arg("-")
36-
.pass_stdin(r#"a = "abcba".strip("aba")"#), @r###"
37-
success: false
38-
exit_code: 1
39-
----- stdout -----
40-
test.py:1:5: Q000 [*] Double quotes found but single quotes preferred
41-
test.py:1:5: B005 Using `.strip()` with multi-character strings is misleading
42-
test.py:1:19: Q000 [*] Double quotes found but single quotes preferred
43-
Found 3 errors.
44-
[*] 2 fixable with the `--fix` option.
45-
46-
----- stderr -----
47-
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in your configuration:
48-
- 'extend-select' -> 'lint.extend-select'
49-
- 'flake8-quotes' -> 'lint.flake8-quotes'
50-
35+
insta::with_settings!({
36+
filters => vec![(tempdir_filter(&tempdir).as_str(), "[TMP]/")]
37+
}, {
38+
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
39+
.args(STDIN_BASE_OPTIONS)
40+
.arg("--config")
41+
.arg(&ruff_toml)
42+
.args(["--stdin-filename", "test.py"])
43+
.arg("-")
44+
.pass_stdin(r#"a = "abcba".strip("aba")"#), @r###"
45+
success: false
46+
exit_code: 1
47+
----- stdout -----
48+
test.py:1:5: Q000 [*] Double quotes found but single quotes preferred
49+
test.py:1:5: B005 Using `.strip()` with multi-character strings is misleading
50+
test.py:1:19: Q000 [*] Double quotes found but single quotes preferred
51+
Found 3 errors.
52+
[*] 2 fixable with the `--fix` option.
53+
54+
----- stderr -----
55+
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `[TMP]/ruff.toml`:
56+
- 'extend-select' -> 'lint.extend-select'
57+
- 'flake8-quotes' -> 'lint.flake8-quotes'
58+
"###);
59+
});
5160

52-
"###);
5361
Ok(())
5462
}
5563

@@ -68,6 +76,9 @@ inline-quotes = "single"
6876
"#,
6977
)?;
7078

79+
insta::with_settings!({
80+
filters => vec![(tempdir_filter(&tempdir).as_str(), "[TMP]/")]
81+
}, {
7182
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
7283
.args(STDIN_BASE_OPTIONS)
7384
.arg("--config")
@@ -85,6 +96,8 @@ inline-quotes = "single"
8596
8697
----- stderr -----
8798
"###);
99+
});
100+
88101
Ok(())
89102
}
90103

@@ -103,6 +116,9 @@ inline-quotes = "single"
103116
"#,
104117
)?;
105118

119+
insta::with_settings!({
120+
filters => vec![(tempdir_filter(&tempdir).as_str(), "[TMP]/")]
121+
}, {
106122
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
107123
.args(STDIN_BASE_OPTIONS)
108124
.arg("--config")
@@ -119,11 +135,11 @@ inline-quotes = "single"
119135
[*] 2 fixable with the `--fix` option.
120136
121137
----- stderr -----
122-
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in your configuration:
138+
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `[TMP]/ruff.toml`:
123139
- 'extend-select' -> 'lint.extend-select'
124-
125-
126140
"###);
141+
});
142+
127143
Ok(())
128144
}
129145

@@ -146,6 +162,9 @@ inline-quotes = "single"
146162
"#,
147163
)?;
148164

165+
insta::with_settings!({
166+
filters => vec![(tempdir_filter(&tempdir).as_str(), "[TMP]/")]
167+
}, {
149168
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
150169
.args(STDIN_BASE_OPTIONS)
151170
.arg("--config")
@@ -162,11 +181,11 @@ inline-quotes = "single"
162181
[*] 2 fixable with the `--fix` option.
163182
164183
----- stderr -----
165-
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in your configuration:
184+
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `[TMP]/ruff.toml`:
166185
- 'flake8-quotes' -> 'lint.flake8-quotes'
167-
168-
169186
"###);
187+
});
188+
170189
Ok(())
171190
}
172191

@@ -222,6 +241,9 @@ OTHER = "OTHER"
222241

223242
fs::write(out_dir.join("a.py"), r#"a = "a""#)?;
224243

244+
insta::with_settings!({
245+
filters => vec![(tempdir_filter(&tempdir).as_str(), "[TMP]/")]
246+
}, {
225247
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
226248
.current_dir(tempdir.path())
227249
.arg("check")
@@ -241,11 +263,11 @@ OTHER = "OTHER"
241263
[*] 3 fixable with the `--fix` option.
242264
243265
----- stderr -----
244-
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in your configuration:
266+
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `ruff.toml`:
245267
- 'extend-select' -> 'lint.extend-select'
246-
247-
248268
"###);
269+
});
270+
249271
Ok(())
250272
}
251273

@@ -266,6 +288,9 @@ inline-quotes = "single"
266288
"#,
267289
)?;
268290

291+
insta::with_settings!({
292+
filters => vec![(tempdir_filter(&tempdir).as_str(), "[TMP]/")]
293+
}, {
269294
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
270295
.current_dir(tempdir.path())
271296
.arg("check")
@@ -288,11 +313,11 @@ if __name__ == "__main__":
288313
[*] 2 fixable with the `--fix` option.
289314
290315
----- stderr -----
291-
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in your configuration:
316+
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `ruff.toml`:
292317
- 'extend-select' -> 'lint.extend-select'
293-
294-
295318
"###);
319+
});
320+
296321
Ok(())
297322
}
298323

@@ -311,6 +336,9 @@ max-line-length = 100
311336
"#,
312337
)?;
313338

339+
insta::with_settings!({
340+
filters => vec![(tempdir_filter(&tempdir).as_str(), "[TMP]/")]
341+
}, {
314342
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
315343
.args(STDIN_BASE_OPTIONS)
316344
.arg("--config")
@@ -330,12 +358,12 @@ _ = "---------------------------------------------------------------------------
330358
Found 1 error.
331359
332360
----- stderr -----
333-
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in your configuration:
361+
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `[TMP]/ruff.toml`:
334362
- 'select' -> 'lint.select'
335363
- 'pycodestyle' -> 'lint.pycodestyle'
336-
337-
338364
"###);
365+
});
366+
339367
Ok(())
340368
}
341369

@@ -353,6 +381,9 @@ inline-quotes = "single"
353381
"#,
354382
)?;
355383

384+
insta::with_settings!({
385+
filters => vec![(tempdir_filter(&tempdir).as_str(), "[TMP]/")]
386+
}, {
356387
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
357388
.current_dir(tempdir.path())
358389
.arg("check")
@@ -377,11 +408,11 @@ if __name__ == "__main__":
377408
[*] 1 fixable with the `--fix` option.
378409
379410
----- stderr -----
380-
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in your configuration:
411+
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `ruff.toml`:
381412
- 'extend-select' -> 'lint.extend-select'
382-
383-
384413
"###);
414+
});
415+
385416
Ok(())
386417
}
387418

@@ -399,6 +430,9 @@ inline-quotes = "single"
399430
"#,
400431
)?;
401432

433+
insta::with_settings!({
434+
filters => vec![(tempdir_filter(&tempdir).as_str(), "[TMP]/")]
435+
}, {
402436
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
403437
.current_dir(tempdir.path())
404438
.arg("check")
@@ -423,11 +457,11 @@ if __name__ == "__main__":
423457
[*] 1 fixable with the `--fix` option.
424458
425459
----- stderr -----
426-
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in your configuration:
460+
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `ruff.toml`:
427461
- 'extend-select' -> 'lint.extend-select'
428-
429-
430462
"###);
463+
});
464+
431465
Ok(())
432466
}
433467

@@ -456,6 +490,9 @@ ignore = ["D203", "D212"]
456490
"#,
457491
)?;
458492

493+
insta::with_settings!({
494+
filters => vec![(tempdir_filter(&tempdir).as_str(), "[TMP]/")]
495+
}, {
459496
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
460497
.current_dir(sub_dir)
461498
.arg("check")
@@ -468,6 +505,8 @@ ignore = ["D203", "D212"]
468505
----- stderr -----
469506
warning: No Python files found under the given path(s)
470507
"###);
508+
});
509+
471510
Ok(())
472511
}
473512

@@ -524,6 +563,9 @@ include = ["*.ipy"]
524563
"#,
525564
)?;
526565

566+
insta::with_settings!({
567+
filters => vec![(tempdir_filter(&tempdir).as_str(), "[TMP]/")]
568+
}, {
527569
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
528570
.current_dir(tempdir.path())
529571
.arg("check")
@@ -540,5 +582,7 @@ include = ["*.ipy"]
540582
541583
----- stderr -----
542584
"###);
585+
});
586+
543587
Ok(())
544588
}

crates/ruff/tests/resolve_files.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,8 @@ fn check_project_include_defaults() {
3939
[BASEPATH]/include-test/subdirectory/c.py
4040
4141
----- stderr -----
42-
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in your configuration:
42+
warning: The top-level linter settings are deprecated in favour of their counterparts in the `lint` section. Please update the following options in `nested-project/pyproject.toml`:
4343
- 'select' -> 'lint.select'
44-
45-
4644
"###);
4745
});
4846
}

crates/ruff_linter/src/logging.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ use fern;
88
use log::Level;
99
use once_cell::sync::Lazy;
1010
use ruff_python_parser::{ParseError, ParseErrorType};
11+
use rustc_hash::FxHashSet;
1112

1213
use ruff_source_file::{LineIndex, OneIndexed, SourceCode, SourceLocation};
1314

1415
use crate::fs;
1516
use crate::source_kind::SourceKind;
1617
use ruff_notebook::Notebook;
1718

18-
pub static WARNINGS: Lazy<Mutex<Vec<&'static str>>> = Lazy::new(Mutex::default);
19+
pub static IDENTIFIERS: Lazy<Mutex<Vec<&'static str>>> = Lazy::new(Mutex::default);
1920

2021
/// Warn a user once, with uniqueness determined by the given ID.
2122
#[macro_export]
@@ -24,7 +25,7 @@ macro_rules! warn_user_once_by_id {
2425
use colored::Colorize;
2526
use log::warn;
2627

27-
if let Ok(mut states) = $crate::logging::WARNINGS.lock() {
28+
if let Ok(mut states) = $crate::logging::IDENTIFIERS.lock() {
2829
if !states.contains(&$id) {
2930
let message = format!("{}", format_args!($($arg)*));
3031
warn!("{}", message.bold());
@@ -34,6 +35,26 @@ macro_rules! warn_user_once_by_id {
3435
};
3536
}
3637

38+
pub static MESSAGES: Lazy<Mutex<FxHashSet<String>>> = Lazy::new(Mutex::default);
39+
40+
/// Warn a user once, if warnings are enabled, with uniqueness determined by the content of the
41+
/// message.
42+
#[macro_export]
43+
macro_rules! warn_user_once_by_message {
44+
($($arg:tt)*) => {
45+
use colored::Colorize;
46+
use log::warn;
47+
48+
if let Ok(mut states) = $crate::logging::MESSAGES.lock() {
49+
let message = format!("{}", format_args!($($arg)*));
50+
if !states.contains(&message) {
51+
warn!("{}", message.bold());
52+
states.insert(message);
53+
}
54+
}
55+
};
56+
}
57+
3758
/// Warn a user once, with uniqueness determined by the calling location itself.
3859
#[macro_export]
3960
macro_rules! warn_user_once {

0 commit comments

Comments
 (0)