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

Method parse_parameter

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

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