pydantic/monty · error · syn::Error
FromArgs can only be derived for structs with named fields
Error message
FromArgs can only be derived for structs with named fields
What it means
A compile-time `syn` error from the `#[derive(FromArgs)]` macro in monty-macros: the derive only supports structs with named fields. Unit structs, newtype/tuple structs, or enums cannot be argument containers because the runtime binder dispatches by parameter name.
Source
Thrown at crates/monty-macros/src/from_args.rs:156
/// `#[from_args(default = <expr>)]`.
Explicit(Box<Expr>),
}
impl Signature {
fn parse(input: &DeriveInput) -> syn::Result<Self> {
let DeriveInput {
ident: struct_ident,
data,
attrs,
..
} = input;
let Data::Struct(DataStruct {
fields: Fields::Named(named),
..
}) = data
else {
return Err(syn::Error::new(
input.span(),
"FromArgs can only be derived for structs with named fields",
));
};
let StructAttrs {
name: func_name,
style,
at_most_total,
vectorcall,
kwarg_error_name,
bad_arg,
kwargs_not_supported_yet,
} = parse_struct_attrs(attrs)?;
let mut fields = Vec::with_capacity(named.named.len());
for field in &named.named {
let opts = parse_field_attrs(&field.attrs)?;View on GitHub (pinned to adc986b362)
Solutions
- Convert the struct to named fields: `struct Args { pattern: String }`
- Use a plain tuple/unit struct without the derive and parse manually
- If multiple shapes are needed, define one named-field struct per shape
Example fix
// before
#[derive(FromArgs)]
struct CompileArgs(String, u32);
// after
#[derive(FromArgs)]
struct CompileArgs {
source: String,
opt_level: u32,
} Defensive patterns
Strategy: type-guard
Validate before calling
// compile-time: ensure the derive target has named fields
const _check: FromArgsShape = { source: 'x' }; Type guard
type NamedFieldsOnly<T> = { [K in keyof T]: T[K] } extends T ? T : never; Prevention
- Only derive FromArgs on structs with named fields
- Never apply it to enums or tuple structs
- Let the compiler catch this — it fails at macro expansion, not runtime
When it happens
Trigger: Writing `#[derive(FromArgs)]` on `struct Args(String)` (tuple struct), `struct Args;` (unit struct), or an enum.
Common situations: Refactoring an existing tuple struct to take parsed CLI/Python-style arguments; applying the derive to an enum to share handling across variants; copy-pasting the derive onto the wrong item.
Understand the failure class
Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.
Related errors
- required positional fields must come before positional field
- `varargs` fields must be `Vec<Value>` — coerce elements in t
- keyword-only fields must have a `default` — the runtime bind
- `default` and `static_string` cannot be applied to `varargs`
- no fields may appear after a `#[from_args(varkwargs)]` field
AI-assisted analysis of pydantic/monty@adc986b362 (2026-09-13).
Data as JSON: /api/errors/2a7c35210a642051.
Report an issue: GitHub.