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

Method parse_parameter

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

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