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

Method validate_arguments

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

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