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:
Dylan 2025-04-29 07:55:30 -05:00 committed by GitHub
parent 31e6576971
commit 3c460a7b9a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 526 additions and 274 deletions

View file

@ -1,12 +1,8 @@
try:
pass
except x, y:
pass
except x, y as exc:
pass
try:
pass
except* x, y:
pass
except* x, y as eg:
pass

View file

@ -0,0 +1,9 @@
# parse_options: {"target-version": "3.13"}
try:
pass
except x, y:
pass
try:
pass
except* x, y:
pass

View file

@ -0,0 +1,9 @@
# parse_options: {"target-version": "3.14"}
try:
pass
except x, y:
pass
try:
pass
except* x, y:
pass

View file

@ -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)
}
}
}

View file

@ -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 {

View file

@ -1,251 +0,0 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/except_stmt_unparenthesized_tuple.py
snapshot_kind: text
---
## AST
```
Module(
ModModule {
range: 0..131,
body: [
Try(
StmtTry {
range: 0..64,
body: [
Pass(
StmtPass {
range: 9..13,
},
),
],
handlers: [
ExceptHandler(
ExceptHandlerExceptHandler {
range: 14..35,
type_: Some(
Tuple(
ExprTuple {
range: 21..25,
elts: [
Name(
ExprName {
range: 21..22,
id: Name("x"),
ctx: Load,
},
),
Name(
ExprName {
range: 24..25,
id: Name("y"),
ctx: Load,
},
),
],
ctx: Load,
parenthesized: false,
},
),
),
name: None,
body: [
Pass(
StmtPass {
range: 31..35,
},
),
],
},
),
ExceptHandler(
ExceptHandlerExceptHandler {
range: 36..64,
type_: Some(
Tuple(
ExprTuple {
range: 43..47,
elts: [
Name(
ExprName {
range: 43..44,
id: Name("x"),
ctx: Load,
},
),
Name(
ExprName {
range: 46..47,
id: Name("y"),
ctx: Load,
},
),
],
ctx: Load,
parenthesized: false,
},
),
),
name: Some(
Identifier {
id: Name("exc"),
range: 51..54,
},
),
body: [
Pass(
StmtPass {
range: 60..64,
},
),
],
},
),
],
orelse: [],
finalbody: [],
is_star: false,
},
),
Try(
StmtTry {
range: 65..130,
body: [
Pass(
StmtPass {
range: 74..78,
},
),
],
handlers: [
ExceptHandler(
ExceptHandlerExceptHandler {
range: 79..101,
type_: Some(
Tuple(
ExprTuple {
range: 87..91,
elts: [
Name(
ExprName {
range: 87..88,
id: Name("x"),
ctx: Load,
},
),
Name(
ExprName {
range: 90..91,
id: Name("y"),
ctx: Load,
},
),
],
ctx: Load,
parenthesized: false,
},
),
),
name: None,
body: [
Pass(
StmtPass {
range: 97..101,
},
),
],
},
),
ExceptHandler(
ExceptHandlerExceptHandler {
range: 102..130,
type_: Some(
Tuple(
ExprTuple {
range: 110..114,
elts: [
Name(
ExprName {
range: 110..111,
id: Name("x"),
ctx: Load,
},
),
Name(
ExprName {
range: 113..114,
id: Name("y"),
ctx: Load,
},
),
],
ctx: Load,
parenthesized: false,
},
),
),
name: Some(
Identifier {
id: Name("eg"),
range: 118..120,
},
),
body: [
Pass(
StmtPass {
range: 126..130,
},
),
],
},
),
],
orelse: [],
finalbody: [],
is_star: true,
},
),
],
},
)
```
## Errors
|
1 | try:
2 | pass
3 | except x, y:
| ^^^^ Syntax Error: Multiple exception types must be parenthesized
4 | pass
5 | except x, y as exc:
|
|
3 | except x, y:
4 | pass
5 | except x, y as exc:
| ^^^^ Syntax Error: Multiple exception types must be parenthesized
6 | pass
7 | try:
|
|
7 | try:
8 | pass
9 | except* x, y:
| ^^^^ Syntax Error: Multiple exception types must be parenthesized
10 | pass
11 | except* x, y as eg:
|
|
9 | except* x, y:
10 | pass
11 | except* x, y as eg:
| ^^^^ Syntax Error: Multiple exception types must be parenthesized
12 | pass
|

View file

@ -0,0 +1,154 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/except_stmt_unparenthesized_tuple_as.py
---
## AST
```
Module(
ModModule {
range: 0..86,
body: [
Try(
StmtTry {
range: 0..42,
body: [
Pass(
StmtPass {
range: 9..13,
},
),
],
handlers: [
ExceptHandler(
ExceptHandlerExceptHandler {
range: 14..42,
type_: Some(
Tuple(
ExprTuple {
range: 21..25,
elts: [
Name(
ExprName {
range: 21..22,
id: Name("x"),
ctx: Load,
},
),
Name(
ExprName {
range: 24..25,
id: Name("y"),
ctx: Load,
},
),
],
ctx: Load,
parenthesized: false,
},
),
),
name: Some(
Identifier {
id: Name("exc"),
range: 29..32,
},
),
body: [
Pass(
StmtPass {
range: 38..42,
},
),
],
},
),
],
orelse: [],
finalbody: [],
is_star: false,
},
),
Try(
StmtTry {
range: 43..85,
body: [
Pass(
StmtPass {
range: 52..56,
},
),
],
handlers: [
ExceptHandler(
ExceptHandlerExceptHandler {
range: 57..85,
type_: Some(
Tuple(
ExprTuple {
range: 65..69,
elts: [
Name(
ExprName {
range: 65..66,
id: Name("x"),
ctx: Load,
},
),
Name(
ExprName {
range: 68..69,
id: Name("y"),
ctx: Load,
},
),
],
ctx: Load,
parenthesized: false,
},
),
),
name: Some(
Identifier {
id: Name("eg"),
range: 73..75,
},
),
body: [
Pass(
StmtPass {
range: 81..85,
},
),
],
},
),
],
orelse: [],
finalbody: [],
is_star: true,
},
),
],
},
)
```
## Errors
|
1 | try:
2 | pass
3 | except x, y as exc:
| ^^^^ Syntax Error: Multiple exception types must be parenthesized when using `as`
4 | pass
5 | try:
|
|
5 | try:
6 | pass
7 | except* x, y as eg:
| ^^^^ Syntax Error: Multiple exception types must be parenthesized when using `as`
8 | pass
|

View file

@ -0,0 +1,144 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/err/except_stmt_unparenthesized_tuple_no_as_py313.py
---
## AST
```
Module(
ModModule {
range: 0..117,
body: [
Try(
StmtTry {
range: 44..79,
body: [
Pass(
StmtPass {
range: 53..57,
},
),
],
handlers: [
ExceptHandler(
ExceptHandlerExceptHandler {
range: 58..79,
type_: Some(
Tuple(
ExprTuple {
range: 65..69,
elts: [
Name(
ExprName {
range: 65..66,
id: Name("x"),
ctx: Load,
},
),
Name(
ExprName {
range: 68..69,
id: Name("y"),
ctx: Load,
},
),
],
ctx: Load,
parenthesized: false,
},
),
),
name: None,
body: [
Pass(
StmtPass {
range: 75..79,
},
),
],
},
),
],
orelse: [],
finalbody: [],
is_star: false,
},
),
Try(
StmtTry {
range: 80..116,
body: [
Pass(
StmtPass {
range: 89..93,
},
),
],
handlers: [
ExceptHandler(
ExceptHandlerExceptHandler {
range: 94..116,
type_: Some(
Tuple(
ExprTuple {
range: 102..106,
elts: [
Name(
ExprName {
range: 102..103,
id: Name("x"),
ctx: Load,
},
),
Name(
ExprName {
range: 105..106,
id: Name("y"),
ctx: Load,
},
),
],
ctx: Load,
parenthesized: false,
},
),
),
name: None,
body: [
Pass(
StmtPass {
range: 112..116,
},
),
],
},
),
],
orelse: [],
finalbody: [],
is_star: true,
},
),
],
},
)
```
## Unsupported Syntax Errors
|
2 | try:
3 | pass
4 | except x, y:
| ^^^^ Syntax Error: Multiple exception types must be parenthesized on Python 3.13 (syntax was added in Python 3.14)
5 | pass
6 | try:
|
|
6 | try:
7 | pass
8 | except* x, y:
| ^^^^ Syntax Error: Multiple exception types must be parenthesized on Python 3.13 (syntax was added in Python 3.14)
9 | pass
|

View file

@ -0,0 +1,125 @@
---
source: crates/ruff_python_parser/tests/fixtures.rs
input_file: crates/ruff_python_parser/resources/inline/ok/except_stmt_unparenthesized_tuple_no_as_py314.py
---
## AST
```
Module(
ModModule {
range: 0..117,
body: [
Try(
StmtTry {
range: 44..79,
body: [
Pass(
StmtPass {
range: 53..57,
},
),
],
handlers: [
ExceptHandler(
ExceptHandlerExceptHandler {
range: 58..79,
type_: Some(
Tuple(
ExprTuple {
range: 65..69,
elts: [
Name(
ExprName {
range: 65..66,
id: Name("x"),
ctx: Load,
},
),
Name(
ExprName {
range: 68..69,
id: Name("y"),
ctx: Load,
},
),
],
ctx: Load,
parenthesized: false,
},
),
),
name: None,
body: [
Pass(
StmtPass {
range: 75..79,
},
),
],
},
),
],
orelse: [],
finalbody: [],
is_star: false,
},
),
Try(
StmtTry {
range: 80..116,
body: [
Pass(
StmtPass {
range: 89..93,
},
),
],
handlers: [
ExceptHandler(
ExceptHandlerExceptHandler {
range: 94..116,
type_: Some(
Tuple(
ExprTuple {
range: 102..106,
elts: [
Name(
ExprName {
range: 102..103,
id: Name("x"),
ctx: Load,
},
),
Name(
ExprName {
range: 105..106,
id: Name("y"),
ctx: Load,
},
),
],
ctx: Load,
parenthesized: false,
},
),
),
name: None,
body: [
Pass(
StmtPass {
range: 112..116,
},
),
],
},
),
],
orelse: [],
finalbody: [],
is_star: true,
},
),
],
},
)
```