Collect contains markers in enum (#14782)

We'll add more contains markers for the wheel variants, so I want to
unify them before rebasing the variants branch on them.
This commit is contained in:
konsti 2025-07-21 14:38:33 +02:00 committed by GitHub
parent 9983273289
commit ab48dfd0cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 30 deletions

View File

@ -5,7 +5,7 @@ use uv_pep440::{Version, VersionPattern, VersionSpecifier};
use crate::cursor::Cursor;
use crate::marker::MarkerValueExtra;
use crate::marker::tree::{ContainerOperator, MarkerValueDependencyGroup};
use crate::marker::tree::{ContainerOperator, MarkerValueContains, MarkerValueDependencyGroup};
use crate::{
ExtraOperator, MarkerExpression, MarkerOperator, MarkerTree, MarkerValue, MarkerValueString,
MarkerValueVersion, MarkerWarningKind, Pep508Error, Pep508ErrorSource, Pep508Url, Reporter,
@ -209,10 +209,9 @@ pub(crate) fn parse_marker_key_op_value<T: Pep508Url>(
MarkerValue::MarkerEnvString(key) => {
let value = match r_value {
MarkerValue::Extra
| MarkerValue::Extras
| MarkerValue::DependencyGroups
| MarkerValue::MarkerEnvVersion(_)
| MarkerValue::MarkerEnvString(_) => {
| MarkerValue::MarkerEnvString(_)
| MarkerValue::MarkerEnvContains(_) => {
reporter.report(
MarkerWarningKind::MarkerMarkerComparison,
"Comparing two markers with each other doesn't make any sense,
@ -245,9 +244,8 @@ pub(crate) fn parse_marker_key_op_value<T: Pep508Url>(
let value = match r_value {
MarkerValue::MarkerEnvVersion(_)
| MarkerValue::MarkerEnvString(_)
| MarkerValue::Extra
| MarkerValue::Extras
| MarkerValue::DependencyGroups => {
| MarkerValue::MarkerEnvContains(_)
| MarkerValue::Extra => {
reporter.report(
MarkerWarningKind::ExtraInvalidComparison,
"Comparing extra with something other than a quoted string is wrong,
@ -279,9 +277,11 @@ pub(crate) fn parse_marker_key_op_value<T: Pep508Url>(
// `'...' == extra`
MarkerValue::Extra => parse_extra_expr(operator, &l_string, reporter),
// `'...' in extras`
MarkerValue::Extras => parse_extras_expr(operator, &l_string, reporter),
MarkerValue::MarkerEnvContains(MarkerValueContains::Extras) => {
parse_extras_expr(operator, &l_string, reporter)
}
// `'...' in dependency_groups`
MarkerValue::DependencyGroups => {
MarkerValue::MarkerEnvContains(MarkerValueContains::DependencyGroups) => {
parse_dependency_groups_expr(operator, &l_string, reporter)
}
// `'...' == '...'`, doesn't make much sense
@ -300,22 +300,12 @@ pub(crate) fn parse_marker_key_op_value<T: Pep508Url>(
}
}
}
MarkerValue::Extras => {
MarkerValue::MarkerEnvContains(key) => {
reporter.report(
MarkerWarningKind::Pep440Error,
format!(
"The `extras` marker must be used as '...' in extras' or '... not in extras',
found `{l_value} {operator} {r_value}`, will be ignored"
),
);
return Ok(None);
}
MarkerValue::DependencyGroups => {
reporter.report(
MarkerWarningKind::Pep440Error,
format!(
"The `dependency_groups` marker must be used as '...' in dependency_groups' or '... not in dependency_groups',
found `{l_value} {operator} {r_value}`, will be ignored"
"The `{key}` marker must be used as '...' in {key}' or '... not in {key}',
found `{key} {operator} {r_value}`, will be ignored"
),
);
return Ok(None);

View File

@ -126,6 +126,24 @@ impl Display for MarkerValueString {
}
}
/// Those markers with exclusively `in` and `not in` operators (PEP 751)
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub enum MarkerValueContains {
/// `extras`. This one is special because it's a list, and user-provided
Extras,
/// `dependency_groups`. This one is special because it's a list, and user-provided
DependencyGroups,
}
impl Display for MarkerValueContains {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
Self::Extras => f.write_str("extras"),
Self::DependencyGroups => f.write_str("dependency_groups"),
}
}
}
/// One of the predefined environment values
///
/// <https://packaging.python.org/en/latest/specifications/dependency-specifiers/#environment-markers>
@ -137,10 +155,8 @@ pub enum MarkerValue {
MarkerEnvString(MarkerValueString),
/// `extra`. This one is special because it's a list, and user-provided
Extra,
/// `extras`. This one is special because it's a list, and user-provided
Extras,
/// `dependency_groups`. This one is special because it's a list, and user-provided
DependencyGroups,
/// Those markers with exclusively `in` and `not in` operators (PEP 751)
MarkerEnvContains(MarkerValueContains),
/// Not a constant, but a user given quoted string with a value inside such as '3.8' or "windows"
QuotedString(ArcStr),
}
@ -181,8 +197,8 @@ impl FromStr for MarkerValue {
"sys_platform" => Self::MarkerEnvString(MarkerValueString::SysPlatform),
"sys.platform" => Self::MarkerEnvString(MarkerValueString::SysPlatformDeprecated),
"extra" => Self::Extra,
"extras" => Self::Extras,
"dependency_groups" => Self::DependencyGroups,
"extras" => Self::MarkerEnvContains(MarkerValueContains::Extras),
"dependency_groups" => Self::MarkerEnvContains(MarkerValueContains::DependencyGroups),
_ => return Err(format!("Invalid key: {s}")),
};
Ok(value)
@ -195,8 +211,7 @@ impl Display for MarkerValue {
Self::MarkerEnvVersion(marker_value_version) => marker_value_version.fmt(f),
Self::MarkerEnvString(marker_value_string) => marker_value_string.fmt(f),
Self::Extra => f.write_str("extra"),
Self::Extras => f.write_str("extras"),
Self::DependencyGroups => f.write_str("dependency_groups"),
Self::MarkerEnvContains(marker_value_contains) => marker_value_contains.fmt(f),
Self::QuotedString(value) => write!(f, "'{value}'"),
}
}