mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-30 13:51:16 +00:00
[semantic-syntax-tests] for for InvalidStarExpression
, DuplicateMatchKey
, and DuplicateMatchClassAttribute
(#17754)
Re: #17526 ## Summary Add integration tests for Python Semantic Syntax for `InvalidStarExpression`, `DuplicateMatchKey`, and `DuplicateMatchClassAttribute`. ## Note - Red knot integration tests for `DuplicateMatchKey` exist already in line 89-101. <!-- What's the purpose of the change? What does it do, and why? --> ## Test Plan This is a test. <!-- How was it tested? -->
This commit is contained in:
parent
965a4dd731
commit
101e1a5ddd
7 changed files with 136 additions and 0 deletions
|
@ -1063,6 +1063,53 @@ mod tests {
|
||||||
PythonVersion::PY310,
|
PythonVersion::PY310,
|
||||||
"MultipleCaseAssignment"
|
"MultipleCaseAssignment"
|
||||||
)]
|
)]
|
||||||
|
#[test_case(
|
||||||
|
"duplicate_match_key",
|
||||||
|
"
|
||||||
|
match x:
|
||||||
|
case {'key': 1, 'key': 2}:
|
||||||
|
pass
|
||||||
|
",
|
||||||
|
PythonVersion::PY310,
|
||||||
|
"DuplicateMatchKey"
|
||||||
|
)]
|
||||||
|
#[test_case(
|
||||||
|
"duplicate_match_class_attribute",
|
||||||
|
"
|
||||||
|
match x:
|
||||||
|
case Point(x=1, x=2):
|
||||||
|
pass
|
||||||
|
",
|
||||||
|
PythonVersion::PY310,
|
||||||
|
"DuplicateMatchClassAttribute"
|
||||||
|
)]
|
||||||
|
#[test_case(
|
||||||
|
"invalid_star_expression",
|
||||||
|
"
|
||||||
|
def func():
|
||||||
|
return *x
|
||||||
|
",
|
||||||
|
PythonVersion::PY310,
|
||||||
|
"InvalidStarExpression"
|
||||||
|
)]
|
||||||
|
#[test_case(
|
||||||
|
"invalid_star_expression_for",
|
||||||
|
"
|
||||||
|
for *x in range(10):
|
||||||
|
pass
|
||||||
|
",
|
||||||
|
PythonVersion::PY310,
|
||||||
|
"InvalidStarExpression"
|
||||||
|
)]
|
||||||
|
#[test_case(
|
||||||
|
"invalid_star_expression_yield",
|
||||||
|
"
|
||||||
|
def func():
|
||||||
|
yield *x
|
||||||
|
",
|
||||||
|
PythonVersion::PY310,
|
||||||
|
"InvalidStarExpression"
|
||||||
|
)]
|
||||||
fn test_semantic_errors(
|
fn test_semantic_errors(
|
||||||
name: &str,
|
name: &str,
|
||||||
contents: &str,
|
contents: &str,
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff_linter/src/linter.rs
|
||||||
|
---
|
||||||
|
<filename>:3:21: SyntaxError: attribute name `x` repeated in class pattern
|
||||||
|
|
|
||||||
|
2 | match x:
|
||||||
|
3 | case Point(x=1, x=2):
|
||||||
|
| ^
|
||||||
|
4 | pass
|
||||||
|
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff_linter/src/linter.rs
|
||||||
|
---
|
||||||
|
<filename>:3:21: SyntaxError: mapping pattern checks duplicate key `'key'`
|
||||||
|
|
|
||||||
|
2 | match x:
|
||||||
|
3 | case {'key': 1, 'key': 2}:
|
||||||
|
| ^^^^^
|
||||||
|
4 | pass
|
||||||
|
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff_linter/src/linter.rs
|
||||||
|
---
|
||||||
|
<filename>:3:12: SyntaxError: Starred expression cannot be used here
|
||||||
|
|
|
||||||
|
2 | def func():
|
||||||
|
3 | return *x
|
||||||
|
| ^^
|
||||||
|
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff_linter/src/linter.rs
|
||||||
|
---
|
||||||
|
<filename>:2:5: SyntaxError: Starred expression cannot be used here
|
||||||
|
|
|
||||||
|
2 | for *x in range(10):
|
||||||
|
| ^^
|
||||||
|
3 | pass
|
||||||
|
|
|
|
@ -0,0 +1,9 @@
|
||||||
|
---
|
||||||
|
source: crates/ruff_linter/src/linter.rs
|
||||||
|
---
|
||||||
|
<filename>:3:11: SyntaxError: Starred expression cannot be used here
|
||||||
|
|
|
||||||
|
2 | def func():
|
||||||
|
3 | yield *x
|
||||||
|
| ^^
|
||||||
|
|
|
|
@ -100,6 +100,26 @@ match 2:
|
||||||
...
|
...
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Duplicate `match` class attribute
|
||||||
|
|
||||||
|
Attribute names in class patterns must be unique:
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[environment]
|
||||||
|
python-version = "3.10"
|
||||||
|
```
|
||||||
|
|
||||||
|
```py
|
||||||
|
class Point:
|
||||||
|
pass
|
||||||
|
|
||||||
|
obj = Point()
|
||||||
|
match obj:
|
||||||
|
# error: [invalid-syntax] "attribute name `x` repeated in class pattern"
|
||||||
|
case Point(x=1, x=2):
|
||||||
|
pass
|
||||||
|
```
|
||||||
|
|
||||||
## `return`, `yield`, `yield from`, and `await` outside function
|
## `return`, `yield`, `yield from`, and `await` outside function
|
||||||
|
|
||||||
```py
|
```py
|
||||||
|
@ -186,6 +206,28 @@ def f[X, Y, X]():
|
||||||
pass
|
pass
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Invalid star expression
|
||||||
|
|
||||||
|
Star expressions can't be used in certain contexts:
|
||||||
|
|
||||||
|
```py
|
||||||
|
def func():
|
||||||
|
# error: [invalid-syntax] "Starred expression cannot be used here"
|
||||||
|
return *[1, 2, 3]
|
||||||
|
|
||||||
|
def gen():
|
||||||
|
# error: [invalid-syntax] "Starred expression cannot be used here"
|
||||||
|
yield * [1, 2, 3]
|
||||||
|
|
||||||
|
# error: [invalid-syntax] "Starred expression cannot be used here"
|
||||||
|
for *x in range(10):
|
||||||
|
pass
|
||||||
|
|
||||||
|
# error: [invalid-syntax] "Starred expression cannot be used here"
|
||||||
|
for x in *range(10):
|
||||||
|
pass
|
||||||
|
```
|
||||||
|
|
||||||
## `await` outside async function
|
## `await` outside async function
|
||||||
|
|
||||||
This error includes `await`, `async for`, `async with`, and `async` comprehensions.
|
This error includes `await`, `async for`, `async with`, and `async` comprehensions.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue