Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions crates/ruff/tests/analyze_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,6 @@ fn venv() -> Result<()> {

----- stderr -----
ruff failed
Cause: Invalid search path settings
Cause: Failed to discover the site-packages directory
Comment on lines -569 to -570
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could try to add those again but I actually don't find them very useful (the message on line 571 contains everything the user needs to know)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I agree, it seems nicer this way. More concise but no less information.

Cause: Invalid `--python` argument `none`: does not point to a Python executable or a directory on disk
Cause: No such file or directory (os error 2)
");
Expand Down
14 changes: 9 additions & 5 deletions crates/ruff_graph/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use ruff_db::{Db as SourceDb, Upcast};
use ruff_python_ast::PythonVersion;
use ty_python_semantic::lint::{LintRegistry, RuleSelection};
use ty_python_semantic::{
Db, Program, ProgramSettings, PythonEnvironmentPath, PythonPlatform, PythonVersionSource,
Db, Program, ProgramSettings, PythonEnvironment, PythonPlatform, PythonVersionSource,
PythonVersionWithSource, SearchPathSettings, SysPrefixPathOrigin, default_lint_registry,
};

Expand All @@ -35,13 +35,17 @@ impl ModuleDb {
python_version: PythonVersion,
venv_path: Option<SystemPathBuf>,
) -> Result<Self> {
let db = Self::default();
let mut search_paths = SearchPathSettings::new(src_roots);
// TODO: Consider setting `PythonPath::Auto` if no venv_path is provided.
// TODO: Consider calling `PythonEnvironment::discover` if the `venv_path` is not provided.
if let Some(venv_path) = venv_path {
search_paths.python_environment =
PythonEnvironmentPath::explicit(venv_path, SysPrefixPathOrigin::PythonCliFlag);
let environment =
PythonEnvironment::new(venv_path, SysPrefixPathOrigin::PythonCliFlag, db.system())?;
search_paths.site_packages_paths = environment
.site_packages_paths(db.system())
.context("Failed to discover the site-packages directory")?
.into_vec();
}
let db = Self::default();
let search_paths = search_paths
.to_search_paths(db.system(), db.vendored())
.context("Invalid search path settings")?;
Expand Down
3 changes: 0 additions & 3 deletions crates/ty/tests/cli/python_environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,6 @@ fn python_cli_argument_virtual_environment() -> anyhow::Result<()> {
----- stderr -----
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
ty failed
Cause: Failed to discover the site-packages directory
Cause: Invalid `--python` argument `<temp_dir>/my-venv/foo/some_other_file.txt`: does not point to a Python executable or a directory on disk
");

Expand All @@ -603,7 +602,6 @@ fn python_cli_argument_virtual_environment() -> anyhow::Result<()> {
----- stderr -----
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
ty failed
Cause: Failed to discover the site-packages directory
Cause: Invalid `--python` argument `<temp_dir>/not-a-directory-or-executable`: does not point to a Python executable or a directory on disk
Cause: No such file or directory (os error 2)
");
Expand Down Expand Up @@ -686,7 +684,6 @@ fn config_file_broken_python_setting() -> anyhow::Result<()> {
----- stderr -----
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
ty failed
Cause: Failed to discover the site-packages directory
Cause: Invalid `environment.python` setting

--> Invalid setting in configuration file `<temp_dir>/pyproject.toml`
Expand Down
3 changes: 1 addition & 2 deletions crates/ty_project/src/combine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{collections::HashMap, hash::BuildHasher};
use ordermap::OrderMap;
use ruff_db::system::SystemPathBuf;
use ruff_python_ast::PythonVersion;
use ty_python_semantic::{PythonEnvironmentPath, PythonPlatform};
use ty_python_semantic::PythonPlatform;

/// Combine two values, preferring the values in `self`.
///
Expand Down Expand Up @@ -141,7 +141,6 @@ macro_rules! impl_noop_combine {

impl_noop_combine!(SystemPathBuf);
impl_noop_combine!(PythonPlatform);
impl_noop_combine!(PythonEnvironmentPath);
impl_noop_combine!(PythonVersion);

// std types
Expand Down
68 changes: 45 additions & 23 deletions crates/ty_project/src/metadata/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::settings::{Override, Settings, TerminalSettings};
use crate::metadata::value::{
RangedValue, RelativeGlobPattern, RelativePathBuf, ValueSource, ValueSourceGuard,
};
use anyhow::Context;
use ordermap::OrderMap;
use ruff_db::RustDoc;
use ruff_db::diagnostic::{
Expand All @@ -29,9 +30,9 @@ use std::sync::Arc;
use thiserror::Error;
use ty_python_semantic::lint::{GetLintError, Level, LintSource, RuleSelection};
use ty_python_semantic::{
ProgramSettings, PythonEnvironmentPath, PythonPlatform, PythonVersionFileSource,
ProgramSettings, PythonEnvironment, PythonPlatform, PythonVersionFileSource,
PythonVersionSource, PythonVersionWithSource, SearchPathSettings, SearchPathValidationError,
SearchPaths, SysPrefixPathOrigin,
SearchPaths, SitePackagesPaths, SysPrefixPathOrigin,
};

#[derive(
Expand Down Expand Up @@ -133,16 +134,52 @@ impl Options {
default
});

let search_paths = self.to_search_paths(project_root, project_name, system, vendored)?;
let python_environment = if let Some(python_path) = environment.python.as_ref() {
let origin = match python_path.source() {
ValueSource::Cli => SysPrefixPathOrigin::PythonCliFlag,
ValueSource::File(path) => {
SysPrefixPathOrigin::ConfigFileSetting(path.clone(), python_path.range())
}
};

Some(PythonEnvironment::new(
python_path.absolute(project_root, system),
origin,
system,
)?)
} else {
PythonEnvironment::discover(project_root, system)
.context("Failed to discover local Python environment")?
};

let site_packages_paths = if let Some(python_environment) = python_environment.as_ref() {
python_environment
.site_packages_paths(system)
.context("Failed to discover the site-packages directory")?
} else {
tracing::debug!("No virtual environment found");

SitePackagesPaths::default()
};

let python_version = options_python_version
.or_else(|| {
search_paths
.try_resolve_installation_python_version()
.map(Cow::into_owned)
python_environment
.as_ref()?
.python_version_from_metadata()
.cloned()
})
.or_else(|| site_packages_paths.python_version_from_layout())
.unwrap_or_default();

let search_paths = self.to_search_paths(
project_root,
project_name,
site_packages_paths,
system,
vendored,
)?;

tracing::info!(
"Python version: Python {python_version}, platform: {python_platform}",
python_version = python_version.version
Expand All @@ -159,6 +196,7 @@ impl Options {
&self,
project_root: &SystemPath,
project_name: &str,
site_packages_paths: SitePackagesPaths,
system: &dyn System,
vendored: &VendoredFileSystem,
) -> Result<SearchPaths, SearchPathValidationError> {
Expand Down Expand Up @@ -230,23 +268,7 @@ impl Options {
.typeshed
.as_ref()
.map(|path| path.absolute(project_root, system)),
python_environment: environment
.python
.as_ref()
.map(|python_path| {
let origin = match python_path.source() {
ValueSource::Cli => SysPrefixPathOrigin::PythonCliFlag,
ValueSource::File(path) => SysPrefixPathOrigin::ConfigFileSetting(
path.clone(),
python_path.range(),
),
};
PythonEnvironmentPath::explicit(
python_path.absolute(project_root, system),
origin,
)
})
.unwrap_or_else(|| PythonEnvironmentPath::Discover(project_root.to_path_buf())),
site_packages_paths: site_packages_paths.into_vec(),
};

settings.to_search_paths(system, vendored)
Expand Down
4 changes: 2 additions & 2 deletions crates/ty_python_semantic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ pub use module_resolver::{
system_module_search_paths,
};
pub use program::{
Program, ProgramSettings, PythonEnvironmentPath, PythonVersionFileSource, PythonVersionSource,
Program, ProgramSettings, PythonVersionFileSource, PythonVersionSource,
PythonVersionWithSource, SearchPathSettings,
};
pub use python_platform::PythonPlatform;
pub use semantic_model::{HasType, SemanticModel};
pub use site_packages::SysPrefixPathOrigin;
pub use site_packages::{PythonEnvironment, SitePackagesPaths, SysPrefixPathOrigin};
pub use util::diagnostics::add_inferred_python_version_hint_to_diagnostic;

pub mod ast_node_ref;
Expand Down
Loading
Loading