Skip to content

Commit 42cbce5

Browse files
authored
[syntax-errors] Fix star annotation before Python 3.11 (#16878)
Summary -- Fixes #16874. I previously emitted a syntax error when starred annotations were _allowed_ rather than when they were actually used. This caused false positives for any starred parameter name because these are allowed to have starred annotations but not required to. The fix is to check if the annotation is actually starred after parsing it. Test Plan -- New inline parser tests derived from the initial report and more examples from the comments, although I think the first case should cover them all.
1 parent 6760251 commit 42cbce5

File tree

3 files changed

+415
-4
lines changed

3 files changed

+415
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# parse_options: {"target-version": "3.10"}
2+
# regression tests for https://github.com/astral-sh/ruff/issues/16874
3+
# starred parameters are fine, just not the annotation
4+
from typing import Annotated, Literal
5+
def foo(*args: Ts): ...
6+
def foo(*x: Literal["this should allow arbitrary strings"]): ...
7+
def foo(*x: Annotated[str, "this should allow arbitrary strings"]): ...
8+
def foo(*args: str, **kwds: int): ...
9+
def union(*x: A | B): ...

crates/ruff_python_parser/src/parser/statement.rs

+17-4
Original file line numberDiff line numberDiff line change
@@ -2959,13 +2959,26 @@ impl<'src> Parser<'src> {
29592959
// # parse_options: {"target-version": "3.11"}
29602960
// def foo(*args: *Ts): ...
29612961

2962+
// test_ok param_with_star_annotation_py310
2963+
// # parse_options: {"target-version": "3.10"}
2964+
// # regression tests for https://github.com/astral-sh/ruff/issues/16874
2965+
// # starred parameters are fine, just not the annotation
2966+
// from typing import Annotated, Literal
2967+
// def foo(*args: Ts): ...
2968+
// def foo(*x: Literal["this should allow arbitrary strings"]): ...
2969+
// def foo(*x: Annotated[str, "this should allow arbitrary strings"]): ...
2970+
// def foo(*args: str, **kwds: int): ...
2971+
// def union(*x: A | B): ...
2972+
29622973
// test_err param_with_star_annotation_py310
29632974
// # parse_options: {"target-version": "3.10"}
29642975
// def foo(*args: *Ts): ...
2965-
self.add_unsupported_syntax_error(
2966-
UnsupportedSyntaxErrorKind::StarAnnotation,
2967-
parsed_expr.range(),
2968-
);
2976+
if parsed_expr.is_starred_expr() {
2977+
self.add_unsupported_syntax_error(
2978+
UnsupportedSyntaxErrorKind::StarAnnotation,
2979+
parsed_expr.range(),
2980+
);
2981+
}
29692982

29702983
parsed_expr
29712984
}

0 commit comments

Comments
 (0)