mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-14 07:38:21 +00:00
[ty] Infer the Python version from --python=<system installation>
on Unix (#18550)
Some checks are pending
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 / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (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 (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
Some checks are pending
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 / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (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 (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
This commit is contained in:
parent
a863000cbc
commit
e84406d8be
13 changed files with 334 additions and 137 deletions
|
@ -1,3 +1,4 @@
|
|||
use std::borrow::Cow;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::Db;
|
||||
|
@ -38,7 +39,7 @@ impl Program {
|
|||
.with_context(|| "Invalid search path settings")?;
|
||||
|
||||
let python_version_with_source =
|
||||
Self::resolve_python_version(python_version_with_source, search_paths.python_version());
|
||||
Self::resolve_python_version(python_version_with_source, &search_paths);
|
||||
|
||||
tracing::info!(
|
||||
"Python version: Python {python_version}, platform: {python_platform}",
|
||||
|
@ -58,10 +59,14 @@ impl Program {
|
|||
|
||||
fn resolve_python_version(
|
||||
config_value: Option<PythonVersionWithSource>,
|
||||
environment_value: Option<&PythonVersionWithSource>,
|
||||
search_paths: &SearchPaths,
|
||||
) -> PythonVersionWithSource {
|
||||
config_value
|
||||
.or_else(|| environment_value.cloned())
|
||||
.or_else(|| {
|
||||
search_paths
|
||||
.try_resolve_installation_python_version()
|
||||
.map(Cow::into_owned)
|
||||
})
|
||||
.unwrap_or_default()
|
||||
}
|
||||
|
||||
|
@ -79,7 +84,7 @@ impl Program {
|
|||
let search_paths = SearchPaths::from_settings(db, &search_paths)?;
|
||||
|
||||
let new_python_version =
|
||||
Self::resolve_python_version(python_version_with_source, search_paths.python_version());
|
||||
Self::resolve_python_version(python_version_with_source, &search_paths);
|
||||
|
||||
if self.search_paths(db) != &search_paths {
|
||||
tracing::debug!("Updating search paths");
|
||||
|
@ -112,8 +117,11 @@ impl Program {
|
|||
let search_paths = SearchPaths::from_settings(db, search_path_settings)?;
|
||||
|
||||
let current_python_version = self.python_version_with_source(db);
|
||||
let python_version_from_environment =
|
||||
search_paths.python_version().cloned().unwrap_or_default();
|
||||
|
||||
let python_version_from_environment = search_paths
|
||||
.try_resolve_installation_python_version()
|
||||
.map(Cow::into_owned)
|
||||
.unwrap_or_default();
|
||||
|
||||
if current_python_version != &python_version_from_environment
|
||||
&& current_python_version.source.priority()
|
||||
|
@ -153,6 +161,13 @@ pub enum PythonVersionSource {
|
|||
/// The virtual environment might have been configured, activated or inferred.
|
||||
PyvenvCfgFile(PythonVersionFileSource),
|
||||
|
||||
/// Value inferred from the layout of the Python installation.
|
||||
///
|
||||
/// This only ever applies on Unix. On Unix, the `site-packages` directory
|
||||
/// will always be at `sys.prefix/lib/pythonX.Y/site-packages`,
|
||||
/// so we can infer the Python version from the parent directory of `site-packages`.
|
||||
InstallationDirectoryLayout { site_packages_parent_dir: Box<str> },
|
||||
|
||||
/// The value comes from a CLI argument, while it's left open if specified using a short argument,
|
||||
/// long argument (`--extra-paths`) or `--config key=value`.
|
||||
Cli,
|
||||
|
@ -169,22 +184,26 @@ impl PythonVersionSource {
|
|||
PythonVersionSource::PyvenvCfgFile(_) => PythonSourcePriority::PyvenvCfgFile,
|
||||
PythonVersionSource::ConfigFile(_) => PythonSourcePriority::ConfigFile,
|
||||
PythonVersionSource::Cli => PythonSourcePriority::Cli,
|
||||
PythonVersionSource::InstallationDirectoryLayout { .. } => {
|
||||
PythonSourcePriority::InstallationDirectoryLayout
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// The priority in which Python version sources are considered.
|
||||
/// A higher value means a higher priority.
|
||||
/// The lower down the variant appears in this enum, the higher its priority.
|
||||
///
|
||||
/// For example, if a Python version is specified in a pyproject.toml file
|
||||
/// but *also* via a CLI argument, the CLI argument will take precedence.
|
||||
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord)]
|
||||
#[cfg_attr(test, derive(strum_macros::EnumIter))]
|
||||
enum PythonSourcePriority {
|
||||
Default = 0,
|
||||
PyvenvCfgFile = 1,
|
||||
ConfigFile = 2,
|
||||
Cli = 3,
|
||||
Default,
|
||||
InstallationDirectoryLayout,
|
||||
PyvenvCfgFile,
|
||||
ConfigFile,
|
||||
Cli,
|
||||
}
|
||||
|
||||
/// Information regarding the file and [`TextRange`] of the configuration
|
||||
|
@ -312,7 +331,9 @@ mod tests {
|
|||
match other {
|
||||
PythonSourcePriority::Cli => assert!(other > priority, "{other:?}"),
|
||||
PythonSourcePriority::ConfigFile => assert_eq!(priority, other),
|
||||
PythonSourcePriority::PyvenvCfgFile | PythonSourcePriority::Default => {
|
||||
PythonSourcePriority::PyvenvCfgFile
|
||||
| PythonSourcePriority::Default
|
||||
| PythonSourcePriority::InstallationDirectoryLayout => {
|
||||
assert!(priority > other, "{other:?}");
|
||||
}
|
||||
}
|
||||
|
@ -327,6 +348,24 @@ mod tests {
|
|||
assert!(other > priority, "{other:?}");
|
||||
}
|
||||
PythonSourcePriority::PyvenvCfgFile => assert_eq!(priority, other),
|
||||
PythonSourcePriority::Default
|
||||
| PythonSourcePriority::InstallationDirectoryLayout => {
|
||||
assert!(priority > other, "{other:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
PythonSourcePriority::InstallationDirectoryLayout => {
|
||||
for other in PythonSourcePriority::iter() {
|
||||
match other {
|
||||
PythonSourcePriority::Cli
|
||||
| PythonSourcePriority::ConfigFile
|
||||
| PythonSourcePriority::PyvenvCfgFile => {
|
||||
assert!(other > priority, "{other:?}");
|
||||
}
|
||||
PythonSourcePriority::InstallationDirectoryLayout => {
|
||||
assert_eq!(priority, other);
|
||||
}
|
||||
PythonSourcePriority::Default => assert!(priority > other, "{other:?}"),
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue