Skip to content

Conversation

ntBre
Copy link
Contributor

@ntBre ntBre commented Oct 8, 2025

Summary

This PR implements the black preview style from psf/black#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

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

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.

@ntBre ntBre added formatter Related to the formatter preview Related to preview mode features labels Oct 8, 2025
Copy link
Contributor

github-actions bot commented Oct 8, 2025

ruff-ecosystem results

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@ntBre ntBre marked this pull request as ready for review October 8, 2025 15:39
@ntBre ntBre requested a review from MichaReiser as a code owner October 8, 2025 15:39
@ntBre ntBre mentioned this pull request Oct 8, 2025
31 tasks
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great.

I just realized that we haven't updated our black tests in a while. It's something I normally do before working on preview styles because we then "inherit" the black tests

Would you mind updating the black tests in a separate PR that we can merge first to get more test coverage?

Updating is normally very smooth but you might run into problems if Black changed their test structure. All that's needed is to run

https://github.com/astral-sh/ruff/blob/a9efdea113a4f676ef85d89ed2dc285f7087478d/crates/ruff_python_formatter/resources/test/fixtures/import_black_tests.py

I believe it takes a path to black's checkout as an argument

@MichaReiser MichaReiser added the great writeup A wonderful example of a quality contribution label Oct 8, 2025
ntBre added 9 commits October 14, 2025 10:15
Summary
--

This PR implements the black preview style from
psf/black#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'
```

<hr>

We do a similar transformation for `with` statements after Python 3.8, so I took
a lot of inspiration from that implementation.

Test Plan
--

New tests
@ntBre ntBre force-pushed the brent/fmt-remove-exception-parens branch from c0ab9cb to 04668e6 Compare October 14, 2025 14:46
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. The most satisfying feeling ever when working on the formatter :)

@ntBre ntBre merged commit 591e9bb into main Oct 14, 2025
37 checks passed
@ntBre ntBre deleted the brent/fmt-remove-exception-parens branch October 14, 2025 15:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

formatter Related to the formatter great writeup A wonderful example of a quality contribution preview Related to preview mode features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants