Enforce target and interpreter requires-python versions (#532)

## 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 https://github.com/astral-sh/puffin/issues/407.
This commit is contained in:
Charlie Marsh 2023-12-04 05:27:36 -05:00 committed by GitHub
parent d96c18b3a8
commit 0ac4254a7e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 23 deletions

View file

@ -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())
@ -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,

View file

@ -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)]

View file

@ -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

View file

@ -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};
@ -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> =
@ -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