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

Method parse_parameter

crates/ruff_python_parser/src/parser/statement.rs:3182–3271  ·  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

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