MCPcopy Create free account
hub / github.com/astral-sh/ruff / validate_arguments

Method validate_arguments

crates/ruff_python_parser/src/parser/expression.rs:3091–3142  ·  view source on GitHub ↗

Performs the following validations on the arguments: 1. There aren't any duplicate keyword argument 2. Generator expressions are parenthesized when required by the argument context.

(
        &mut self,
        arguments: &ast::Arguments,
        has_trailing_comma: bool,
        context: ArgumentsContext,
    )

Source from the content-addressed store, hash-verified

3089 /// 1. There aren't any duplicate keyword argument
3090 /// 2. Generator expressions are parenthesized when required by the argument context.
3091 fn validate_arguments(
3092 &mut self,
3093 arguments: &ast::Arguments,
3094 has_trailing_comma: bool,
3095 context: ArgumentsContext,
3096 ) {
3097 let mut all_arg_names =
3098 FxHashSet::with_capacity_and_hasher(arguments.keywords.len(), FxBuildHasher);
3099
3100 for (name, range) in arguments
3101 .keywords
3102 .iter()
3103 .filter_map(|argument| argument.arg.as_ref().map(|arg| (arg, argument.range)))
3104 {
3105 let arg_name = name.as_str();
3106 if !all_arg_names.insert(arg_name) {
3107 self.add_error(
3108 ParseErrorType::DuplicateKeywordArgumentError(arg_name.to_string()),
3109 range,
3110 );
3111 }
3112 }
3113
3114 let generator_must_be_parenthesized = match context {
3115 ArgumentsContext::Call => has_trailing_comma || arguments.len() > 1,
3116 // CPython rejects an unparenthesized generator expression as a class base even though
3117 // this restriction isn't specified in the class definition grammar.
3118 ArgumentsContext::ClassDefinition => true,
3119 };
3120
3121 if generator_must_be_parenthesized {
3122 for arg in &*arguments.args {
3123 if let Some(ast::ExprGenerator {
3124 range,
3125 parenthesized: false,
3126 ..
3127 }) = arg.as_generator_expr()
3128 {
3129 // test_ok args_unparenthesized_generator
3130 // zip((x for x in range(10)), (y for y in range(10)))
3131 // sum(x for x in range(10))
3132 // sum((x for x in range(10)),)
3133
3134 // test_err args_unparenthesized_generator
3135 // sum(x for x in range(10), 5)
3136 // total(1, 2, x for x in range(5), 6)
3137 // sum(x for x in range(10),)
3138 self.add_error(ParseErrorType::UnparenthesizedGeneratorExpression, range);
3139 }
3140 }
3141 }
3142 }
3143}
3144
3145/// Identifies the syntactic context for an argument list.

Callers 1

parse_argumentsMethod · 0.80

Calls 9

as_generator_exprMethod · 0.80
lenMethod · 0.45
iterMethod · 0.45
mapMethod · 0.45
as_refMethod · 0.45
as_strMethod · 0.45
insertMethod · 0.45
add_errorMethod · 0.45
to_stringMethod · 0.45

Tested by

no test coverage detected