Skip to content

Commit ea4a248

Browse files
committed
move inline tests to linter, simplify test visitor
1 parent fa8412a commit ea4a248

File tree

53 files changed

+115
-1139
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+115
-1139
lines changed

crates/ruff_linter/resources/test/fixtures/syntax_errors/yield_scope.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
yield # error
2+
yield 1 # error
3+
yield from 1 # error
4+
await 1 # error
5+
[(yield x) for x in range(3)] # error
6+
7+
8+
def f():
9+
yield # okay
10+
yield 1 # okay
11+
yield from 1 # okay
12+
await 1 # okay
13+
14+
15+
lambda: (yield) # okay
16+
lambda: (yield 1) # okay
17+
lambda: (yield from 1) # okay
18+
lambda: (await 1) # okay
19+
20+
121
def outer():
222
class C:
323
yield 1 # error
@@ -17,4 +37,5 @@ class C:
1737

1838
lambda x: await x # okay for now, lambda breaks _async_ scope but is a function
1939

40+
2041
await 1 # error
Lines changed: 91 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,117 @@
11
---
22
source: crates/ruff_linter/src/linter.rs
33
---
4-
resources/test/fixtures/syntax_errors/yield_scope.py:3:9: F704 `yield` statement outside of a function
4+
resources/test/fixtures/syntax_errors/yield_scope.py:1:1: F704 `yield` statement outside of a function
55
|
6-
1 | def outer():
7-
2 | class C:
8-
3 | yield 1 # error
9-
| ^^^^^^^ F704
10-
4 |
11-
5 | [(yield 1) for x in range(3)] # error
6+
1 | yield # error
7+
| ^^^^^ F704
8+
2 | yield 1 # error
9+
3 | yield from 1 # error
1210
|
1311

14-
resources/test/fixtures/syntax_errors/yield_scope.py:5:7: F704 `yield` statement outside of a function
12+
resources/test/fixtures/syntax_errors/yield_scope.py:2:1: F704 `yield` statement outside of a function
1513
|
16-
3 | yield 1 # error
17-
4 |
18-
5 | [(yield 1) for x in range(3)] # error
19-
| ^^^^^^^ F704
20-
6 | ((yield 1) for x in range(3)) # error
21-
7 | {(yield 1) for x in range(3)} # error
14+
1 | yield # error
15+
2 | yield 1 # error
16+
| ^^^^^^^ F704
17+
3 | yield from 1 # error
18+
4 | await 1 # error
2219
|
2320

24-
resources/test/fixtures/syntax_errors/yield_scope.py:6:7: F704 `yield` statement outside of a function
21+
resources/test/fixtures/syntax_errors/yield_scope.py:3:1: F704 `yield from` statement outside of a function
2522
|
26-
5 | [(yield 1) for x in range(3)] # error
27-
6 | ((yield 1) for x in range(3)) # error
28-
| ^^^^^^^ F704
29-
7 | {(yield 1) for x in range(3)} # error
30-
8 | {(yield 1): 0 for x in range(3)} # error
23+
1 | yield # error
24+
2 | yield 1 # error
25+
3 | yield from 1 # error
26+
| ^^^^^^^^^^^^ F704
27+
4 | await 1 # error
28+
5 | [(yield x) for x in range(3)] # error
3129
|
3230

33-
resources/test/fixtures/syntax_errors/yield_scope.py:7:7: F704 `yield` statement outside of a function
31+
resources/test/fixtures/syntax_errors/yield_scope.py:4:1: F704 `await` statement outside of a function
3432
|
35-
5 | [(yield 1) for x in range(3)] # error
36-
6 | ((yield 1) for x in range(3)) # error
37-
7 | {(yield 1) for x in range(3)} # error
38-
| ^^^^^^^ F704
39-
8 | {(yield 1): 0 for x in range(3)} # error
40-
9 | {0: (yield 1) for x in range(3)} # error
33+
2 | yield 1 # error
34+
3 | yield from 1 # error
35+
4 | await 1 # error
36+
| ^^^^^^^ F704
37+
5 | [(yield x) for x in range(3)] # error
4138
|
4239

43-
resources/test/fixtures/syntax_errors/yield_scope.py:8:7: F704 `yield` statement outside of a function
40+
resources/test/fixtures/syntax_errors/yield_scope.py:5:3: F704 `yield` statement outside of a function
4441
|
45-
6 | ((yield 1) for x in range(3)) # error
46-
7 | {(yield 1) for x in range(3)} # error
47-
8 | {(yield 1): 0 for x in range(3)} # error
48-
| ^^^^^^^ F704
49-
9 | {0: (yield 1) for x in range(3)} # error
42+
3 | yield from 1 # error
43+
4 | await 1 # error
44+
5 | [(yield x) for x in range(3)] # error
45+
| ^^^^^^^ F704
5046
|
5147

