mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-03 18:28:24 +00:00
Make syntax error for unparenthesized except tuples version specific to before 3.14 (#17660)
What it says on the tin 😄
This commit is contained in:
parent
31e6576971
commit
3c460a7b9a
9 changed files with 526 additions and 274 deletions
|
@ -813,6 +813,41 @@ pub enum UnsupportedSyntaxErrorKind {
|
|||
/// [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,
|
||||
/// Represents the use of multiple exception names in an except clause without an `as` binding, before Python 3.14.
|
||||
///
|
||||
/// ## Examples
|
||||
/// Before Python 3.14, catching multiple exceptions required
|
||||
/// parentheses like so:
|
||||
///
|
||||
/// ```python
|
||||
/// try:
|
||||
/// ...
|
||||
/// except (ExceptionA, ExceptionB, ExceptionC):
|
||||
/// ...
|
||||
/// ```
|
||||
///
|
||||
/// Starting with Python 3.14, thanks to [PEP 758], it was permitted
|
||||
/// to omit the parentheses:
|
||||
///
|
||||
/// ```python
|
||||
/// try:
|
||||
/// ...
|
||||
/// except ExceptionA, ExceptionB, ExceptionC:
|
||||
/// ...
|
||||
/// ```
|
||||
///
|
||||
/// However, parentheses are still required in the presence of an `as`:
|
||||
///
|
||||
/// ```python
|
||||
/// try:
|
||||
/// ...
|
||||
/// except (ExceptionA, ExceptionB, ExceptionC) as e:
|
||||
/// ...
|
||||
/// ```
|
||||
///
|
||||
///
|
||||
/// [PEP 758]: https://peps.python.org/pep-0758/
|
||||
UnparenthesizedExceptionTypes,
|
||||
}
|
||||
|
||||
impl Display for UnsupportedSyntaxError {
|
||||
|
@ -888,6 +923,9 @@ impl Display for UnsupportedSyntaxError {
|
|||
UnsupportedSyntaxErrorKind::UnparenthesizedUnpackInFor => {
|
||||
"Cannot use iterable unpacking in `for` statements"
|
||||
}
|
||||
UnsupportedSyntaxErrorKind::UnparenthesizedExceptionTypes => {
|
||||
"Multiple exception types must be parenthesized"
|
||||
}
|
||||
};
|
||||
|
||||
write!(
|
||||
|
@ -955,6 +993,9 @@ impl UnsupportedSyntaxErrorKind {
|
|||
UnsupportedSyntaxErrorKind::UnparenthesizedUnpackInFor => {
|
||||
Change::Added(PythonVersion::PY39)
|
||||
}
|
||||
UnsupportedSyntaxErrorKind::UnparenthesizedExceptionTypes => {
|
||||
Change::Added(PythonVersion::PY314)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1581,25 +1581,50 @@ impl<'src> Parser<'src> {
|
|||
..
|
||||
})
|
||||
) {
|
||||
// test_err except_stmt_unparenthesized_tuple
|
||||
// try:
|
||||
// pass
|
||||
// except x, y:
|
||||
// pass
|
||||
// except x, y as exc:
|
||||
// pass
|
||||
// try:
|
||||
// pass
|
||||
// except* x, y:
|
||||
// pass
|
||||
// except* x, y as eg:
|
||||
// pass
|
||||
self.add_error(
|
||||
ParseErrorType::OtherError(
|
||||
"Multiple exception types must be parenthesized".to_string(),
|
||||
),
|
||||
&parsed_expr,
|
||||
);
|
||||
if self.at(TokenKind::As) {
|
||||
// test_err except_stmt_unparenthesized_tuple_as
|
||||
// try:
|
||||
// pass
|
||||
// except x, y as exc:
|
||||
// pass
|
||||
// try:
|
||||
// pass
|
||||
// except* x, y as eg:
|
||||
// pass
|
||||
self.add_error(
|
||||
ParseErrorType::OtherError(
|
||||
"Multiple exception types must be parenthesized when using `as`"
|
||||
.to_string(),
|
||||
),
|
||||
&parsed_expr,
|
||||
);
|
||||
} else {
|
||||
// test_err except_stmt_unparenthesized_tuple_no_as_py313
|
||||
// # parse_options: {"target-version": "3.13"}
|
||||
// try:
|
||||
// pass
|
||||
// except x, y:
|
||||
// pass
|
||||
// try:
|
||||
// pass
|
||||
// except* x, y:
|
||||
// pass
|
||||
|
||||
// test_ok except_stmt_unparenthesized_tuple_no_as_py314
|
||||
// # parse_options: {"target-version": "3.14"}
|
||||
// try:
|
||||
// pass
|
||||
// except x, y:
|
||||
// pass
|
||||
// try:
|
||||
// pass
|
||||
// except* x, y:
|
||||
// pass
|
||||
self.add_unsupported_syntax_error(
|
||||
UnsupportedSyntaxErrorKind::UnparenthesizedExceptionTypes,
|
||||
parsed_expr.range(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Some(Box::new(parsed_expr.expr))
|
||||
} else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue