[ty] Resolve python environment in Options::to_program_settings (#18960)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
This commit is contained in:
Micha Reiser 2025-06-26 17:57:16 +02:00 committed by GitHub
parent d00697621e
commit 1dcdf7f41d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 208 additions and 277 deletions

View file

@ -1,8 +1,8 @@
use std::sync::Arc;
use crate::Db;
use crate::module_resolver::{SearchPathValidationError, SearchPaths};
use crate::python_platform::PythonPlatform;
use crate::{Db, SysPrefixPathOrigin};
use ruff_db::diagnostic::Span;
use ruff_db::files::system_path_to_file;
@ -173,9 +173,8 @@ pub struct SearchPathSettings {
/// bundled as a zip file in the binary
pub custom_typeshed: Option<SystemPathBuf>,
/// Path to the Python environment from which ty resolves third party dependencies
/// and their type information.
pub python_environment: PythonEnvironmentPath,
/// List of site packages paths to use.
pub site_packages_paths: Vec<SystemPathBuf>,
}
impl SearchPathSettings {
@ -191,7 +190,7 @@ impl SearchPathSettings {
src_roots: vec![],
extra_paths: vec![],
custom_typeshed: None,
python_environment: PythonEnvironmentPath::Testing(vec![]),
site_packages_paths: vec![],
}
}
@ -203,43 +202,3 @@ impl SearchPathSettings {
SearchPaths::from_settings(self, system, vendored)
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum PythonEnvironmentPath {
/// The path to the Python environment isn't known. Try to discover the Python environment
/// by inspecting environment variables, the project structure, etc. and derive the path from it.
///
/// The path is the project root in which to search for a Python environment.
Discover(SystemPathBuf),
/// Path to a Python environment that is explicitly specified.
///
/// The path that either represents the value of [`sys.prefix`] at runtime in Python
/// for a given Python executable, or which represents a path relative to `sys.prefix`
/// that we will attempt later to resolve into `sys.prefix`. Exactly which this variant
/// represents depends on the [`SysPrefixPathOrigin`] element in the tuple.
///
/// For the case of a virtual environment, where a
/// Python binary is at `/.venv/bin/python`, `sys.prefix` is the path to
/// the virtual environment the Python binary lies inside, i.e. `/.venv`,
/// and `site-packages` will be at `.venv/lib/python3.X/site-packages`.
/// System Python installations generally work the same way: if a system
/// Python installation lies at `/opt/homebrew/bin/python`, `sys.prefix`
/// will be `/opt/homebrew`, and `site-packages` will be at
/// `/opt/homebrew/lib/python3.X/site-packages`.
///
/// [`sys.prefix`]: https://docs.python.org/3/library/sys.html#sys.prefix
Explicit(SystemPathBuf, SysPrefixPathOrigin),
/// Don't search for a Python environment, instead use the provided site packages paths.
///
/// This variant is mainly intended for testing where we want to skip resolving `site-packages`
/// because it would unnecessarily complicate the test setup.
Testing(Vec<SystemPathBuf>),
}
impl PythonEnvironmentPath {
pub fn explicit(path: impl Into<SystemPathBuf>, origin: SysPrefixPathOrigin) -> Self {
Self::Explicit(path.into(), origin)
}
}