52-
resources/test/fixtures/syntax_errors/yield_scope.py:9:10: F704 `yield` statement outside of a function
53-
|
54-
7 | {(yield 1) for x in range(3)} # error
55-
8 | {(yield 1): 0 for x in range(3)} # error
56-
9 | {0: (yield 1) for x in range(3)} # error
57-
| ^^^^^^^ F704
58-
|
48+
resources/test/fixtures/syntax_errors/yield_scope.py:23:9: F704 `yield` statement outside of a function
49+
|
50+
21 | def outer():
51+
22 | class C:
52+
23 | yield 1 # error
53+
| ^^^^^^^ F704
54+
24 |
55+
25 | [(yield 1) for x in range(3)] # error
56+
|
57+
58+
resources/test/fixtures/syntax_errors/yield_scope.py:25:7: F704 `yield` statement outside of a function
59+
|
60+
23 | yield 1 # error
61+
24 |
62+
25 | [(yield 1) for x in range(3)] # error
63+
| ^^^^^^^ F704
64+
26 | ((yield 1) for x in range(3)) # error
65+
27 | {(yield 1) for x in range(3)} # error
66+
|
67+
68+
resources/test/fixtures/syntax_errors/yield_scope.py:26:7: F704 `yield` statement outside of a function
69+
|
70+
25 | [(yield 1) for x in range(3)] # error
71+
26 | ((yield 1) for x in range(3)) # error
72+
| ^^^^^^^ F704
73+
27 | {(yield 1) for x in range(3)} # error
74+
28 | {(yield 1): 0 for x in range(3)} # error
75+
|
76+
77+
resources/test/fixtures/syntax_errors/yield_scope.py:27:7: F704 `yield` statement outside of a function
78+
|
79+
25 | [(yield 1) for x in range(3)] # error
80+
26 | ((yield 1) for x in range(3)) # error
81+
27 | {(yield 1) for x in range(3)} # error
82+
| ^^^^^^^ F704
83+
28 | {(yield 1): 0 for x in range(3)} # error
84+
29 | {0: (yield 1) for x in range(3)} # error
85+
|
86+
87+
resources/test/fixtures/syntax_errors/yield_scope.py:28:7: F704 `yield` statement outside of a function
88+
|
89+
26 | ((yield 1) for x in range(3)) # error
90+
27 | {(yield 1) for x in range(3)} # error
91+
28 | {(yield 1): 0 for x in range(3)} # error
92+
| ^^^^^^^ F704
93+
29 | {0: (yield 1) for x in range(3)} # error
94+
|
95+
96+
resources/test/fixtures/syntax_errors/yield_scope.py:29:10: F704 `yield` statement outside of a function
97+
|
98+
27 | {(yield 1) for x in range(3)} # error
99+
28 | {(yield 1): 0 for x in range(3)} # error
100+
29 | {0: (yield 1) for x in range(3)} # error
101+
| ^^^^^^^ F704
102+
|
59103

60-
resources/test/fixtures/syntax_errors/yield_scope.py:16:10: F704 `await` statement outside of a function
104+
resources/test/fixtures/syntax_errors/yield_scope.py:36:10: F704 `await` statement outside of a function
61105
|
62-
15 | class C:
63-
16 | [await x for x in range(3)] # error, classes break async scope
106+
35 | class C:
107+
36 | [await x for x in range(3)] # error, classes break async scope
64108
| ^^^^^^^ F704
65-
17 |
66-
18 | lambda x: await x # okay for now, lambda breaks _async_ scope but is a function
109+
37 |
110+
38 | lambda x: await x # okay for now, lambda breaks _async_ scope but is a function
67111
|
68112

69-
resources/test/fixtures/syntax_errors/yield_scope.py:20:1: F704 `await` statement outside of a function
113+
resources/test/fixtures/syntax_errors/yield_scope.py:41:1: F704 `await` statement outside of a function
70114
|
71-
18 | lambda x: await x # okay for now, lambda breaks _async_ scope but is a function
72-
19 |
73-
20 | await 1 # error
115+
41 | await 1 # error
74116
| ^^^^^^^ F704
75117
|

crates/ruff_python_parser/resources/inline/err/yield_outside_function.py

Lines changed: 0 additions & 5 deletions
This file was deleted.

crates/ruff_python_parser/resources/inline/ok/yield_inside_function.py

Lines changed: 0 additions & 10 deletions
This file was deleted.

crates/ruff_python_parser/src/semantic_errors.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -622,24 +622,6 @@ impl SemanticSyntaxChecker {
622622
return;
623623
}
624624

