[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]: https://github.com/python/cpython/issues/76298
This commit is contained in:
Brent Westbrook 2025-03-06 11:57:20 -05:00 committed by GitHub
parent 0a627ef216
commit 6c14225c66
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 1217 additions and 8 deletions

View file

@ -11,6 +11,7 @@ use ruff_python_ast::{
};
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};
use crate::error::StarTupleKind;
use crate::parser::progress::ParserProgress;
use crate::parser::{helpers, FunctionKind, Parser};
use crate::string::{parse_fstring_literal_element, parse_string_literal, StringType};
@ -2089,10 +2090,27 @@ impl<'src> Parser<'src> {
}
let value = self.at_expr().then(|| {
Box::new(
self.parse_expression_list(ExpressionContext::starred_bitwise_or())
.expr,
)
let parsed_expr = self.parse_expression_list(ExpressionContext::starred_bitwise_or());
// test_ok iter_unpack_yield_py37
// # parse_options: {"target-version": "3.7"}
// rest = (4, 5, 6)
// def g(): yield (1, 2, 3, *rest)
// test_ok iter_unpack_yield_py38
// # 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
// test_err iter_unpack_yield_py37
// # 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
self.check_tuple_unpacking(&parsed_expr, StarTupleKind::Yield);
Box::new(parsed_expr.expr)
});
Expr::Yield(ast::ExprYield {