Skip to content

Commit ea605b1

Browse files
wrap nonlocal statements in inline tests in function scopes
1 parent d1dc233 commit ea605b1

13 files changed

+286
-123
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def _():
2+
nonlocal
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def _():
2+
nonlocal x + 1
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def _():
2+
nonlocal ,
3+
nonlocal x,
4+
nonlocal x, y,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
def _():
2+
nonlocal x
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
def _():
2+
nonlocal x
3+
nonlocal x, y, z

crates/ruff_python_parser/src/parser/statement.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -899,29 +899,33 @@ impl<'src> Parser<'src> {
899899
self.bump(TokenKind::Nonlocal);
900900

901901
// test_err nonlocal_stmt_trailing_comma
902-
// nonlocal ,
903-
// nonlocal x,
904-
// nonlocal x, y,
902+
// def _():
903+
// nonlocal ,
904+
// nonlocal x,
905+
// nonlocal x, y,
905906

906907
// test_err nonlocal_stmt_expression
907-
// nonlocal x + 1
908+
// def _():
909+
// nonlocal x + 1
908910
let names = self.parse_comma_separated_list_into_vec(
909911
RecoveryContextKind::Identifiers,
910912
Parser::parse_identifier,
911913
);
912914

913915
if names.is_empty() {
914916
// test_err nonlocal_stmt_empty
915-
// nonlocal
917+
// def _():
918+
// nonlocal
916919
self.add_error(
917920
ParseErrorType::EmptyNonlocalNames,
918921
self.current_token_range(),
919922
);
920923
}
921924

922925
// test_ok nonlocal_stmt
923-
// nonlocal x
924-
// nonlocal x, y, z
926+
// def _():
927+
// nonlocal x
928+
// nonlocal x, y, z
925929
ast::StmtNonlocal {
926930
range: self.node_range(start),
927931
names,

crates/ruff_python_parser/src/semantic_errors.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ impl SemanticSyntaxChecker {
134134
);
135135
}
136136
Stmt::Nonlocal(ast::StmtNonlocal { range, .. }) => {
137+
// test_ok nonlocal_declaration_at_module_level
138+
// def _():
139+
// nonlocal x
140+
137141
// test_err nonlocal_declaration_at_module_level
138142
// nonlocal x
139143
// nonlocal x, y

crates/ruff_python_parser/tests/fixtures.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ impl SemanticSyntaxContext for SemanticSyntaxCheckerVisitor<'_> {
538538
}
539539

540540
fn in_module_scope(&self) -> bool {
541-
true
541+
self.scopes.len() == 1
542542
}
543543

544544
fn in_function_scope(&self) -> bool {
Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,41 @@
11
---
22
source: crates/ruff_python_parser/tests/fixtures.rs
33
input_file: crates/ruff_python_parser/resources/inline/err/nonlocal_stmt_empty.py
4-
snapshot_kind: text
54
---
65
## AST
76

87
```
98
Module(
109
ModModule {
11-
range: 0..9,
10+
range: 0..22,
1211
body: [
13-
Nonlocal(
14-
StmtNonlocal {
15-
range: 0..8,
16-
names: [],
12+
FunctionDef(
13+
StmtFunctionDef {
14+
range: 0..21,
15+
is_async: false,
16+
decorator_list: [],
17+
name: Identifier {
18+
id: Name("_"),
19+
range: 4..5,
20+
},
21+
type_params: None,
22+
parameters: Parameters {
23+
range: 5..7,
24+
posonlyargs: [],
25+
args: [],
26+
vararg: None,
27+
kwonlyargs: [],
28+
kwarg: None,
29+
},
30+
returns: None,
31+
body: [
32+
Nonlocal(
33+
StmtNonlocal {
34+
range: 13..21,
35+
names: [],
36+
},
37+
),
38+
],
1739
},
1840
),
1941
],
@@ -23,6 +45,7 @@ Module(
2345
## Errors
2446

2547
|
26-
1 | nonlocal
27-
| ^ Syntax Error: Nonlocal statement must have at least one name
48+
1 | def _():
49+
2 | nonlocal
50+
| ^ Syntax Error: Nonlocal statement must have at least one name
2851
|
Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,75 @@
11
---
22
source: crates/ruff_python_parser/tests/fixtures.rs
33
input_file: crates/ruff_python_parser/resources/inline/err/nonlocal_stmt_expression.py
4-
snapshot_kind: text
54
---
65
## AST
76

87
```
98
Module(
109
ModModule {
11-
range: 0..15,
10+
range: 0..28,
1211
body: [
13-
Nonlocal(
14-
StmtNonlocal {
15-
range: 0..10,
16-
names: [
17-
Identifier {
18-
id: Name("x"),
19-
range: 9..10,
20-
},
12+
FunctionDef(
13+
StmtFunctionDef {
14+
range: 0..27,
15+
is_async: false,
16+
decorator_list: [],
17+
name: Identifier {
18+
id: Name("_"),
19+
range: 4..5,
20+
},
21+
type_params: None,
22+
parameters: Parameters {
23+
range: 5..7,
24+
posonlyargs: [],
25+
args: [],
26+
vararg: None,
27+
kwonlyargs: [],
28+
kwarg: None,
29+
},
30+
returns: None,
31+
body: [
32+
Nonlocal(
33+
StmtNonlocal {
34+
range: 13..23,
35+
names: [
36+
Identifier {
37+
id: Name("x"),
38+
range: 22..23,
39+
},
40+
],
41+
},
42+
),
43+
Expr(
44+
StmtExpr {
45+
range: 24..27,
46+
value: UnaryOp(
47+
ExprUnaryOp {
48+
range: 24..27,
49+
op: UAdd,
50+
operand: NumberLiteral(
51+
ExprNumberLiteral {
52+
range: 26..27,
53+
value: Int(
54+
1,
55+
),
56+
},
57+
),
58+
},
59+
),
60+
},
61+
),
2162
],
2263
},
2364
),
24-
Expr(
25-
StmtExpr {
26-
range: 11..14,
27-
value: UnaryOp(
28-
ExprUnaryOp {
29-
range: 11..14,
30-
op: UAdd,
31-
operand: NumberLiteral(
32-
ExprNumberLiteral {
33-
range: 13..14,
34-
value: Int(
35-
1,
36-
),
37-
},
38-
),
39-
},
40-
),
41-
},
42-
),
4365
],
4466
},
4567
)
4668
```
4769
## Errors
4870

4971
|
50-
1 | nonlocal x + 1
51-
| ^ Syntax Error: Simple statements must be separated by newlines or semicolons
72+
1 | def _():
73+
2 | nonlocal x + 1
74+
| ^ Syntax Error: Simple statements must be separated by newlines or semicolons
5275
|

0 commit comments

Comments
 (0)