625-
// test_err yield_outside_function
626-
// yield 1
627-
// yield from 1
628-
// await 1
629-
// yield
630-
// [(yield x) for x in range(3)]
631-
632-
// test_ok yield_inside_function
633-
// def f():
634-
// yield 1
635-
// yield from 1
636-
// await 1
637-
// yield
638-
//
639-
// lambda: (yield)
640-
// lambda: (yield 1)
641-
// lambda: (yield from 1)
642-
// lambda: (await 1)
643625
Self::add_error(
644626
ctx,
645627
SemanticSyntaxErrorKind::YieldOutsideFunction(kind),

crates/ruff_python_parser/tests/fixtures.rs

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -467,16 +467,6 @@ enum Scope {
467467
Class,
468468
}
469469

470-
impl Scope {
471-
fn is_module(&self) -> bool {
472-
matches!(self, Self::Module)
473-
}
474-
475-
fn is_function(&self) -> bool {
476-
matches!(self, Self::Function { .. })
477-
}
478-
}
479-
480470
struct SemanticSyntaxCheckerVisitor<'a> {
481471
checker: SemanticSyntaxChecker,
482472
diagnostics: RefCell<Vec<SemanticSyntaxError>>,
@@ -511,15 +501,6 @@ impl<'a> SemanticSyntaxCheckerVisitor<'a> {
511501
f(&mut checker, self);
512502
self.checker = checker;
513503
}
514-
515-
/// Returns an iterator over all scopes, starting from the current [`Scope`].
516-
fn scopes(&self) -> impl Iterator<Item = &Scope> {
517-
self.scopes.iter().rev()
518-
}
519-
520-
fn current_scope(&self) -> &Scope {
521-
self.scopes().next().unwrap()
522-
}
523504
}
524505

525506
impl SemanticSyntaxContext for SemanticSyntaxCheckerVisitor<'_> {
@@ -566,26 +547,19 @@ impl SemanticSyntaxContext for SemanticSyntaxCheckerVisitor<'_> {
566547
}
567548

568549
fn in_module_scope(&self) -> bool {
569-
self.current_scope().is_module()
550+
true
570551
}
571552

572553
fn in_function_scope(&self) -> bool {
573-
self.current_scope().is_function()
554+
true
574555
}
575556

576557
fn in_notebook(&self) -> bool {
577558
false
578559
}
579560

580561
fn in_function_context(&self) -> bool {
581-
for scope in self.scopes() {
582-
match scope {
583-
Scope::Class => return false,
584-
Scope::Function { .. } => return true,
585-
Scope::Comprehension { .. } | Scope::Module => {}
586-
}
587-
}
588-
false
562+
true
589563
}
590564
}
591565

crates/ruff_python_parser/tests/snapshots/invalid_syntax@ann_assign_stmt_invalid_annotation.py.snap

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -198,23 +198,3 @@ Module(
198198
4 | x: y := int = 1
199199
| ^^ Syntax Error: Expected a statement
200200
|
201-
202-
203-
## Semantic Syntax Errors
204-
205-
|
206-
1 | x: *int = 1
207-
2 | x: yield a = 1
208-
| ^^^^^^^ Syntax Error: `yield` statement outside of a function
209-
3 | x: yield from b = 1
210-
4 | x: y := int = 1
211-
|
212-
213-
214-
|
215-
1 | x: *int = 1
216-
2 | x: yield a = 1
217-
3 | x: yield from b = 1
218-
| ^^^^^^^^^^^^ Syntax Error: `yield from` statement outside of a function
219-
4 | x: y := int = 1
220-
|

crates/ruff_python_parser/tests/snapshots/invalid_syntax@assert_invalid_msg_expr.py.snap

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,3 @@ Module(
158158
4 | assert False, x := 1
159159
| ^^ Syntax Error: Expected a statement
160160
|
161-
162-
163-
## Semantic Syntax Errors
164-
165-
|
166-
1 | assert False, *x
167-
2 | assert False, assert x
168-
3 | assert False, yield x
169-
| ^^^^^^^ Syntax Error: `yield` statement outside of a function
170-
4 | assert False, x := 1
171-
|

crates/ruff_python_parser/tests/snapshots/invalid_syntax@assert_invalid_test_expr.py.snap

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,3 @@ Module(
146146
4 | assert x := 1
147147
| ^^ Syntax Error: Expected a statement
148148
|
149-
150-
151-
## Semantic Syntax Errors
152-
153-
|
154-
1 | assert *x
155-
2 | assert assert x
156-
3 | assert yield x
157-
| ^^^^^^^ Syntax Error: `yield` statement outside of a function
158-
4 | assert x := 1
159-
|

crates/ruff_python_parser/tests/snapshots/invalid_syntax@assign_stmt_invalid_value_expr.py.snap

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -254,24 +254,3 @@ Module(
254254
5 | x = x := 1
255255
| ^^ Syntax Error: Expected a statement
256256
|
257-
258-
259-
## Semantic Syntax Errors
260-
261-
|
262-
1 | x = *a and b
263-
2 | x = *yield x
264-
| ^^^^^^^ Syntax Error: `yield` statement outside of a function
265-
3 | x = *yield from x
266-
4 | x = *lambda x: x
267-
|
268-
269-
270-
|
271-
1 | x = *a and b
272-
2 | x = *yield x
273-
3 | x = *yield from x
274-
| ^^^^^^^^^^^^ Syntax Error: `yield from` statement outside of a function
275-
4 | x = *lambda x: x
276-
5 | x = x := 1
277-
|

0 commit comments

Comments
 (0)