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

Method validate_arguments

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

Performs the following validations on the arguments: - 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

2992 /// Performs the following validations on the arguments:
2993 /// - Generator expressions are parenthesized when required by the argument context.
2994 fn validate_arguments(
2995 &mut self,
2996 arguments: &ast::Arguments,
2997 has_trailing_comma: bool,
2998 context: ArgumentsContext,
2999 ) {
3000 let generator_must_be_parenthesized = match context {
3001 ArgumentsContext::Call => has_trailing_comma || arguments.len() > 1,
3002 // CPython rejects an unparenthesized generator expression as a class base even though
3003 // this restriction isn't specified in the class definition grammar.
3004 ArgumentsContext::ClassDefinition => true,
3005 };
3006
3007 if generator_must_be_parenthesized {
3008 for arg in &*arguments.args {
3009 if let Some(ast::ExprGenerator {
3010 range,
3011 parenthesized: false,
3012 ..
3013 }) = arg.as_generator_expr()
3014 {
3015 // test_ok args_unparenthesized_generator
3016 // zip((x for x in range(10)), (y for y in range(10)))
3017 // sum(x for x in range(10))
3018 // sum((x for x in range(10)),)
3019
3020 // test_err args_unparenthesized_generator
3021 // sum(x for x in range(10), 5)
3022 // total(1, 2, x for x in range(5), 6)
3023 // sum(x for x in range(10),)
3024 self.add_error(ParseErrorType::UnparenthesizedGeneratorExpression, range);
3025 }
3026 }
3027 }
3028 }
3029}
3030
3031/// Identifies the syntactic context for an argument list.

Callers 1

parse_argumentsMethod · 0.80

Calls 3

as_generator_exprMethod · 0.80
lenMethod · 0.45
add_errorMethod · 0.45

Tested by

no test coverage detected