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

Method parse_parameter

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

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