mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 14:21:24 +00:00
[ty] Tell the user why we inferred a certain Python version when reporting version-specific syntax errors (#18295)
This commit is contained in:
parent
0a11baf29c
commit
6453ac9ea1
9 changed files with 201 additions and 107 deletions
51
crates/ty_python_semantic/src/util/diagnostics.rs
Normal file
51
crates/ty_python_semantic/src/util/diagnostics.rs
Normal file
|
@ -0,0 +1,51 @@
|
|||
use crate::{Db, Program, PythonVersionWithSource};
|
||||
use ruff_db::{
|
||||
diagnostic::{Annotation, Diagnostic, Severity, Span, SubDiagnostic},
|
||||
files::system_path_to_file,
|
||||
};
|
||||
|
||||
/// Add a subdiagnostic to `diagnostic` that explains why a certain Python version was inferred.
|
||||
///
|
||||
/// ty can infer the Python version from various sources, such as command-line arguments,
|
||||
/// configuration files, or defaults.
|
||||
pub fn add_inferred_python_version_hint_to_diagnostic(
|
||||
db: &dyn Db,
|
||||
diagnostic: &mut Diagnostic,
|
||||
action: &str,
|
||||
) {
|
||||
let program = Program::get(db);
|
||||
let PythonVersionWithSource { version, source } = program.python_version_with_source(db);
|
||||
|
||||
match source {
|
||||
crate::PythonVersionSource::Cli => {
|
||||
diagnostic.info(format_args!(
|
||||
"Python {version} was assumed when {action} because it was specified on the command line",
|
||||
));
|
||||
}
|
||||
crate::PythonVersionSource::File(path, range) => {
|
||||
if let Ok(file) = system_path_to_file(db.upcast(), &**path) {
|
||||
let mut sub_diagnostic = SubDiagnostic::new(
|
||||
Severity::Info,
|
||||
format_args!("Python {version} was assumed when {action}"),
|
||||
);
|
||||
sub_diagnostic.annotate(
|
||||
Annotation::primary(Span::from(file).with_optional_range(*range)).message(
|
||||
format_args!("Python {version} assumed due to this configuration setting"),
|
||||
),
|
||||
);
|
||||
diagnostic.sub(sub_diagnostic);
|
||||
} else {
|
||||
diagnostic.info(format_args!(
|
||||
"Python {version} was assumed when {action} because of your configuration file(s)",
|
||||
));
|
||||
}
|
||||
}
|
||||
crate::PythonVersionSource::Default => {
|
||||
diagnostic.info(format_args!(
|
||||
"Python {version} was assumed when {action} \
|
||||
because it is the newest Python version supported by ty, \
|
||||
and neither a command-line argument nor a configuration setting was provided",
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1 +1,2 @@
|
|||
pub(crate) mod diagnostics;
|
||||
pub(crate) mod subscript;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue