Expand Semantic Syntax Coverage (#17725)
Re: #17526 ## Summary Adds tests to red knot and `linter.rs` for the semantic syntax. Specifically add tests for `ReboundComprehensionVariable`, `DuplicateTypeParameter`, and `MultipleCaseAssignment`. Refactor the `test_async_comprehension_in_sync_comprehension` → `test_semantic_error` to be more general for all semantic syntax test cases. ## Test Plan This is a test. ## Question I'm happy to contribute more tests the coming days. Should that happen here or should we merge this PR such that the refactor `test_async_comprehension_in_sync_comprehension` → `test_semantic_error` is available on main and others can chime in, too?
This commit is contained in:
@@ -130,6 +130,62 @@ async def g():
|
||||
(x async for x in g())
|
||||
```
|
||||
|
||||
## Rebound comprehension variable
|
||||
|
||||
Walrus operators cannot rebind variables already in use as iterators:
|
||||
|
||||
```py
|
||||
# error: [invalid-syntax] "assignment expression cannot rebind comprehension variable"
|
||||
[x := 2 for x in range(10)]
|
||||
|
||||
# error: [invalid-syntax] "assignment expression cannot rebind comprehension variable"
|
||||
{y := 5 for y in range(10)}
|
||||
```
|
||||
|
||||
## Multiple case assignments
|
||||
|
||||
Variable names in pattern matching must be unique within a single pattern:
|
||||
|
||||
```toml
|
||||
[environment]
|
||||
python-version = "3.10"
|
||||
```
|
||||
|
||||
```py
|
||||
x = [1, 2]
|
||||
match x:
|
||||
# error: [invalid-syntax] "multiple assignments to name `a` in pattern"
|
||||
case [a, a]:
|
||||
pass
|
||||
case _:
|
||||
pass
|
||||
|
||||
d = {"key": "value"}
|
||||
match d:
|
||||
# error: [invalid-syntax] "multiple assignments to name `b` in pattern"
|
||||
case {"key": b, "other": b}:
|
||||
pass
|
||||
```
|
||||
|
||||
## Duplicate type parameter
|
||||
|
||||
Type parameter names must be unique in a generic class or function definition:
|
||||
|
||||
```toml
|
||||
[environment]
|
||||
python-version = "3.12"
|
||||
```
|
||||
|
||||
```py
|
||||
# error: [invalid-syntax] "duplicate type parameter"
|
||||
class C[T, T]:
|
||||
pass
|
||||
|
||||
# error: [invalid-syntax] "duplicate type parameter"
|
||||
def f[X, Y, X]():
|
||||
pass
|
||||
```
|
||||
|
||||
## `await` outside async function
|
||||
|
||||
This error includes `await`, `async for`, `async with`, and `async` comprehensions.
|
||||
|
||||
Reference in New Issue
Block a user