Skip to content

Commit 47da321

Browse files
committed
add linter test and fix duplicate syntax error
1 parent 0790899 commit 47da321

File tree

2 files changed

+72
-9
lines changed

2 files changed

+72
-9
lines changed

crates/ruff/tests/lint.rs

+63
Original file line numberDiff line numberDiff line change
@@ -2667,6 +2667,69 @@ fn walrus_before_py38() {
26672667
);
26682668
}
26692669

2670+
#[test]
2671+
fn unparenthesized_walrus_before_py310() {
2672+
let stdin = r#"
2673+
# okay on 3.10 but not on 3.9
2674+
{x := 1, 2, 3} # set literal
2675+
{last := x for x in range(3)} # set comprehension
2676+
# sequence indexes
2677+
lst[x := 1]
2678+
dct[x := 1]
2679+
2680+
# never okay
2681+
l[x:=1:-1]
2682+
2683+
# always okay
2684+
{(x := 1), 2, 3}
2685+
{(last := x) for x in range(3)}
2686+
lst[(x := 1)]
2687+
dct[(x := 1)]
2688+
l[(x:=1):-1]
2689+
"#;
2690+
2691+
// 1 error from "never okay" slice
2692+
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
2693+
.args(STDIN_BASE_OPTIONS)
2694+
.args(["--stdin-filename", "test.py"])
2695+
.arg("--target-version=py310")
2696+
.arg("-")
2697+
.pass_stdin(stdin),
2698+
@r"
2699+
success: false
2700+
exit_code: 1
2701+
----- stdout -----
2702+
test.py:10:3: SyntaxError: Unparenthesized named expression cannot be used here
2703+
Found 1 error.
2704+
2705+
----- stderr -----
2706+
"
2707+
);
2708+
2709+
// 5 errors on 3.9 with preview - 4 from 3.9, 1 from above
2710+
assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME))
2711+
.args(STDIN_BASE_OPTIONS)
2712+
.args(["--stdin-filename", "test.py"])
2713+
.arg("--target-version=py39")
2714+
.arg("--preview")
2715+
.arg("-")
2716+
.pass_stdin(stdin),
2717+
@r"
2718+
success: false
2719+
exit_code: 1
2720+
----- stdout -----
2721+
test.py:3:2: SyntaxError: Cannot use unparenthesized assignment expression on Python 3.9 (syntax was added in Python 3.10)
2722+
test.py:4:2: SyntaxError: Cannot use unparenthesized assignment expression on Python 3.9 (syntax was added in Python 3.10)
2723+
test.py:6:5: SyntaxError: Cannot use unparenthesized assignment expression on Python 3.9 (syntax was added in Python 3.10)
2724+
test.py:7:5: SyntaxError: Cannot use unparenthesized assignment expression on Python 3.9 (syntax was added in Python 3.10)
2725+
test.py:10:3: SyntaxError: Unparenthesized named expression cannot be used here
2726+
Found 5 errors.
2727+
2728+
----- stderr -----
2729+
"
2730+
);
2731+
}
2732+
26702733
#[test]
26712734
fn match_before_py310() {
26722735
// ok on 3.10

crates/ruff_python_parser/src/parser/expression.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -874,16 +874,16 @@ impl<'src> Parser<'src> {
874874
let lower =
875875
self.parse_named_expression_or_higher(ExpressionContext::starred_conditional());
876876

877-
if self.options.target_version < PythonVersion::PY310
878-
&& lower.is_unparenthesized_named_expr()
879-
{
880-
self.add_unsupported_syntax_error(
881-
UnsupportedSyntaxErrorKind::UnparWalrusBefore310,
882-
lower.range(),
883-
);
884-
}
885-
886877
if self.at_ts(NEWLINE_EOF_SET.union([TokenKind::Rsqb, TokenKind::Comma].into())) {
878+
// check this here because if we don't return, we will emit a ParseError instead
879+
if self.options.target_version < PythonVersion::PY310
880+
&& lower.is_unparenthesized_named_expr()
881+
{
882+
self.add_unsupported_syntax_error(
883+
UnsupportedSyntaxErrorKind::UnparWalrus,
884+
lower.range(),
885+
);
886+
}
887887
return lower.expr;
888888
}
889889

0 commit comments

Comments
 (0)