Skip to content

Commit 832f9bb

Browse files
committed
Preserve custom validation in composed binders
Do not resurrect named checks from display metadata when composing an explicitly custom parser. Keep CSV duplicate validation at its own parser boundary. Assisted-by: OpenAI Codex:model version not exposed
1 parent 3ae04d3 commit 832f9bb

4 files changed

Lines changed: 43 additions & 53 deletions

File tree

‎crates/stdlib/src/csv.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,13 @@ mod _csv {
396396
}
397397

398398
fn from_args(vm: &VirtualMachine, args: &mut FuncArgs) -> Result<Self, ArgumentError> {
399+
if !args.args.is_empty() && args.kwargs.contains_key("dialect") {
400+
return Err(vm
401+
.new_type_error(
402+
"argument for function given by name ('dialect') and position (1)",
403+
)
404+
.into());
405+
}
399406
let res = Self {
400407
dialect: args.take_positional_keyword("dialect"),
401408
delimiter: args.kwargs.swap_remove("delimiter"),

‎crates/vm/src/builtins/property.rs‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ impl FromArgs for SetNameArgs {
2727
]);
2828

2929
fn from_args(vm: &VirtualMachine, args: &mut FuncArgs) -> Result<Self, ArgumentError> {
30+
if !args.kwargs.is_empty() {
31+
return Err(vm
32+
.new_type_error("__set_name__() takes no keyword arguments")
33+
.into());
34+
}
3035
let [owner, name]: [PyObjectRef; 2] =
3136
core::mem::take(&mut args.args)
3237
.try_into()

‎crates/vm/src/function/argument.rs‎

Lines changed: 23 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -857,43 +857,6 @@ fn binding_accepts_keyword(params: &[Param], name: &Wtf8) -> bool {
857857
})
858858
}
859859

860-
/// First positional-or-keyword parameter that was also passed by keyword.
861-
fn duplicate_name_and_position<'a>(
862-
params: &'a [Param],
863-
nargs: usize,
864-
kwargs: &KwArgs,
865-
) -> Option<(&'a str, usize)> {
866-
fn walk<'a>(
867-
params: &'a [Param],
868-
nargs: usize,
869-
kwargs: &KwArgs,
870-
pos: &mut usize,
871-
) -> Option<(&'a str, usize)> {
872-
for param in params {
873-
match param.kind {
874-
ParamKind::Flatten(Some(inner)) => {
875-
if let Some(hit) = walk(inner, nargs, kwargs, pos) {
876-
return Some(hit);
877-
}
878-
}
879-
ParamKind::Flatten(None) | ParamKind::PositionalOnly => {
880-
*pos += 1;
881-
}
882-
ParamKind::PositionalOrKeyword => {
883-
*pos += 1;
884-
if nargs >= *pos && !param.name.is_empty() && kwargs.contains_key(param.name) {
885-
return Some((param.name, *pos));
886-
}
887-
}
888-
ParamKind::VarPositional => return None,
889-
ParamKind::KeywordOnly | ParamKind::VarKeyword => {}
890-
}
891-
}
892-
None
893-
}
894-
walk(params, nargs, kwargs, &mut 0)
895-
}
896-
897860
/// Walk `params` in declaration order looking for the first required argument
898861
/// that is missing. `Err(None)` ends the walk without a result.
899862
fn missing_from_params(
@@ -1152,7 +1115,7 @@ pub trait FromArgs: Sized {
11521115

11531116
/// Validate a known binding shape before converting any argument, following
11541117
/// `_PyArg_UnpackKeywords` error ordering. Explicit custom parsers keep the
1155-
/// conservative positional-count, duplicate, and missing-argument checks.
1118+
/// positional-count and no-keyword checks, but own named validation.
11561119
/// Invalid known parameter ordering is rejected at compile time.
11571120
///
11581121
/// The missing-argument walk is skipped when every required positional
@@ -1204,21 +1167,6 @@ pub trait FromArgs: Sized {
12041167
if Self::TAKES_NO_KEYWORDS && num_given < *arity.start() {
12051168
return Some(ArgumentError::TooFewArgs.into_exception(arity, num_given, 0, callee, vm));
12061169
}
1207-
if let Some(params) = Self::PARAMS
1208-
&& !args.kwargs.is_empty()
1209-
&& let Some((name, pos)) = duplicate_name_and_position(params, num_given, &args.kwargs)
1210-
{
1211-
return Some(callee.given_by_name_and_position(name, pos, vm));
1212-
}
1213-
if num_given < *arity.start() || const { Self::HAS_REQUIRED_KEYWORD_ONLY } {
1214-
let mut pos = 0;
1215-
let mut skip = instance;
1216-
if let Err(Some(missing)) =
1217-
Self::missing_from(num_given, &args.kwargs, &mut pos, &mut skip)
1218-
{
1219-
return Some(missing.into_exception(arity, num_given, 0, callee, vm));
1220-
}
1221-
}
12221170
None
12231171
}
12241172

@@ -1881,6 +1829,13 @@ mod tests {
18811829
second: i32,
18821830
}
18831831

1832+
#[allow(dead_code)]
1833+
#[derive(FromArgs)]
1834+
struct FlattenedLegacy {
1835+
#[pyarg(flatten)]
1836+
args: (PyObjectRef, LegacyNamed),
1837+
}
1838+
18841839
struct DisplayOnly(FuncArgs);
18851840

18861841
impl FromArgs for DisplayOnly {
@@ -2025,6 +1980,21 @@ mod tests {
20251980
});
20261981
}
20271982

1983+
#[test]
1984+
fn legacy_conversion_order_survives_receivers_and_flattened_composition() {
1985+
crate::Interpreter::without_stdlib(Default::default()).enter(|vm| {
1986+
for message in [
1987+
binding_error::<(PyObjectRef, LegacyNamed)>(2, &[], true, vm),
1988+
binding_error::<FlattenedLegacy>(2, &[], true, vm),
1989+
binding_error::<(LegacyNamed, ())>(1, &["first"], false, vm),
1990+
] {
1991+
assert!(message.contains("NoneType"), "{message}");
1992+
assert!(!message.contains("missing required"), "{message}");
1993+
assert!(!message.contains("given by name"), "{message}");
1994+
}
1995+
});
1996+
}
1997+
20281998
#[test]
20291999
fn explicitly_custom_signatures_do_not_opt_in_to_clinic_validation() {
20302000
const { assert!(DisplayOnly::BINDING_PARAMS.is_none()) };

‎extra_tests/snippets/vm_argument_errors.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,14 @@ def test_custom_parsers_preserve_their_conversion_boundaries():
382382
csv.reader(TestFailingIter(), "excel", dialect="excel")
383383
with assert_raises(RuntimeError):
384384
csv.reader(TestFailingIter(), unexpected=1)
385+
check(
386+
TypeError,
387+
"argument for function given by name ('dialect') and position (1)",
388+
csv.reader,
389+
[],
390+
"excel",
391+
dialect="excel",
392+
)
385393

386394
try:
387395
import _thread

0 commit comments

Comments
 (0)