Skip to content

Commit 11654aa

Browse files
committed
[ty] Minor cleanup for site-packages discovery logic (astral-sh#18446)
1 parent 8485dbb commit 11654aa

File tree

5 files changed

+28
-9
lines changed

5 files changed

+28
-9
lines changed

crates/ty/src/args.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ impl CheckCommand {
175175
python_platform: self
176176
.python_platform
177177
.map(|platform| RangedValue::cli(platform.into())),
178-
python: self.python.map(RelativePathBuf::cli),
178+
python: self
179+
.python
180+
.map(|python| RangedValue::cli(RelativePathBuf::cli(python))),
179181
typeshed: self.typeshed.map(RelativePathBuf::cli),
180182
extra_paths: self.extra_search_path.map(|extra_search_paths| {
181183
extra_search_paths

crates/ty/tests/cli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,7 @@ fn config_file_broken_python_setting() -> anyhow::Result<()> {
10621062
WARN ty is pre-release software and not ready for production use. Expect to encounter bugs, missing features, and fatal errors.
10631063
ty failed
10641064
Cause: Invalid search path settings
1065-
Cause: Failed to discover the site-packages directory: Invalid `--python` argument: `<temp_dir>/not-a-directory-or-executable` does not point to a Python executable or a directory on disk
1065+
Cause: Failed to discover the site-packages directory: Invalid `environment.python` setting in your configuration file: `<temp_dir>/not-a-directory-or-executable` does not point to a Python executable or a directory on disk
10661066
");
10671067

10681068
Ok(())

crates/ty_project/src/metadata/options.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,11 @@ impl Options {
184184
.map(|python_path| {
185185
PythonPath::sys_prefix(
186186
python_path.absolute(project_root, system),
187-
SysPrefixPathOrigin::PythonCliFlag,
187+
if python_path.source().is_cli() {
188+
SysPrefixPathOrigin::PythonCliFlag
189+
} else {
190+
SysPrefixPathOrigin::ConfigFileSetting
191+
},
188192
)
189193
})
190194
.or_else(|| {
@@ -193,8 +197,8 @@ impl Options {
193197
})
194198
})
195199
.or_else(|| {
196-
std::env::var("CONDA_PREFIX").ok().map(|path| {
197-
PythonPath::sys_prefix(path, SysPrefixPathOrigin::CondaPrefixVar)
200+
std::env::var("CONDA_PREFIX").ok().map(|conda_prefix| {
201+
PythonPath::sys_prefix(conda_prefix, SysPrefixPathOrigin::CondaPrefixVar)
198202
})
199203
})
200204
.unwrap_or_else(|| {
@@ -387,7 +391,7 @@ pub struct EnvironmentOptions {
387391
python = "./.venv"
388392
"#
389393
)]
390-
pub python: Option<RelativePathBuf>,
394+
pub python: Option<RangedValue<RelativePathBuf>>,
391395
}
392396

393397
#[derive(

crates/ty_project/src/metadata/value.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ impl ValueSource {
3232
ValueSource::Cli => None,
3333
}
3434
}
35+
36+
pub const fn is_cli(&self) -> bool {
37+
matches!(self, ValueSource::Cli)
38+
}
3539
}
3640

3741
thread_local! {

crates/ty_python_semantic/src/site_packages.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,8 @@ impl fmt::Display for SysPrefixPath {
857857
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
858858
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
859859
pub enum SysPrefixPathOrigin {
860+
/// The `sys.prefix` path came from a configuration file setting: `pyproject.toml` or `ty.toml`
861+
ConfigFileSetting,
860862
/// The `sys.prefix` path came from a `--python` CLI flag
861863
PythonCliFlag,
862864
/// The `sys.prefix` path came from the `VIRTUAL_ENV` environment variable
@@ -878,7 +880,10 @@ impl SysPrefixPathOrigin {
878880
pub(crate) const fn must_be_virtual_env(self) -> bool {
879881
match self {
880882
Self::LocalVenv | Self::VirtualEnvVar => true,
881-
Self::PythonCliFlag | Self::DerivedFromPyvenvCfg | Self::CondaPrefixVar => false,
883+
Self::ConfigFileSetting
884+
| Self::PythonCliFlag
885+
| Self::DerivedFromPyvenvCfg
886+
| Self::CondaPrefixVar => false,
882887
}
883888
}
884889

@@ -888,7 +893,7 @@ impl SysPrefixPathOrigin {
888893
/// the `sys.prefix` directory, e.g. the `--python` CLI flag.
889894
pub(crate) const fn must_point_directly_to_sys_prefix(self) -> bool {
890895
match self {
891-
Self::PythonCliFlag => false,
896+
Self::PythonCliFlag | Self::ConfigFileSetting => false,
892897
Self::VirtualEnvVar
893898
| Self::CondaPrefixVar
894899
| Self::DerivedFromPyvenvCfg
@@ -901,6 +906,9 @@ impl Display for SysPrefixPathOrigin {
901906
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
902907
match self {
903908
Self::PythonCliFlag => f.write_str("`--python` argument"),
909+
Self::ConfigFileSetting => {
910+
f.write_str("`environment.python` setting in your configuration file")
911+
}
904912
Self::VirtualEnvVar => f.write_str("`VIRTUAL_ENV` environment variable"),
905913
Self::CondaPrefixVar => f.write_str("`CONDA_PREFIX` environment variable"),
906914
Self::DerivedFromPyvenvCfg => f.write_str("derived `sys.prefix` path"),
@@ -1460,8 +1468,9 @@ mod tests {
14601468
}
14611469

14621470
#[test]
1463-
fn reject_env_that_is_not_a_directory() {
1471+
fn reject_configured_env_that_is_not_a_directory_or_executable() {
14641472
let system = TestSystem::default();
1473+
14651474
system
14661475
.memory_file_system()
14671476
.write_file_all("/env", "")

0 commit comments

Comments
 (0)