Skip to content

Raise clear error for multiple variadic arguments#1555

Open
Fridayai700 wants to merge 1 commit intofastapi:masterfrom
Fridayai700:fix-multiple-list-args-260
Open

Raise clear error for multiple variadic arguments#1555
Fridayai700 wants to merge 1 commit intofastapi:masterfrom
Fridayai700:fix-multiple-list-args-260

Conversation

@Fridayai700
Copy link

Summary

Closes #260.

When a command has more than one argument annotated with List[...] (or any type that maps to nargs=-1), Click raises an opaque TypeError: Cannot have two nargs < 0. This PR adds early validation in Typer's parameter collection to detect multiple variadic arguments and raise a clear UsageError explaining the constraint.

Changes

  • Added validation after parameter collection in get_params_convertors_ctx_param_name_from_function() to check for multiple arguments with nargs < 0
  • Raises click.UsageError with a message naming all conflicting arguments
  • Added test test_multiple_list_arguments_error in tests/test_type_conversion.py

Example

Before:

TypeError: Cannot have two nargs < 0

After:

UsageError: Only one argument can take multiple values (nargs=-1), but 2 were found: 'names', 'others'. Use List[...] (or Sequence[...]) for at most one argument.

When a command has more than one argument annotated with List[...],
Click raises an opaque "TypeError: Cannot have two nargs < 0".
This adds validation in Typer's parameter collection to detect
multiple variadic arguments early and raise a clear UsageError
explaining the constraint.

Closes fastapi#260
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Multiple arguments with multiple values raises an error from Click

1 participant