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

Method validate_arguments

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

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