-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[syntax-errors] Tuple unpacking in
return
and yield
before Python…
… 3.8 (#16485) Summary -- Checks for tuple unpacking in `return` and `yield` statements before Python 3.8, as described [here]. Test Plan -- Inline tests. [here]: python/cpython#76298
- Loading branch information
Showing
15 changed files
with
1,217 additions
and
8 deletions.
There are no files selected for viewing
3 changes: 3 additions & 0 deletions
3
crates/ruff_python_parser/resources/inline/err/iter_unpack_return_py37.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# parse_options: {"target-version": "3.7"} | ||
rest = (4, 5, 6) | ||
def f(): return 1, 2, 3, *rest |
4 changes: 4 additions & 0 deletions
4
crates/ruff_python_parser/resources/inline/err/iter_unpack_yield_py37.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# parse_options: {"target-version": "3.7"} | ||
rest = (4, 5, 6) | ||
def g(): yield 1, 2, 3, *rest | ||
def h(): yield 1, (yield 2, *rest), 3 |
3 changes: 3 additions & 0 deletions
3
crates/ruff_python_parser/resources/inline/ok/iter_unpack_return_py37.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# parse_options: {"target-version": "3.7"} | ||
rest = (4, 5, 6) | ||
def f(): return (1, 2, 3, *rest) |
3 changes: 3 additions & 0 deletions
3
crates/ruff_python_parser/resources/inline/ok/iter_unpack_return_py38.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# parse_options: {"target-version": "3.8"} | ||
rest = (4, 5, 6) | ||
def f(): return 1, 2, 3, *rest |
3 changes: 3 additions & 0 deletions
3
crates/ruff_python_parser/resources/inline/ok/iter_unpack_yield_py37.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# parse_options: {"target-version": "3.7"} | ||
rest = (4, 5, 6) | ||
def g(): yield (1, 2, 3, *rest) |
4 changes: 4 additions & 0 deletions
4
crates/ruff_python_parser/resources/inline/ok/iter_unpack_yield_py38.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# parse_options: {"target-version": "3.8"} | ||
rest = (4, 5, 6) | ||
def g(): yield 1, 2, 3, *rest | ||
def h(): yield 1, (yield 2, *rest), 3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
crates/ruff_python_parser/tests/snapshots/invalid_syntax@iter_unpack_return_py37.py.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
--- | ||
source: crates/ruff_python_parser/tests/fixtures.rs | ||
input_file: crates/ruff_python_parser/resources/inline/err/iter_unpack_return_py37.py | ||
--- | ||
## AST | ||
|
||
``` | ||
Module( | ||
ModModule { | ||
range: 0..91, | ||
body: [ | ||
Assign( | ||
StmtAssign { | ||
range: 43..59, | ||
targets: [ | ||
Name( | ||
ExprName { | ||
range: 43..47, | ||
id: Name("rest"), | ||
ctx: Store, | ||
}, | ||
), | ||
], | ||
value: Tuple( | ||
ExprTuple { | ||
range: 50..59, | ||
elts: [ | ||
NumberLiteral( | ||
ExprNumberLiteral { | ||
range: 51..52, | ||
value: Int( | ||
4, | ||
), | ||
}, | ||
), | ||
NumberLiteral( | ||
ExprNumberLiteral { | ||
range: 54..55, | ||
value: Int( | ||
5, | ||
), | ||
}, | ||
), | ||
NumberLiteral( | ||
ExprNumberLiteral { | ||
range: 57..58, | ||
value: Int( | ||
6, | ||
), | ||
}, | ||
), | ||
], | ||
ctx: Load, | ||
parenthesized: true, | ||
}, | ||
), | ||
}, | ||
), | ||
FunctionDef( | ||
StmtFunctionDef { | ||
range: 60..90, | ||
is_async: false, | ||
decorator_list: [], | ||
name: Identifier { | ||
id: Name("f"), | ||
range: 64..65, | ||
}, | ||
type_params: None, | ||
parameters: Parameters { | ||
range: 65..67, | ||
posonlyargs: [], | ||
args: [], | ||
vararg: None, | ||
kwonlyargs: [], | ||
kwarg: None, | ||
}, | ||
returns: None, | ||
body: [ | ||
Return( | ||
StmtReturn { | ||
range: 69..90, | ||
value: Some( | ||
Tuple( | ||
ExprTuple { | ||
range: 76..90, | ||
elts: [ | ||
NumberLiteral( | ||
ExprNumberLiteral { | ||
range: 76..77, | ||
value: Int( | ||
1, | ||
), | ||
}, | ||
), | ||
NumberLiteral( | ||
ExprNumberLiteral { | ||
range: 79..80, | ||
value: Int( | ||
2, | ||
), | ||
}, | ||
), | ||
NumberLiteral( | ||
ExprNumberLiteral { | ||
range: 82..83, | ||
value: Int( | ||
3, | ||
), | ||
}, | ||
), | ||
Starred( | ||
ExprStarred { | ||
range: 85..90, | ||
value: Name( | ||
ExprName { | ||
range: 86..90, | ||
id: Name("rest"), | ||
ctx: Load, | ||
}, | ||
), | ||
ctx: Load, | ||
}, | ||
), | ||
], | ||
ctx: Load, | ||
parenthesized: false, | ||
}, | ||
), | ||
), | ||
}, | ||
), | ||
], | ||
}, | ||
), | ||
], | ||
}, | ||
) | ||
``` | ||
## Unsupported Syntax Errors | ||
|
||
| | ||
1 | # parse_options: {"target-version": "3.7"} | ||
2 | rest = (4, 5, 6) | ||
3 | def f(): return 1, 2, 3, *rest | ||
| ^^^^^ Syntax Error: Cannot use iterable unpacking in return statements on Python 3.7 (syntax was added in Python 3.8) | ||
| |
Oops, something went wrong.