Report Python versions in pyvenv.cfg version mismatch (#13027)
When working on #13025 I noticed this message was lacking versions, which seems frustrating if you're debugging things. I refactored the general `matches_interpreter` utilities that were added in https://github.com/astral-sh/uv/pull/12884 into a more purpose-fit function that returns an `Option` with the versions if there's a mismatch.
This commit is contained in:
@@ -10,6 +10,7 @@ use tracing::debug;
|
||||
use uv_cache::Cache;
|
||||
use uv_cache_key::cache_digest;
|
||||
use uv_fs::{LockedFile, Simplified};
|
||||
use uv_pep440::Version;
|
||||
|
||||
use crate::discovery::find_python_installation;
|
||||
use crate::installation::PythonInstallation;
|
||||
@@ -356,12 +357,22 @@ impl PythonEnvironment {
|
||||
}
|
||||
}
|
||||
|
||||
/// If this is a virtual environment (indicated by the presence of
|
||||
/// a `pyvenv.cfg` file), this returns true if the `pyvenv.cfg` version
|
||||
/// is the same as the interpreter Python version. Also returns true
|
||||
/// if this is not a virtual environment.
|
||||
pub fn matches_interpreter(&self, interpreter: &Interpreter) -> bool {
|
||||
let Ok(cfg) = self.cfg() else { return true };
|
||||
cfg.matches_interpreter(interpreter)
|
||||
/// Check if the `pyvenv.cfg` version is the same as the interpreter's Python version.
|
||||
///
|
||||
/// Returns [`None`] if the versions are the consistent or there is no `pyvenv.cfg`. If the
|
||||
/// versions do not match, returns a tuple of the `pyvenv.cfg` and interpreter's Python versions
|
||||
/// for display.
|
||||
pub fn get_pyvenv_version_conflict(&self) -> Option<(Version, Version)> {
|
||||
let cfg = self.cfg().ok()?;
|
||||
let cfg_version = cfg.version?.into_version();
|
||||
|
||||
// Determine if we should be checking for patch-level equality
|
||||
let exe_version = if cfg_version.release().get(2).is_none() {
|
||||
self.interpreter().python_minor_version()
|
||||
} else {
|
||||
self.interpreter().python_patch_version()
|
||||
};
|
||||
|
||||
(cfg_version != exe_version).then_some((cfg_version, exe_version))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user