Skip to content

Commit bf6cc0c

Browse files
committed
add mdtests covering all context methods
1 parent 4ac2593 commit bf6cc0c

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,85 @@ async def elements(n):
5959
async def f():
6060
return {n: [x async for x in elements(n)] for n in range(3)}
6161
```
62+
63+
## Late `__future__` import
64+
65+
```py
66+
from collections import namedtuple
67+
68+
# error: [invalid-syntax] "__future__ imports must be at the top of the file"
69+
from __future__ import print_function
70+
```
71+
72+
## Invalid annotation
73+
74+
This one might be a bit redundant with the `invalid-type-form` error.
75+
76+
```toml
77+
[environment]
78+
python-version = "3.12"
79+
```
80+
81+
```py
82+
from __future__ import annotations
83+
84+
# error: [invalid-type-form] "Named expressions are not allowed in type expressions"
85+
# error: [invalid-syntax] "named expression cannot be used within a type annotation"
86+
def f() -> (y := 3): ...
87+
```
88+
89+
## Duplicate `match` key
90+
91+
```toml
92+
[environment]
93+
python-version = "3.10"
94+
```
95+
96+
```py
97+
match 2:
98+
# error: [invalid-syntax] "mapping pattern checks duplicate key `"x"`"
99+
case {"x": 1, "x": 2}:
100+
...
101+
```
102+
103+
## `return`, `yield`, `yield from`, and `await` outside function
104+
105+
```py
106+
# error: [invalid-syntax] "`return` statement outside of a function"
107+
return
108+
109+
# error: [invalid-syntax] "`yield` statement outside of a function"
110+
yield
111+
112+
# error: [invalid-syntax] "`yield from` statement outside of a function"
113+
yield from []
114+
115+
# error: [invalid-syntax] "`await` statement outside of a function"
116+
# error: [invalid-syntax] "`await` outside of an asynchronous function"
117+
await 1
118+
119+
def f():
120+
# error: [invalid-syntax] "`await` outside of an asynchronous function"
121+
await 1
122+
```
123+
124+
Generators are evaluated lazily, so `await` is allowed, even outside of a function.
125+
126+
```py
127+
async def g():
128+
yield 1
129+
130+
(x async for x in g())
131+
```
132+
133+
## Load before `global` declaration
134+
135+
This should be an error, but it's not yet.
136+
137+
TODO implement `SemanticSyntaxContext::global`
138+
139+
```py
140+
def f():
141+
x = 1
142+
global x
143+
```

0 commit comments

Comments
 (0)