[ty] Use python version and path from Python extension (#19012)

This commit is contained in:
Micha Reiser 2025-07-14 11:47:27 +02:00 committed by GitHub
parent 26f736bc46
commit 90026047f9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
22 changed files with 344 additions and 99 deletions

View file

@ -113,6 +113,9 @@ pub enum PythonVersionSource {
/// long argument (`--extra-paths`) or `--config key=value`.
Cli,
/// The value comes from the Python VS Code extension (the selected interpreter).
PythonVSCodeExtension,
/// We fell back to a default value because the value was not specified via the CLI or a config file.
#[default]
Default,

View file

@ -1065,6 +1065,8 @@ pub enum SysPrefixPathOrigin {
ConfigFileSetting(Arc<SystemPathBuf>, Option<TextRange>),
/// The `sys.prefix` path came from a `--python` CLI flag
PythonCliFlag,
/// The selected interpreter in the VS Code's Python extension.
PythonVSCodeExtension,
/// The `sys.prefix` path came from the `VIRTUAL_ENV` environment variable
VirtualEnvVar,
/// The `sys.prefix` path came from the `CONDA_PREFIX` environment variable
@ -1086,6 +1088,7 @@ impl SysPrefixPathOrigin {
Self::LocalVenv | Self::VirtualEnvVar => true,
Self::ConfigFileSetting(..)
| Self::PythonCliFlag
| Self::PythonVSCodeExtension
| Self::DerivedFromPyvenvCfg
| Self::CondaPrefixVar => false,
}
@ -1097,7 +1100,9 @@ impl SysPrefixPathOrigin {
/// the `sys.prefix` directory, e.g. the `--python` CLI flag.
pub(crate) const fn must_point_directly_to_sys_prefix(&self) -> bool {
match self {
Self::PythonCliFlag | Self::ConfigFileSetting(..) => false,
Self::PythonCliFlag | Self::ConfigFileSetting(..) | Self::PythonVSCodeExtension => {
false
}
Self::VirtualEnvVar
| Self::CondaPrefixVar
| Self::DerivedFromPyvenvCfg
@ -1115,6 +1120,9 @@ impl std::fmt::Display for SysPrefixPathOrigin {
Self::CondaPrefixVar => f.write_str("`CONDA_PREFIX` environment variable"),
Self::DerivedFromPyvenvCfg => f.write_str("derived `sys.prefix` path"),
Self::LocalVenv => f.write_str("local virtual environment"),
Self::PythonVSCodeExtension => {
f.write_str("selected interpreter in the VS Code Python extension")
}
}
}
}

View file

@ -62,6 +62,12 @@ pub fn add_inferred_python_version_hint_to_diagnostic(
or in a configuration file",
);
}
crate::PythonVersionSource::PythonVSCodeExtension => {
diagnostic.info(format_args!(
"Python {version} was assumed when {action} \
because it's the version of the selected Python interpreter in the VS Code Python extension",
));
}
crate::PythonVersionSource::InstallationDirectoryLayout {
site_packages_parent_dir,
} => {