mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-28 15:34:19 +00:00
[syntax-errors] Tuple unpacking in for
statement iterator clause before Python 3.9 (#16558)
Summary -- This PR reuses a slightly modified version of the `check_tuple_unpacking` method added for detecting unpacking in `return` and `yield` statements to detect the same issue in the iterator clause of `for` loops. I ran into the same issue with a bare `for x in *rest: ...` example (invalid even on Python 3.13) and added it as a comment on https://github.com/astral-sh/ruff/issues/16520. I considered just making this an additional `StarTupleKind` variant as well, but this change was in a different version of Python, so I kept it separate. Test Plan -- New inline tests.
This commit is contained in:
parent
27e9d1fe3e
commit
2382fe1f25
9 changed files with 673 additions and 6 deletions
|
@ -616,6 +616,33 @@ pub enum UnsupportedSyntaxErrorKind {
|
|||
TypeParameterList,
|
||||
TypeAliasStatement,
|
||||
TypeParamDefault,
|
||||
|
||||
/// Represents the use of tuple unpacking in a `for` statement iterator clause before Python
|
||||
/// 3.9.
|
||||
///
|
||||
/// ## Examples
|
||||
///
|
||||
/// Like [`UnsupportedSyntaxErrorKind::StarTuple`] in `return` and `yield` statements, prior to
|
||||
/// Python 3.9, tuple unpacking in the iterator clause of a `for` statement required
|
||||
/// parentheses:
|
||||
///
|
||||
/// ```python
|
||||
/// # valid on Python 3.8 and earlier
|
||||
/// for i in (*a, *b): ...
|
||||
/// ```
|
||||
///
|
||||
/// Omitting the parentheses was invalid:
|
||||
///
|
||||
/// ```python
|
||||
/// for i in *a, *b: ... # SyntaxError
|
||||
/// ```
|
||||
///
|
||||
/// This was changed as part of the [PEG parser rewrite] included in Python 3.9 but not
|
||||
/// documented directly until the [Python 3.11 release].
|
||||
///
|
||||
/// [PEG parser rewrite]: https://peps.python.org/pep-0617/
|
||||
/// [Python 3.11 release]: https://docs.python.org/3/whatsnew/3.11.html#other-language-changes
|
||||
UnparenthesizedUnpackInFor,
|
||||
}
|
||||
|
||||
impl Display for UnsupportedSyntaxError {
|
||||
|
@ -642,6 +669,9 @@ impl Display for UnsupportedSyntaxError {
|
|||
UnsupportedSyntaxErrorKind::TypeParamDefault => {
|
||||
"Cannot set default type for a type parameter"
|
||||
}
|
||||
UnsupportedSyntaxErrorKind::UnparenthesizedUnpackInFor => {
|
||||
"Cannot use iterable unpacking in `for` statements"
|
||||
}
|
||||
};
|
||||
|
||||
write!(
|
||||
|
@ -687,6 +717,9 @@ impl UnsupportedSyntaxErrorKind {
|
|||
UnsupportedSyntaxErrorKind::TypeParameterList => Change::Added(PythonVersion::PY312),
|
||||
UnsupportedSyntaxErrorKind::TypeAliasStatement => Change::Added(PythonVersion::PY312),
|
||||
UnsupportedSyntaxErrorKind::TypeParamDefault => Change::Added(PythonVersion::PY313),
|
||||
UnsupportedSyntaxErrorKind::UnparenthesizedUnpackInFor => {
|
||||
Change::Added(PythonVersion::PY39)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue