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::cursor::Cursor;
use crate::marker::MarkerValueExtra; use crate::marker::MarkerValueExtra;
use crate::marker::tree::{ContainerOperator, MarkerValueDependencyGroup}; use crate::marker::tree::{ContainerOperator, MarkerValueContains, MarkerValueDependencyGroup};
use crate::{ use crate::{
ExtraOperator, MarkerExpression, MarkerOperator, MarkerTree, MarkerValue, MarkerValueString, ExtraOperator, MarkerExpression, MarkerOperator, MarkerTree, MarkerValue, MarkerValueString,
MarkerValueVersion, MarkerWarningKind, Pep508Error, Pep508ErrorSource, Pep508Url, Reporter, MarkerValueVersion, MarkerWarningKind, Pep508Error, Pep508ErrorSource, Pep508Url, Reporter,
@ -209,10 +209,9 @@ pub(crate) fn parse_marker_key_op_value<T: Pep508Url>(
MarkerValue::MarkerEnvString(key) => { MarkerValue::MarkerEnvString(key) => {
let value = match r_value { let value = match r_value {
MarkerValue::Extra MarkerValue::Extra
| MarkerValue::Extras
| MarkerValue::DependencyGroups
| MarkerValue::MarkerEnvVersion(_) | MarkerValue::MarkerEnvVersion(_)
| MarkerValue::MarkerEnvString(_) => { | MarkerValue::MarkerEnvString(_)
| MarkerValue::MarkerEnvContains(_) => {
reporter.report( reporter.report(
MarkerWarningKind::MarkerMarkerComparison, MarkerWarningKind::MarkerMarkerComparison,
"Comparing two markers with each other doesn't make any sense, "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 { let value = match r_value {
MarkerValue::MarkerEnvVersion(_) MarkerValue::MarkerEnvVersion(_)
| MarkerValue::MarkerEnvString(_) | MarkerValue::MarkerEnvString(_)
| MarkerValue::Extra | MarkerValue::MarkerEnvContains(_)
| MarkerValue::Extras | MarkerValue::Extra => {
| MarkerValue::DependencyGroups => {
reporter.report( reporter.report(
MarkerWarningKind::ExtraInvalidComparison, MarkerWarningKind::ExtraInvalidComparison,
"Comparing extra with something other than a quoted string is wrong, "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` // `'...' == extra`
MarkerValue::Extra => parse_extra_expr(operator, &l_string, reporter), MarkerValue::Extra => parse_extra_expr(operator, &l_string, reporter),
// `'...' in extras` // `'...' 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` // `'...' in dependency_groups`
MarkerValue::DependencyGroups => { MarkerValue::MarkerEnvContains(MarkerValueContains::DependencyGroups) => {
parse_dependency_groups_expr(operator, &l_string, reporter) parse_dependency_groups_expr(operator, &l_string, reporter)
} }
// `'...' == '...'`, doesn't make much sense // `'...' == '...'`, 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( reporter.report(
MarkerWarningKind::Pep440Error, MarkerWarningKind::Pep440Error,
format!( format!(
"The `extras` marker must be used as '...' in extras' or '... not in extras', "The `{key}` marker must be used as '...' in {key}' or '... not in {key}',
found `{l_value} {operator} {r_value}`, will be ignored" found `{key} {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"
), ),
); );
return Ok(None); 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 /// One of the predefined environment values
/// ///
/// <https://packaging.python.org/en/latest/specifications/dependency-specifiers/#environment-markers> /// <https://packaging.python.org/en/latest/specifications/dependency-specifiers/#environment-markers>
@ -137,10 +155,8 @@ pub enum MarkerValue {
MarkerEnvString(MarkerValueString), MarkerEnvString(MarkerValueString),
/// `extra`. This one is special because it's a list, and user-provided /// `extra`. This one is special because it's a list, and user-provided
Extra, Extra,
/// `extras`. This one is special because it's a list, and user-provided /// Those markers with exclusively `in` and `not in` operators (PEP 751)
Extras, MarkerEnvContains(MarkerValueContains),
/// `dependency_groups`. This one is special because it's a list, and user-provided
DependencyGroups,
/// Not a constant, but a user given quoted string with a value inside such as '3.8' or "windows" /// Not a constant, but a user given quoted string with a value inside such as '3.8' or "windows"
QuotedString(ArcStr), QuotedString(ArcStr),
} }
@ -181,8 +197,8 @@ impl FromStr for MarkerValue {
"sys_platform" => Self::MarkerEnvString(MarkerValueString::SysPlatform), "sys_platform" => Self::MarkerEnvString(MarkerValueString::SysPlatform),
"sys.platform" => Self::MarkerEnvString(MarkerValueString::SysPlatformDeprecated), "sys.platform" => Self::MarkerEnvString(MarkerValueString::SysPlatformDeprecated),
"extra" => Self::Extra, "extra" => Self::Extra,
"extras" => Self::Extras, "extras" => Self::MarkerEnvContains(MarkerValueContains::Extras),
"dependency_groups" => Self::DependencyGroups, "dependency_groups" => Self::MarkerEnvContains(MarkerValueContains::DependencyGroups),
_ => return Err(format!("Invalid key: {s}")), _ => return Err(format!("Invalid key: {s}")),
}; };
Ok(value) Ok(value)
@ -195,8 +211,7 @@ impl Display for MarkerValue {
Self::MarkerEnvVersion(marker_value_version) => marker_value_version.fmt(f), Self::MarkerEnvVersion(marker_value_version) => marker_value_version.fmt(f),
Self::MarkerEnvString(marker_value_string) => marker_value_string.fmt(f), Self::MarkerEnvString(marker_value_string) => marker_value_string.fmt(f),
Self::Extra => f.write_str("extra"), Self::Extra => f.write_str("extra"),
Self::Extras => f.write_str("extras"), Self::MarkerEnvContains(marker_value_contains) => marker_value_contains.fmt(f),
Self::DependencyGroups => f.write_str("dependency_groups"),
Self::QuotedString(value) => write!(f, "'{value}'"), Self::QuotedString(value) => write!(f, "'{value}'"),
} }
} }