Skip to content

Commit

Permalink
Enforce target and interpreter requires-python versions (#532)
Browse files Browse the repository at this point in the history
## Summary

This PR modifies the behavior of our `--python-version` override in two
ways:

1. First, we always use the "real" interpreter in the source
distribution builder. I think this is correct. We don't need to use the
fake markers for recursive builds, because all we care about is the
top-level resolution, and we already assume that a single source
distribution will always return the same metadata regardless of its
build environment.
2. Second, we require that source distributions are compatible with
_both_ the "real" interpreter version and the marker environment. This
ensures that we don't try to build source distributions that are
compatible with our interpreter, but incompatible with the target
version.

Closes #407.
  • Loading branch information
charliermarsh authored Dec 4, 2023
1 parent d96c18b commit 0ac4254
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 23 deletions.
10 changes: 4 additions & 6 deletions crates/puffin-cli/src/commands/pip_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,14 @@ pub(crate) async fn pip_compile(
// Determine the compatible platform tags.
let tags = Tags::from_interpreter(venv.interpreter())?;

// Determine the interpreter to use for resolution.
let interpreter = venv.interpreter().clone();

// Determine the markers to use for resolution.
let markers = python_version.map_or_else(
|| Cow::Borrowed(venv.interpreter().markers()),
|python_version| Cow::Owned(python_version.markers(venv.interpreter().markers())),
);
// Inject the fake python version if necessary
let interpreter_info = venv
.interpreter()
.clone()
.patch_markers(markers.clone().into_owned());

// Instantiate a client.
let client = RegistryClientBuilder::new(cache.clone())
Expand All @@ -143,7 +141,7 @@ pub(crate) async fn pip_compile(
let build_dispatch = BuildDispatch::new(
client.clone(),
cache.clone(),
interpreter_info,
interpreter,
fs::canonicalize(venv.python_executable())?,
no_build,
index_urls,
Expand Down
6 changes: 0 additions & 6 deletions crates/puffin-interpreter/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,6 @@ impl Interpreter {
pub fn sys_executable(&self) -> &Path {
&self.sys_executable
}

/// Inject markers of a fake python version
#[must_use]
pub fn patch_markers(self, markers: MarkerEnvironment) -> Self {
Self { markers, ..self }
}
}

#[derive(Debug, Deserialize, Serialize, Clone)]
Expand Down
3 changes: 2 additions & 1 deletion crates/puffin-resolver/src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,8 @@ impl<'a, Context: BuildContext + Send + Sync> Resolver<'a, Context> {
metadata,
&package_name,
self.tags,
self.build_context.interpreter().version(),
self.markers,
self.build_context.interpreter(),
self.exclude_newer.as_ref(),
);
self.index
Expand Down
25 changes: 15 additions & 10 deletions crates/puffin-resolver/src/version_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use chrono::{DateTime, Utc};
use tracing::warn;

use distribution_filename::{SourceDistFilename, WheelFilename};
use pep440_rs::Version;
use pep508_rs::MarkerEnvironment;
use platform_tags::{TagPriority, Tags};
use puffin_interpreter::Interpreter;
use puffin_macros::warn_once;
use puffin_normalize::PackageName;
use pypi_types::{SimpleJson, Yanked};
Expand All @@ -25,7 +26,8 @@ impl VersionMap {
metadata: SimpleJson,
package_name: &PackageName,
tags: &Tags,
python_version: &Version,
markers: &MarkerEnvironment,
interpreter: &Interpreter,
exclude_newer: Option<&DateTime<Utc>>,
) -> Self {
let mut version_map: BTreeMap<PubGrubVersion, PrioritizedDistribution> =
Expand All @@ -38,14 +40,17 @@ impl VersionMap {
// distributions which give no other indication of their compatibility and wheels which
// may be tagged `py3-none-any` but have `requires-python: ">=3.9"`.
// TODO(konstin): https://github.com/astral-sh/puffin/issues/406
if !file
.requires_python
.as_ref()
.map_or(true, |requires_python| {
requires_python.contains(python_version)
})
{
continue;
if let Some(requires_python) = file.requires_python.as_ref() {
// The interpreter and marker version are often the same, but can differ. For
// example, if the user is resolving against a target Python version passed in
// via the command-line, that version will differ from the interpreter version.
let interpreter_version = interpreter.version();
let marker_version = &markers.python_version.version;
if !requires_python.contains(interpreter_version)
|| !requires_python.contains(marker_version)
{
continue;
}
}

// Support resolving as if it were an earlier timestamp, at least as long files have
Expand Down

0 comments on commit 0ac4254

Please sign in to comment.