Skip to content

Commit

Permalink
REdo requires python
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Aug 30, 2024
1 parent 0510842 commit 2d22ba2
Show file tree
Hide file tree
Showing 18 changed files with 324 additions and 290 deletions.
2 changes: 1 addition & 1 deletion crates/bench/benches/uv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ mod resolver {
let python_requirement = if universal {
PythonRequirement::from_requires_python(
interpreter,
&RequiresPython::greater_than_equal_version(&Version::new([3, 11])),
RequiresPython::greater_than_equal_version(&Version::new([3, 11])),
)
} else {
PythonRequirement::from_interpreter(interpreter)
Expand Down
23 changes: 9 additions & 14 deletions crates/uv-resolver/src/lock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub struct Lock {
/// The list of supported environments specified by the user.
supported_environments: Vec<MarkerTree>,
/// The range of supported Python versions.
requires_python: Option<RequiresPython>,
requires_python: RequiresPython,
/// We discard the lockfile if these options don't match.
options: ResolverOptions,
/// The actual locked version and their metadata.
Expand Down Expand Up @@ -186,7 +186,7 @@ impl Lock {
fn new(
version: u32,
mut packages: Vec<Package>,
requires_python: Option<RequiresPython>,
requires_python: RequiresPython,
options: ResolverOptions,
manifest: ResolverManifest,
supported_environments: Vec<MarkerTree>,
Expand Down Expand Up @@ -241,11 +241,9 @@ impl Lock {

// Remove wheels that don't match `requires-python` and can't be selected for
// installation.
if let Some(requires_python) = &requires_python {
package
.wheels
.retain(|wheel| requires_python.matches_wheel_tag(&wheel.filename));
}
package
.wheels
.retain(|wheel| requires_python.matches_wheel_tag(&wheel.filename));
}
packages.sort_by(|dist1, dist2| dist1.id.cmp(&dist2.id));

Expand Down Expand Up @@ -390,8 +388,8 @@ impl Lock {
}

/// Returns the supported Python version range for the lockfile, if present.
pub fn requires_python(&self) -> Option<&RequiresPython> {
self.requires_python.as_ref()
pub fn requires_python(&self) -> &RequiresPython {
&self.requires_python
}

/// Returns the resolution mode used to generate this lock.
Expand Down Expand Up @@ -527,9 +525,7 @@ impl Lock {
let mut doc = toml_edit::DocumentMut::new();
doc.insert("version", value(i64::from(self.version)));

if let Some(ref requires_python) = self.requires_python {
doc.insert("requires-python", value(requires_python.to_string()));
}
doc.insert("requires-python", value(self.requires_python.to_string()));

if !self.fork_markers.is_empty() {
let fork_markers = each_element_on_its_line_array(
Expand Down Expand Up @@ -1158,8 +1154,7 @@ impl ResolverManifest {
#[serde(rename_all = "kebab-case")]
struct LockWire {
version: u32,
#[serde(default)]
requires_python: Option<RequiresPython>,
requires_python: RequiresPython,
/// If this lockfile was built from a forking resolution with non-identical forks, store the
/// forks in the lockfile so we can recreate them in subsequent resolutions.
#[serde(rename = "resolution-markers", default)]
Expand Down
2 changes: 1 addition & 1 deletion crates/uv-resolver/src/pubgrub/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ impl PubGrubPackage {
}
}

#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Hash, Ord)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Hash, Ord)]
pub enum PubGrubPython {
/// The Python version installed in the current environment.
Installed,
Expand Down
74 changes: 50 additions & 24 deletions crates/uv-resolver/src/pubgrub/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::candidate_selector::CandidateSelector;
use crate::error::ErrorTree;
use crate::fork_urls::ForkUrls;
use crate::prerelease::AllowPrerelease;
use crate::python_requirement::{PythonRequirement, PythonTarget};
use crate::python_requirement::{PythonRequirement, PythonRequirementSource};
use crate::resolver::{IncompletePackage, UnavailablePackage, UnavailableReason};
use crate::{RequiresPython, ResolverMarkers};

Expand Down Expand Up @@ -52,25 +52,19 @@ impl ReportFormatter<PubGrubPackage, Range<Version>, UnavailableReason>
&**package,
PubGrubPackageInner::Python(PubGrubPython::Target)
) {
return if let Some(target) = self.python_requirement.target() {
format!(
"the requested {package} version ({target}) does not satisfy {}",
self.compatible_range(package, set)
)
} else {
format!(
"the requested {package} version does not satisfy {}",
self.compatible_range(package, set)
)
};
let target = self.python_requirement.target();
return format!(
"the requested {package} version ({target}) does not satisfy {}",
self.compatible_range(package, set)
);
}
if matches!(
&**package,
PubGrubPackageInner::Python(PubGrubPython::Installed)
) {
let installed = self.python_requirement.exact();
return format!(
"the current {package} version ({}) does not satisfy {}",
self.python_requirement.installed(),
"the current {package} version ({installed}) does not satisfy {}",
self.compatible_range(package, set)
);
}
Expand Down Expand Up @@ -554,16 +548,13 @@ impl PubGrubReportFormatter<'_> {
&**dependency,
PubGrubPackageInner::Python(PubGrubPython::Target)
) {
if let Some(PythonTarget::RequiresPython(requires_python)) =
self.python_requirement.target()
{
hints.insert(PubGrubHint::RequiresPython {
requires_python: requires_python.clone(),
package: package.clone(),
package_set: self.simplify_set(package_set, package).into_owned(),
package_requires_python: dependency_set.clone(),
});
}
hints.insert(PubGrubHint::RequiresPython {
source: self.python_requirement.source(),
requires_python: self.python_requirement.target().clone(),
package: package.clone(),
package_set: self.simplify_set(package_set, package).into_owned(),
package_requires_python: dependency_set.clone(),
});
}
}
DerivationTree::External(External::NotRoot(..)) => {}
Expand Down Expand Up @@ -798,6 +789,7 @@ pub(crate) enum PubGrubHint {
},
/// The `Requires-Python` requirement was not satisfied.
RequiresPython {
source: PythonRequirementSource,
requires_python: RequiresPython,
#[derivative(PartialEq = "ignore", Hash = "ignore")]
package: PubGrubPackage,
Expand Down Expand Up @@ -932,6 +924,7 @@ impl std::fmt::Display for PubGrubHint {
)
}
Self::RequiresPython {
source: PythonRequirementSource::RequiresPython,
requires_python,
package,
package_set,
Expand All @@ -948,6 +941,39 @@ impl std::fmt::Display for PubGrubHint {
package_requires_python.bold(),
)
}
Self::RequiresPython {
source: PythonRequirementSource::PythonVersion,
requires_python,
package,
package_set,
package_requires_python,
} => {
write!(
f,
"{}{} The `--python-version` value ({}) includes Python versions that are not supported by your dependencies (e.g., {} only supports {}). Consider using a higher `--python-version` value.",
"hint".bold().cyan(),
":".bold(),
requires_python.bold(),
PackageRange::compatibility(package, package_set, None).bold(),
package_requires_python.bold(),
)
}
Self::RequiresPython {
source: PythonRequirementSource::Interpreter,
requires_python: _,
package,
package_set,
package_requires_python,
} => {
write!(
f,
"{}{} The Python interpreter uses a Python version that is not supported by your dependencies (e.g., {} only supports {}). Consider passing a `--python-version` value to raise the minimum supported version.",
"hint".bold().cyan(),
":".bold(),
PackageRange::compatibility(package, package_set, None).bold(),
package_requires_python.bold(),
)
}
}
}
}
Expand Down
Loading

0 comments on commit 2d22ba2

Please sign in to comment.