Skip to content

Commit c54410d

Browse files
committed
(tests) Add integration tests for Python Semantic Syntax
for `InvalidStarExpression`, `DuplicateMatchKey`, and `DuplicateMatchClassAttribue`
1 parent 03d8679 commit c54410d

7 files changed

+136
-0
lines changed

crates/red_knot_python_semantic/resources/mdtest/diagnostics/semantic_syntax_errors.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,26 @@ match 2:
100100
...
101101
```
102102

103+
## Duplicate `match` class attribute
104+
105+
Attribute names in class patterns must be unique:
106+
107+
```toml
108+
[environment]
109+
python-version = "3.10"
110+
```
111+
112+
```py
113+
class Point:
114+
pass
115+
116+
obj = Point()
117+
match obj:
118+
# error: [invalid-syntax] "attribute name `x` repeated in class pattern"
119+
case Point(x=1, x=2):
120+
pass
121+
```
122+
103123
## `return`, `yield`, `yield from`, and `await` outside function
104124

105125
```py
@@ -186,6 +206,28 @@ def f[X, Y, X]():
186206
pass
187207
```
188208

209+
## Invalid star expression
210+
211+
Star expressions can't be used in certain contexts:
212+
213+
```py
214+
def func():
215+
# error: [invalid-syntax] "can't use starred expression here"
216+
return *[1, 2, 3]
217+
218+
def gen():
219+
# error: [invalid-syntax] "can't use starred expression here"
220+
yield * [1, 2, 3]
221+
222+
# error: [invalid-syntax] "can't use starred expression here"
223+
for *x in range(10):
224+
pass
225+
226+
# error: [invalid-syntax] "can't use starred expression here"
227+
for x in *range(10):
228+
pass
229+
```
230+
189231
## `await` outside async function
190232

191233
This error includes `await`, `async for`, `async with`, and `async` comprehensions.

crates/ruff_linter/src/linter.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,6 +1064,53 @@ mod tests {
10641064
PythonVersion::PY310,
10651065
"MultipleCaseAssignment"
10661066
)]
1067+
#[test_case(
1068+
"duplicate_match_key",
1069+
"
1070+
match x:
1071+
case {'key': 1, 'key': 2}:
1072+
pass
1073+
",
1074+
PythonVersion::PY310,
1075+
"DuplicateMatchKey"
1076+
)]
1077+
#[test_case(
1078+
"duplicate_match_class_attribute",
1079+
"
1080+
match x:
1081+
case Point(x=1, x=2):
1082+
pass
1083+
",
1084+
PythonVersion::PY310,
1085+
"DuplicateMatchClassAttribute"
1086+
)]
1087+
#[test_case(
1088+
"invalid_star_expression",
1089+
"
1090+
def func():
1091+
return *x
1092+
",
1093+
PythonVersion::PY310,
1094+
"InvalidStarExpression"
1095+
)]
1096+
#[test_case(
1097+
"invalid_star_expression_for",
1098+
"
1099+
for *x in range(10):
1100+
pass
1101+
",
1102+
PythonVersion::PY310,
1103+
"InvalidStarExpression"
1104+
)]
1105+
#[test_case(
1106+
"invalid_star_expression_yield",
1107+
"
1108+
def func():
1109+
yield *x
1110+
",
1111+
PythonVersion::PY310,
1112+
"InvalidStarExpression"
1113+
)]
10671114
fn test_semantic_errors(
10681115
name: &str,
10691116
contents: &str,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
source: crates/ruff_linter/src/linter.rs
3+
---
4+
<filename>:3:21: SyntaxError: attribute name `x` repeated in class pattern
5+
|
6+
2 | match x:
7+
3 | case Point(x=1, x=2):
8+
| ^
9+
4 | pass
10+
|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
source: crates/ruff_linter/src/linter.rs
3+
---
4+
<filename>:3:21: SyntaxError: mapping pattern checks duplicate key `'key'`
5+
|
6+
2 | match x:
7+
3 | case {'key': 1, 'key': 2}:
8+
| ^^^^^
9+
4 | pass
10+
|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
source: crates/ruff_linter/src/linter.rs
3+
---
4+
<filename>:3:12: SyntaxError: can't use starred expression here
5+
|
6+
2 | def func():
7+
3 | return *x
8+
| ^^
9+
|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
source: crates/ruff_linter/src/linter.rs
3+
---
4+
<filename>:2:5: SyntaxError: can't use starred expression here
5+
|
6+
2 | for *x in range(10):
7+
| ^^
8+
3 | pass
9+
|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
source: crates/ruff_linter/src/linter.rs
3+
---
4+
<filename>:3:11: SyntaxError: can't use starred expression here
5+
|
6+
2 | def func():
7+
3 | yield *x
8+
| ^^
9+
|

0 commit comments

Comments
 (0)