Skip to content

Commit ea62fc9

Browse files
maxmynterntBre
authored andcommitted
(tests) Add integration tests for Python Semantic Syntax
for `InvalidStarExpression`, `DuplicateMatchKey`, and `DuplicateMatchClassAttribue`
1 parent 5e2c818 commit ea62fc9

7 files changed

+136
-0
lines changed

crates/ruff_linter/src/linter.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,53 @@ mod tests {
10631063
PythonVersion::PY310,
10641064
"MultipleCaseAssignment"
10651065
)]
1066+
#[test_case(
1067+
"duplicate_match_key",
1068+
"
1069+
match x:
1070+
case {'key': 1, 'key': 2}:
1071+
pass
1072+
",
1073+
PythonVersion::PY310,
1074+
"DuplicateMatchKey"
1075+
)]
1076+
#[test_case(
1077+
"duplicate_match_class_attribute",
1078+
"
1079+
match x:
1080+
case Point(x=1, x=2):
1081+
pass
1082+
",
1083+
PythonVersion::PY310,
1084+
"DuplicateMatchClassAttribute"
1085+
)]
1086+
#[test_case(
1087+
"invalid_star_expression",
1088+
"
1089+
def func():
1090+
return *x
1091+
",
1092+
PythonVersion::PY310,
1093+
"InvalidStarExpression"
1094+
)]
1095+
#[test_case(
1096+
"invalid_star_expression_for",
1097+
"
1098+
for *x in range(10):
1099+
pass
1100+
",
1101+
PythonVersion::PY310,
1102+
"InvalidStarExpression"
1103+
)]
1104+
#[test_case(
1105+
"invalid_star_expression_yield",
1106+
"
1107+
def func():
1108+
yield *x
1109+
",
1110+
PythonVersion::PY310,
1111+
"InvalidStarExpression"
1112+
)]
10661113
fn test_semantic_errors(
10671114
name: &str,
10681115
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+
|

crates/ty_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.

0 commit comments

Comments
 (0)