ruff/crates/ruff_python_formatter/src
Brent Westbrook 591e9bbccb
Remove parentheses around multiple exception types on Python 3.14+ (#20768)
Summary
--

This PR implements the black preview style from
https://github.com/psf/black/pull/4720. As of Python 3.14, you're
allowed to omit the parentheses around groups of exceptions, as long as
there's no `as` binding:

**3.13**

```pycon
Python 3.13.4 (main, Jun  4 2025, 17:37:06) [Clang 20.1.4 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> try: ...
... except (Exception, BaseException): ...
...
Ellipsis
>>> try: ...
... except Exception, BaseException: ...
...
  File "<python-input-1>", line 2
    except Exception, BaseException: ...
           ^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: multiple exception types must be parenthesized
```

**3.14**

```pycon
Python 3.14.0rc2 (main, Sep  2 2025, 14:20:56) [Clang 20.1.4 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> try: ...
... except Exception, BaseException: ...
...
Ellipsis
>>> try: ...
... except (Exception, BaseException): ...
...
Ellipsis
>>> try: ...
... except Exception, BaseException as e: ...
...
  File "<python-input-2>", line 2
    except Exception, BaseException as e: ...
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: multiple exception types must be parenthesized when using 'as'
```

I think this ended up being pretty straightforward, at least once Micha
showed me where to start :)

Test Plan
--

New tests

At first I thought we were deviating from black in how we handle
comments within the exception type tuple, but I think this applies to
how we format all tuples, not specifically with the new preview style.
2025-10-14 11:17:45 -04:00
..
comments [ruff] improve handling of intermixed comments inside from-imports (#20561) 2025-10-07 08:14:09 -07:00
expression Remove parentheses around multiple exception types on Python 3.14+ (#20768) 2025-10-14 11:17:45 -04:00
module [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
other Remove parentheses around multiple exception types on Python 3.14+ (#20768) 2025-10-14 11:17:45 -04:00
pattern Update pre-commit dependencies (#19162) 2025-07-07 04:07:44 +00:00
snapshots Update insta snapshots (#14366) 2024-11-15 19:31:15 +01:00
statement [formatter] Fix missing blank lines before decorated classes in .pyi files (#18888) 2025-06-24 16:25:44 +02:00
string Update rust toolchain to 1.90 (#20469) 2025-09-18 16:54:49 +02:00
type_param [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
builders.rs Implement template strings (#17851) 2025-05-30 15:00:56 -05:00
cli.rs Remove parentheses around multiple exception types on Python 3.14+ (#20768) 2025-10-14 11:17:45 -04:00
context.rs Update Rust toolchain to 1.89 (#19807) 2025-08-07 18:21:50 +02:00
db.rs Update Rust toolchain to 1.88 and MSRV to 1.86 (#19011) 2025-06-28 20:24:00 +02:00
generated.rs Implement template strings (#17851) 2025-05-30 15:00:56 -05:00
lib.rs Update Rust toolchain to 1.88 and MSRV to 1.86 (#19011) 2025-06-28 20:24:00 +02:00
main.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
options.rs Add support for using uv as an alternative formatter backend (#19665) 2025-09-09 20:39:53 +05:30
prelude.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00
preview.rs Remove parentheses around multiple exception types on Python 3.14+ (#20768) 2025-10-14 11:17:45 -04:00
range.rs [ty] AST garbage collection (#18482) 2025-06-13 08:40:11 -04:00
shared_traits.rs Upgrade Rust toolchain to 1.83 (#14677) 2024-11-29 12:05:05 +00:00
verbatim.rs Switch to Rust 2024 edition (#18129) 2025-05-16 13:25:28 +02:00