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

Method validate_arguments

crates/ruff_python_parser/src/parser/expression.rs:3081–3132  ·  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

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