[semantic-syntax-tests] IrrefutableCasePattern, SingleStarredAssignment, WriteToDebug, InvalidExpression (#17748)
Re: #17526 ## Summary Add integration test for semantic syntax for `IrrefutableCasePattern`, `SingleStarredAssignment`, `WriteToDebug`, and `InvalidExpression`. ## Notes - Following @ntBre's suggestion, I will keep the test coming in batches like this over the next few days in separate PRs to keep the review load per PR manageable while also not spamming too many. - I did not add a test for `del __debug__` which is one of the examples in `crates/ruff_python_parser/src/semantic_errors.rs:1051`. For python version `<= 3.8` there is no error and for `>=3.9` the error is not `WriteToDebug` but `SyntaxError: cannot delete __debug__ on Python 3.9 (syntax was removed in 3.9)`. - The `blacken-docs` bypass is necessary because otherwise the test does not pass pre-commit checks; but we want to check for this faulty syntax. <!-- What's the purpose of the change? What does it do, and why? --> ## Test Plan This is a test.
This commit is contained in:
@@ -228,11 +228,102 @@ for x in *range(10):
|
||||
pass
|
||||
```
|
||||
|
||||
## Irrefutable case pattern
|
||||
|
||||
Irrefutable patterns, i.e. wildcard or capture patterns, must be the last case in a match statement.
|
||||
Following case statements are unreachable.
|
||||
|
||||
```toml
|
||||
[environment]
|
||||
python-version = "3.12"
|
||||
```
|
||||
|
||||
```py
|
||||
value = 5
|
||||
|
||||
match value:
|
||||
# error: [invalid-syntax] "wildcard makes remaining patterns unreachable"
|
||||
case _: # Irrefutable wildcard pattern
|
||||
pass
|
||||
case 5:
|
||||
pass
|
||||
|
||||
match value:
|
||||
# error: [invalid-syntax] "name capture `variable` makes remaining patterns unreachable"
|
||||
case variable: # Irrefutable capture pattern
|
||||
pass
|
||||
case 10:
|
||||
pass
|
||||
```
|
||||
|
||||
## Single starred assignment
|
||||
|
||||
Starred assignment targets cannot appear by themselves. They must be in the context of a list or
|
||||
tuple.
|
||||
|
||||
```py
|
||||
# error: [invalid-syntax] "starred assignment target must be in a list or tuple"
|
||||
*a = [1, 2, 3, 4]
|
||||
```
|
||||
|
||||
## Write to debug
|
||||
|
||||
The special Python builtin `__debug__` should not be modified.
|
||||
|
||||
```toml
|
||||
[environment]
|
||||
python-version = "3.12"
|
||||
```
|
||||
|
||||
```py
|
||||
# error: [invalid-syntax] "cannot assign to `__debug__`"
|
||||
__debug__ = False
|
||||
|
||||
# error: [invalid-syntax] "cannot assign to `__debug__`"
|
||||
def process(__debug__):
|
||||
pass
|
||||
|
||||
# error: [invalid-syntax] "cannot assign to `__debug__`"
|
||||
class Generic[__debug__]:
|
||||
pass
|
||||
```
|
||||
|
||||
## Invalid expression
|
||||
|
||||
Certain expressions like `yield` or inlined walrus assignments are not valid in specific contexts.
|
||||
|
||||
```toml
|
||||
[environment]
|
||||
python-version = "3.12"
|
||||
```
|
||||
|
||||
```py
|
||||
# error: [invalid-type-form] "`yield` expressions are not allowed in type expressions"
|
||||
# error: [invalid-syntax] "yield expression cannot be used within a TypeVar bound"
|
||||
# error: [invalid-syntax] "`yield` statement outside of a function"
|
||||
type X[T: (yield 1)] = int
|
||||
|
||||
# error: [invalid-type-form] "`yield` expressions are not allowed in type expressions"
|
||||
# error: [invalid-syntax] "yield expression cannot be used within a type alias"
|
||||
# error: [invalid-syntax] "`yield` statement outside of a function"
|
||||
type Y = (yield 1)
|
||||
|
||||
# error: [invalid-type-form] "Named expressions are not allowed in type expressions"
|
||||
# error: [invalid-syntax] "named expression cannot be used within a generic definition"
|
||||
def f[T](x: int) -> (y := 3):
|
||||
return x
|
||||
|
||||
# error: [invalid-syntax] "`yield from` statement outside of a function"
|
||||
# error: [invalid-syntax] "yield expression cannot be used within a generic definition"
|
||||
class C[T]((yield from [object])):
|
||||
pass
|
||||
```
|
||||
|
||||
## `await` outside async function
|
||||
|
||||
This error includes `await`, `async for`, `async with`, and `async` comprehensions.
|
||||
|
||||
```python
|
||||
```py
|
||||
async def elements(n):
|
||||
yield n
|
||||
|
||||
@@ -245,7 +336,6 @@ def _():
|
||||
# error: [invalid-syntax] "`async with` outside of an asynchronous function"
|
||||
async with elements(1) as x:
|
||||
...
|
||||
# error: [invalid-syntax] "cannot use an asynchronous comprehension outside of an asynchronous function on Python 3.9 (syntax was added in 3.11)"
|
||||
# error: [invalid-syntax] "asynchronous comprehension outside of an asynchronous function"
|
||||
[x async for x in elements(1)]
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user