mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:24:57 +00:00
Unify OldDiagnostic
and Message
(#18391)
Some checks are pending
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks-instrumented (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
Some checks are pending
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks-instrumented (push) Blocked by required conditions
CI / benchmarks-walltime (push) Blocked by required conditions
Summary
--
This PR unifies the remaining differences between `OldDiagnostic` and
`Message` (`OldDiagnostic` was only missing an optional `noqa_offset`
field) and
replaces `Message` with `OldDiagnostic`.
The biggest functional difference is that the combined `OldDiagnostic`
kind no
longer implements `AsRule` for an infallible conversion to `Rule`. This
was
pretty easy to work around with `is_some_and` and `is_none_or` in the
few places
it was needed. In `LintContext::report_diagnostic_if_enabled` we can
just use
the new `Violation::rule` method, which takes care of most cases.
Most of the interesting changes are in [this
range](8156992540
)
before I started renaming.
Test Plan
--
Existing tests
Future Work
--
I think it's time to start shifting some of these fields to the new
`Diagnostic`
kind. I believe we want `Fix` for sure, but I'm less sure about the
others. We
may want to keep a thin wrapper type here anyway to implement a `rule`
method,
so we could leave some of these fields on that too.
This commit is contained in:
parent
4e83db4d40
commit
10a1d9f01e
96 changed files with 902 additions and 914 deletions
|
@ -23,7 +23,7 @@ use ruff_source_file::SourceFileBuilder;
|
|||
use crate::codes::Rule;
|
||||
use crate::fix::{FixResult, fix_file};
|
||||
use crate::linter::check_path;
|
||||
use crate::message::{Emitter, EmitterContext, Message, TextEmitter};
|
||||
use crate::message::{Emitter, EmitterContext, OldDiagnostic, TextEmitter};
|
||||
use crate::package::PackageRoot;
|
||||
use crate::packaging::detect_package_root;
|
||||
use crate::settings::types::UnsafeFixes;
|
||||
|
@ -39,7 +39,10 @@ pub(crate) fn test_resource_path(path: impl AsRef<Path>) -> std::path::PathBuf {
|
|||
|
||||
/// Run [`check_path`] on a Python file in the `resources/test/fixtures` directory.
|
||||
#[cfg(not(fuzzing))]
|
||||
pub(crate) fn test_path(path: impl AsRef<Path>, settings: &LinterSettings) -> Result<Vec<Message>> {
|
||||
pub(crate) fn test_path(
|
||||
path: impl AsRef<Path>,
|
||||
settings: &LinterSettings,
|
||||
) -> Result<Vec<OldDiagnostic>> {
|
||||
let path = test_resource_path("fixtures").join(path);
|
||||
let source_type = PySourceType::from(&path);
|
||||
let source_kind = SourceKind::from_path(path.as_ref(), source_type)?.expect("valid source");
|
||||
|
@ -48,7 +51,7 @@ pub(crate) fn test_path(path: impl AsRef<Path>, settings: &LinterSettings) -> Re
|
|||
|
||||
#[cfg(not(fuzzing))]
|
||||
pub(crate) struct TestedNotebook {
|
||||
pub(crate) messages: Vec<Message>,
|
||||
pub(crate) diagnostics: Vec<OldDiagnostic>,
|
||||
pub(crate) source_notebook: Notebook,
|
||||
pub(crate) linted_notebook: Notebook,
|
||||
}
|
||||
|
@ -77,14 +80,14 @@ pub(crate) fn assert_notebook_path(
|
|||
);
|
||||
|
||||
Ok(TestedNotebook {
|
||||
messages,
|
||||
diagnostics: messages,
|
||||
source_notebook: source_kind.expect_ipy_notebook(),
|
||||
linted_notebook,
|
||||
})
|
||||
}
|
||||
|
||||
/// Run [`check_path`] on a snippet of Python code.
|
||||
pub fn test_snippet(contents: &str, settings: &LinterSettings) -> Vec<Message> {
|
||||
pub fn test_snippet(contents: &str, settings: &LinterSettings) -> Vec<OldDiagnostic> {
|
||||
let path = Path::new("<filename>");
|
||||
let contents = dedent(contents);
|
||||
test_contents(&SourceKind::Python(contents.into_owned()), path, settings).0
|
||||
|
@ -108,7 +111,7 @@ pub(crate) fn test_contents<'a>(
|
|||
source_kind: &'a SourceKind,
|
||||
path: &Path,
|
||||
settings: &LinterSettings,
|
||||
) -> (Vec<Message>, Cow<'a, SourceKind>) {
|
||||
) -> (Vec<OldDiagnostic>, Cow<'a, SourceKind>) {
|
||||
let source_type = PySourceType::from(path);
|
||||
let target_version = settings.resolve_target_version(path);
|
||||
let options =
|
||||
|
@ -288,7 +291,7 @@ Either ensure you always emit a fix or change `Violation::FIX_AVAILABILITY` to e
|
|||
diagnostic
|
||||
})
|
||||
.chain(parsed.errors().iter().map(|parse_error| {
|
||||
Message::from_parse_error(parse_error, &locator, source_code.clone())
|
||||
OldDiagnostic::from_parse_error(parse_error, &locator, source_code.clone())
|
||||
}))
|
||||
.sorted()
|
||||
.collect();
|
||||
|
@ -306,7 +309,9 @@ fn print_syntax_errors(
|
|||
|
||||
let messages: Vec<_> = errors
|
||||
.iter()
|
||||
.map(|parse_error| Message::from_parse_error(parse_error, locator, source_file.clone()))
|
||||
.map(|parse_error| {
|
||||
OldDiagnostic::from_parse_error(parse_error, locator, source_file.clone())
|
||||
})
|
||||
.collect();
|
||||
|
||||
if let Some(notebook) = source.as_ipy_notebook() {
|
||||
|
@ -317,18 +322,22 @@ fn print_syntax_errors(
|
|||
}
|
||||
|
||||
/// Print the [`Message::Diagnostic`]s in `messages`.
|
||||
fn print_diagnostics(mut messages: Vec<Message>, path: &Path, source: &SourceKind) -> String {
|
||||
messages.retain(|msg| !msg.is_syntax_error());
|
||||
fn print_diagnostics(
|
||||
mut diagnostics: Vec<OldDiagnostic>,
|
||||
path: &Path,
|
||||
source: &SourceKind,
|
||||
) -> String {
|
||||
diagnostics.retain(|msg| !msg.is_syntax_error());
|
||||
|
||||
if let Some(notebook) = source.as_ipy_notebook() {
|
||||
print_jupyter_messages(&messages, path, notebook)
|
||||
print_jupyter_messages(&diagnostics, path, notebook)
|
||||
} else {
|
||||
print_messages(&messages)
|
||||
print_messages(&diagnostics)
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn print_jupyter_messages(
|
||||
messages: &[Message],
|
||||
diagnostics: &[OldDiagnostic],
|
||||
path: &Path,
|
||||
notebook: &Notebook,
|
||||
) -> String {
|
||||
|
@ -341,7 +350,7 @@ pub(crate) fn print_jupyter_messages(
|
|||
.with_unsafe_fixes(UnsafeFixes::Enabled)
|
||||
.emit(
|
||||
&mut output,
|
||||
messages,
|
||||
diagnostics,
|
||||
&EmitterContext::new(&FxHashMap::from_iter([(
|
||||
path.file_name().unwrap().to_string_lossy().to_string(),
|
||||
notebook.index().clone(),
|
||||
|
@ -352,7 +361,7 @@ pub(crate) fn print_jupyter_messages(
|
|||
String::from_utf8(output).unwrap()
|
||||
}
|
||||
|
||||
pub(crate) fn print_messages(messages: &[Message]) -> String {
|
||||
pub(crate) fn print_messages(diagnostics: &[OldDiagnostic]) -> String {
|
||||
let mut output = Vec::new();
|
||||
|
||||
TextEmitter::default()
|
||||
|
@ -362,7 +371,7 @@ pub(crate) fn print_messages(messages: &[Message]) -> String {
|
|||
.with_unsafe_fixes(UnsafeFixes::Enabled)
|
||||
.emit(
|
||||
&mut output,
|
||||
messages,
|
||||
diagnostics,
|
||||
&EmitterContext::new(&FxHashMap::default()),
|
||||
)
|
||||
.unwrap();
|
||||
|
@ -371,7 +380,7 @@ pub(crate) fn print_messages(messages: &[Message]) -> String {
|
|||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! assert_messages {
|
||||
macro_rules! assert_diagnostics {
|
||||
($value:expr, $path:expr, $notebook:expr) => {{
|
||||
insta::with_settings!({ omit_expression => true }, {
|
||||
insta::assert_snapshot!(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue