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

Method parse_parameter

crates/ruff_python_parser/src/parser/statement.rs:3172–3261  ·  view source on GitHub ↗

Parses a single parameter for the given function kind. Matches either the `param_no_default_star_annotation` or `param_no_default` rule in the [Python grammar] depending on whether star annotation is allowed or not. Use [`Parser::parse_parameter_with_default`] to allow parameter with default values. [Python grammar]: https://docs.python.org/3/reference/grammar.html

(
        &mut self,
        start: TextSize,
        function_kind: FunctionKind,
        allow_star_annotation: AllowStarAnnotation,
    )

Source from the content-addressed store, hash-verified

3170 ///
3171 /// [Python grammar]: https://docs.python.org/3/reference/grammar.html
3172 fn parse_parameter(
3173 &mut self,
3174 start: TextSize,
3175 function_kind: FunctionKind,
3176 allow_star_annotation: AllowStarAnnotation,
3177 ) -> ast::Parameter {
3178 let name = self.parse_identifier();
3179
3180 // Annotations are only allowed for function definition. For lambda expression,
3181 // the `:` token would indicate its body.
3182 let annotation = match function_kind {
3183 FunctionKind::FunctionDef if self.eat(TokenKind::Colon) => {
3184 if self.at_expr() {
3185 let parsed_expr = match allow_star_annotation {
3186 AllowStarAnnotation::Yes => {
3187 // test_ok param_with_star_annotation
3188 // def foo(*args: *int | str): ...
3189 // def foo(*args: *(int or str)): ...
3190
3191 // test_err param_with_invalid_star_annotation
3192 // def foo(*args: *): ...
3193 // def foo(*args: (*tuple[int])): ...
3194 // def foo(*args: *int or str): ...
3195 // def foo(*args: *yield x): ...
3196 // # def foo(*args: **int): ...
3197 let parsed_expr = self.parse_conditional_expression_or_higher_impl(
3198 ExpressionContext::starred_bitwise_or(),
3199 );
3200
3201 // test_ok param_with_star_annotation_py311
3202 // # parse_options: {"target-version": "3.11"}
3203 // def foo(*args: *Ts): ...
3204
3205 // test_ok param_with_star_annotation_py310
3206 // # parse_options: {"target-version": "3.10"}
3207 // # regression tests for https://github.com/astral-sh/ruff/issues/16874
3208 // # starred parameters are fine, just not the annotation
3209 // from typing import Annotated, Literal
3210 // def foo(*args: Ts): ...
3211 // def foo(*x: Literal["this should allow arbitrary strings"]): ...
3212 // def foo(*x: Annotated[str, "this should allow arbitrary strings"]): ...
3213 // def foo(*args: str, **kwds: int): ...
3214 // def union(*x: A | B): ...
3215
3216 // test_err param_with_star_annotation_py310
3217 // # parse_options: {"target-version": "3.10"}
3218 // def foo(*args: *Ts): ...
3219 if parsed_expr.is_starred_expr() {
3220 self.add_unsupported_syntax_error(
3221 UnsupportedSyntaxErrorKind::StarAnnotation,
3222 parsed_expr.range(),
3223 );
3224 }
3225
3226 parsed_expr
3227 }
3228 AllowStarAnnotation::No => {
3229 // test_ok param_with_annotation

Callers 2

parse_parametersMethod · 0.80

Calls 11

parse_identifierMethod · 0.80
at_exprMethod · 0.80
is_starred_exprMethod · 0.80
current_token_rangeMethod · 0.80
node_rangeMethod · 0.80
eatMethod · 0.45
rangeMethod · 0.45
add_errorMethod · 0.45

Tested by

no test coverage detected