mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-15 16:10:17 +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,8 +1,9 @@
|
|||
use std::borrow::Cow;
|
||||
use std::fmt;
|
||||
use std::iter::FusedIterator;
|
||||
use std::str::Split;
|
||||
use std::str::{FromStr, Split};
|
||||
|
||||
use camino::Utf8Component;
|
||||
use compact_str::format_compact;
|
||||
use rustc_hash::{FxBuildHasher, FxHashSet};
|
||||
|
||||
|
@ -15,7 +16,9 @@ use crate::db::Db;
|
|||
use crate::module_name::ModuleName;
|
||||
use crate::module_resolver::typeshed::{TypeshedVersions, vendored_typeshed_versions};
|
||||
use crate::site_packages::{PythonEnvironment, SitePackagesPaths, SysPrefixPathOrigin};
|
||||
use crate::{Program, PythonPath, PythonVersionWithSource, SearchPathSettings};
|
||||
use crate::{
|
||||
Program, PythonPath, PythonVersionSource, PythonVersionWithSource, SearchPathSettings,
|
||||
};
|
||||
|
||||
use super::module::{Module, ModuleKind};
|
||||
use super::path::{ModulePath, SearchPath, SearchPathValidationError};
|
||||
|
@ -155,10 +158,12 @@ pub struct SearchPaths {
|
|||
|
||||
typeshed_versions: TypeshedVersions,
|
||||
|
||||
/// The Python version for the search paths, if any.
|
||||
/// The Python version implied by the virtual environment.
|
||||
///
|
||||
/// This is read from the `pyvenv.cfg` if present.
|
||||
python_version: Option<PythonVersionWithSource>,
|
||||
/// If this environment was a system installation or the `pyvenv.cfg` file
|
||||
/// of the virtual environment did not contain a `version` or `version_info` key,
|
||||
/// this field will be `None`.
|
||||
python_version_from_pyvenv_cfg: Option<PythonVersionWithSource>,
|
||||
}
|
||||
|
||||
impl SearchPaths {
|
||||
|
@ -304,7 +309,7 @@ impl SearchPaths {
|
|||
static_paths,
|
||||
site_packages,
|
||||
typeshed_versions,
|
||||
python_version,
|
||||
python_version_from_pyvenv_cfg: python_version,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -330,8 +335,50 @@ impl SearchPaths {
|
|||
&self.typeshed_versions
|
||||
}
|
||||
|
||||
pub fn python_version(&self) -> Option<&PythonVersionWithSource> {
|
||||
self.python_version.as_ref()
|
||||
pub fn try_resolve_installation_python_version(&self) -> Option<Cow<PythonVersionWithSource>> {
|
||||
if let Some(version) = self.python_version_from_pyvenv_cfg.as_ref() {
|
||||
return Some(Cow::Borrowed(version));
|
||||
}
|
||||
|
||||
if cfg!(windows) {
|
||||
// The path to `site-packages` on Unix is
|
||||
// `<sys.prefix>/lib/pythonX.Y/site-packages`,
|
||||
// but on Windows it's `<sys.prefix>/Lib/site-packages`.
|
||||
return None;
|
||||
}
|
||||
|
||||
let primary_site_packages = self.site_packages.first()?.as_system_path()?;
|
||||
|
||||
let mut site_packages_ancestor_components =
|
||||
primary_site_packages.components().rev().skip(1).map(|c| {
|
||||
// This should have all been validated in `site_packages.rs`
|
||||
// when we resolved the search paths for the project.
|
||||
debug_assert!(
|
||||
matches!(c, Utf8Component::Normal(_)),
|
||||
"Unexpected component in site-packages path `{c:?}` \
|
||||
(expected `site-packages` to be an absolute path with symlinks resolved, \
|
||||
located at `<sys.prefix>/lib/pythonX.Y/site-packages`)"
|
||||
);
|
||||
|
||||
c.as_str()
|
||||
});
|
||||
|
||||
let parent_component = site_packages_ancestor_components.next()?;
|
||||
|
||||
if site_packages_ancestor_components.next()? != "lib" {
|
||||
return None;
|
||||
}
|
||||
|
||||
let version = parent_component
|
||||
.strip_prefix("python")
|
||||
.or_else(|| parent_component.strip_prefix("pypy"))?
|
||||
.trim_end_matches('t');
|
||||
|
||||
let version = PythonVersion::from_str(version).ok()?;
|
||||
let source = PythonVersionSource::InstallationDirectoryLayout {
|
||||
site_packages_parent_dir: Box::from(parent_component),
|
||||
};
|
||||
Some(Cow::Owned(PythonVersionWithSource { version, source }))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -351,7 +398,7 @@ pub(crate) fn dynamic_resolution_paths(db: &dyn Db) -> Vec<SearchPath> {
|
|||
static_paths,
|
||||
site_packages,
|
||||
typeshed_versions: _,
|
||||
python_version: _,
|
||||
python_version_from_pyvenv_cfg: _,
|
||||
} = Program::get(db).search_paths(db);
|
||||
|
||||
let mut dynamic_paths = Vec::new();
|
||||
|
|
|
@ -4,7 +4,7 @@ use std::num::{NonZeroU16, NonZeroUsize};
|
|||
use std::ops::{RangeFrom, RangeInclusive};
|
||||
use std::str::FromStr;
|
||||
|
||||
use ruff_python_ast::PythonVersion;
|
||||
use ruff_python_ast::{PythonVersion, PythonVersionDeserializationError};
|
||||
use rustc_hash::FxHashMap;
|
||||
|
||||
use crate::Program;
|
||||
|
@ -49,55 +49,26 @@ impl fmt::Display for TypeshedVersionsParseError {
|
|||
|
||||
impl std::error::Error for TypeshedVersionsParseError {
|
||||
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
||||
if let TypeshedVersionsParseErrorKind::IntegerParsingFailure { err, .. } = &self.reason {
|
||||
Some(err)
|
||||
if let TypeshedVersionsParseErrorKind::VersionParseError(err) = &self.reason {
|
||||
err.source()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq, Clone)]
|
||||
#[derive(Debug, PartialEq, Eq, Clone, thiserror::Error)]
|
||||
pub(crate) enum TypeshedVersionsParseErrorKind {
|
||||
#[error("File has too many lines ({0}); maximum allowed is {max_allowed}", max_allowed = NonZeroU16::MAX)]
|
||||
TooManyLines(NonZeroUsize),
|
||||
#[error("Expected every non-comment line to have exactly one colon")]
|
||||
UnexpectedNumberOfColons,
|
||||
#[error("Expected all components of '{0}' to be valid Python identifiers")]
|
||||
InvalidModuleName(String),
|
||||
#[error("Expected every non-comment line to have exactly one '-' character")]
|
||||
UnexpectedNumberOfHyphens,
|
||||
UnexpectedNumberOfPeriods(String),
|
||||
IntegerParsingFailure {
|
||||
version: String,
|
||||
err: std::num::ParseIntError,
|
||||
},
|
||||
}
|
||||
|
||||
impl fmt::Display for TypeshedVersionsParseErrorKind {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
match self {
|
||||
Self::TooManyLines(num_lines) => write!(
|
||||
f,
|
||||
"File has too many lines ({num_lines}); maximum allowed is {}",
|
||||
NonZeroU16::MAX
|
||||
),
|
||||
Self::UnexpectedNumberOfColons => {
|
||||
f.write_str("Expected every non-comment line to have exactly one colon")
|
||||
}
|
||||
Self::InvalidModuleName(name) => write!(
|
||||
f,
|
||||
"Expected all components of '{name}' to be valid Python identifiers"
|
||||
),
|
||||
Self::UnexpectedNumberOfHyphens => {
|
||||
f.write_str("Expected every non-comment line to have exactly one '-' character")
|
||||
}
|
||||
Self::UnexpectedNumberOfPeriods(format) => write!(
|
||||
f,
|
||||
"Expected all versions to be in the form {{MAJOR}}.{{MINOR}}; got '{format}'"
|
||||
),
|
||||
Self::IntegerParsingFailure { version, err } => write!(
|
||||
f,
|
||||
"Failed to convert '{version}' to a pair of integers due to {err}",
|
||||
),
|
||||
}
|
||||
}
|
||||
#[error("{0}")]
|
||||
VersionParseError(#[from] PythonVersionDeserializationError),
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
|
@ -304,12 +275,12 @@ impl FromStr for PyVersionRange {
|
|||
let mut parts = s.split('-').map(str::trim);
|
||||
match (parts.next(), parts.next(), parts.next()) {
|
||||
(Some(lower), Some(""), None) => {
|
||||
let lower = python_version_from_versions_file_string(lower)?;
|
||||
let lower = PythonVersion::from_str(lower)?;
|
||||
Ok(Self::AvailableFrom(lower..))
|
||||
}
|
||||
(Some(lower), Some(upper), None) => {
|
||||
let lower = python_version_from_versions_file_string(lower)?;
|
||||
let upper = python_version_from_versions_file_string(upper)?;
|
||||
let lower = PythonVersion::from_str(lower)?;
|
||||
let upper = PythonVersion::from_str(upper)?;
|
||||
Ok(Self::AvailableWithin(lower..=upper))
|
||||
}
|
||||
_ => Err(TypeshedVersionsParseErrorKind::UnexpectedNumberOfHyphens),
|
||||
|
@ -328,23 +299,6 @@ impl fmt::Display for PyVersionRange {
|
|||
}
|
||||
}
|
||||
|
||||
fn python_version_from_versions_file_string(
|
||||
s: &str,
|
||||
) -> Result<PythonVersion, TypeshedVersionsParseErrorKind> {
|
||||
let mut parts = s.split('.').map(str::trim);
|
||||
let (Some(major), Some(minor), None) = (parts.next(), parts.next(), parts.next()) else {
|
||||
return Err(TypeshedVersionsParseErrorKind::UnexpectedNumberOfPeriods(
|
||||
s.to_string(),
|
||||
));
|
||||
};
|
||||
PythonVersion::try_from((major, minor)).map_err(|int_parse_error| {
|
||||
TypeshedVersionsParseErrorKind::IntegerParsingFailure {
|
||||
version: s.to_string(),
|
||||
err: int_parse_error,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::fmt::Write as _;
|
||||
|
@ -681,15 +635,17 @@ foo: 3.8- # trailing comment
|
|||
TypeshedVersions::from_str("foo: 38-"),
|
||||
Err(TypeshedVersionsParseError {
|
||||
line_number: ONE,
|
||||
reason: TypeshedVersionsParseErrorKind::UnexpectedNumberOfPeriods("38".to_string())
|
||||
reason: TypeshedVersionsParseErrorKind::VersionParseError(
|
||||
PythonVersionDeserializationError::WrongPeriodNumber(Box::from("38"))
|
||||
)
|
||||
})
|
||||
);
|
||||
assert_eq!(
|
||||
TypeshedVersions::from_str("foo: 3..8-"),
|
||||
Err(TypeshedVersionsParseError {
|
||||
line_number: ONE,
|
||||
reason: TypeshedVersionsParseErrorKind::UnexpectedNumberOfPeriods(
|
||||
"3..8".to_string()
|
||||
reason: TypeshedVersionsParseErrorKind::VersionParseError(
|
||||
PythonVersionDeserializationError::WrongPeriodNumber(Box::from("3..8"))
|
||||
)
|
||||
})
|
||||
);
|
||||
|
@ -697,8 +653,8 @@ foo: 3.8- # trailing comment
|
|||
TypeshedVersions::from_str("foo: 3.8-3..11"),
|
||||
Err(TypeshedVersionsParseError {
|
||||
line_number: ONE,
|
||||
reason: TypeshedVersionsParseErrorKind::UnexpectedNumberOfPeriods(
|
||||
"3..11".to_string()
|
||||
reason: TypeshedVersionsParseErrorKind::VersionParseError(
|
||||
PythonVersionDeserializationError::WrongPeriodNumber(Box::from("3..11"))
|
||||
)
|
||||
})
|
||||
);
|
||||
|
@ -708,20 +664,30 @@ foo: 3.8- # trailing comment
|
|||
fn invalid_typeshed_versions_non_digits() {
|
||||
let err = TypeshedVersions::from_str("foo: 1.two-").unwrap_err();
|
||||
assert_eq!(err.line_number, ONE);
|
||||
let TypeshedVersionsParseErrorKind::IntegerParsingFailure { version, err } = err.reason
|
||||
let TypeshedVersionsParseErrorKind::VersionParseError(
|
||||
PythonVersionDeserializationError::InvalidMinorVersion(invalid_minor, parse_error),
|
||||
) = err.reason
|
||||
else {
|
||||
panic!()
|
||||
panic!(
|
||||
"Expected an invalid-minor-version parse error, got `{}`",
|
||||
err.reason
|
||||
)
|
||||
};
|
||||
assert_eq!(version, "1.two".to_string());
|
||||
assert_eq!(*err.kind(), IntErrorKind::InvalidDigit);
|
||||
assert_eq!(&*invalid_minor, "two");
|
||||
assert_eq!(*parse_error.kind(), IntErrorKind::InvalidDigit);
|
||||
|
||||
let err = TypeshedVersions::from_str("foo: 3.8-four.9").unwrap_err();
|
||||
assert_eq!(err.line_number, ONE);
|
||||
let TypeshedVersionsParseErrorKind::IntegerParsingFailure { version, err } = err.reason
|
||||
let TypeshedVersionsParseErrorKind::VersionParseError(
|
||||
PythonVersionDeserializationError::InvalidMajorVersion(invalid_major, parse_error),
|
||||
) = err.reason
|
||||
else {
|
||||
panic!()
|
||||
panic!(
|
||||
"Expected an invalid-major-version parse error, got `{}`",
|
||||
err.reason
|
||||
)
|
||||
};
|
||||
assert_eq!(version, "four.9".to_string());
|
||||
assert_eq!(*err.kind(), IntErrorKind::InvalidDigit);
|
||||
assert_eq!(&*invalid_major, "four");
|
||||
assert_eq!(*parse_error.kind(), IntErrorKind::InvalidDigit);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue