Commit Graph
441 Commits
Author SHA1 Message Date
Micha Reiser 37684d51ce Format Python using the arena and source-order visitor
Read children directly from the stored argument and parameter lists.
Use the syntax tree to locate comments and construct formatted output.
2026-11-20 16:16:30 +00:00
Charlie Marsh 08aac42edb Coalesce source slices when formatting attributes (#29841)
## Summary

Formatting and printing the top-level modules in CPython’s standard
library took 3.9% less time in a local benchmark (125.7 ms to 120.8 ms),
or 2.4% less with parsing included. CodSpeed reports a 5.1% efficiency
improvement on the [`large/dataset.py` formatter
benchmark](https://github.com/astral-sh/ruff/pull/29841#issuecomment-6515803489).

Today the formatter emits a source slice for `a`, a dot token, and a
source slice for `b` just to print `a.b`. We now emit the whole
expression as one slice when the dot touches both names, the first name
is unparenthesized, and there are no comments or call-chain breaks to
handle. Reading directly from the source also preserves Unicode
spellings like `𝓪.𝓫`.

For `factory().attr`, we still format the call and handle any line break
before the dot, but we can emit `.attr` as one slice. This follows the
approach in [Oxc #28462](https://github.com/oxc-project/oxc/pull/28462).
2026-10-30 21:16:18 +00:00
Brent Westbrook c8589d0eb1 Move diagnostic format dispatch into Ruff and ty (#29647)
Summary
--

This addresses the suggestion from [#29635]. `DisplayDiagnosticConfig`
previously determined the
`DiagnosticFormat` for diagnostics coming from both Ruff and ty, which
meant that it had to have a
shared enum between the two, despite each program supporting a different
subset of the available
formats. This PR eliminates the shared `DiagnosticFormat` and dispatch
through `DisplayDiagnostics`,
leaving Ruff and ty to construct `Renderer`s directly and do their own
dispatch on the output
formats they actually support. This will be especially helpful in
#29635, where we want to provide
slightly different arguments to each new `SarifRenderer`.

The `Renderer` trait itself isn't strictly necessary, but it made the
calls on the Ruff side a lot
neater since the trait provides a default `write` method based on the
required `render` method.
Codex also threw in a default `display` method that helps in a couple of
places too.

Test Plan
--

Existing tests

[#29635]:
https://github.com/astral-sh/ruff/pull/29635#discussion_r4352505588
2026-10-23 09:38:24 -04:00
Brent Westbrook ef23c9c39e Update default and latest Python versions for 3.15 (#28792)
Summary
--

This PR updates our default Python version to 3.11, in line with 3.10
reaching [EOL] next month, and
updates our latest Python version to 3.15. I followed last year's PR
(#20725) to find everywhere
that needed to change. Fortunately there were not as many test changes
needed as last year's
deferred annotation issues.

I also updated the language classifier in our pyproject.toml.

[EOL]: https://devguide.python.org/versions/#supported-versions

Test Plan
--

Existing tests with updated snapshots. Most of the updates were to
snapshots for TC001, TC002, and
TC003, which now default to the lazy-import fix.

After updating the snapshots, I tried to make sure we still had coverage
for whatever changed. That
meant adding a few more mdtests for the TC rules, and one small mdtest
for RUF017 to restore
coverage for the `functools.reduce` fix.
2026-10-09 12:26:57 -04:00
Brent Westbrook 3f5a91cecf Stabilize pragma comment width handling (#28795)
Summary
--

I wasn't really planning to do a full stabilization pass, but I quickly skimmed through `preview.rs`
and remembered that the isort pragma change was kind a bug fix but technically breaking and thought
we might want to stabilize it.

This PR stabilizes the isort pragma comment width handling introduced in #27313, along with the
related pragma handling for `E501` and `W505` from #24071. There was a comment about also
stabilizing the related formatter preview feature on the function from #24071, so that is stabilized
here too.

I'm fine holding off on this, especially if the formatter change is a better fit for the 2027 style
guide. Like I said above, I mostly wanted to stabilize the isort change, but that snowballed a bit
since the helpers were shared.

Test Plan
--

Updated existing snapshots
2026-10-09 12:26:57 -04:00
Charlie Marsh 7b13f01ba1 [ty] Reduce retained AST memory by shrinking expressions (#28335)
## Summary

We reduce `Expr` from 64 to 56 bytes on 64-bit targets. The largest
variants determine the enum's size, so shrinking those variants saves
eight bytes in every `Expr` value, including expressions in the parsed
modules retained by ty.

The changes are:

- Store positional call arguments in `ThinVec`, reducing `Arguments` and
`ExprCall` by eight bytes.
- Store all comparison operands in one boxed slice. This removes the
separate left-operand allocation and reduces `ExprCompare` by eight
bytes while keeping operators in a boxed slice.
- Store dictionary-comprehension generators in a boxed slice, reducing
`ExprDictComp` by eight bytes.
- Store f-string and t-string elements in boxed slices, and represent a
single f-string directly as `FString`. This keeps single parts inline
and reduces `ExprFString` and `ExprTString` by eight bytes.

The parser, AST visitors, formatter, lint rules, and type inference use
the updated layouts. Size assertions cover the 56-byte enum and its
affected variants.
2026-09-10 15:39:14 -04:00
Qatadaha Bin MatloobandMicha Reiser b3fb43af25 Alternate nested quotes inside format spec interpolations (#28259)
Co-authored-by: Micha Reiser <micha@reiser.io>
2026-09-07 17:53:16 +02:00
Charlie Marsh bdf1320bba Update Rust toolchain to 1.97 and MSRV to 1.95 (#26844) 2026-07-15 08:12:16 +02:00
Samuel RigaudandSamuel Rigaud 3f0ea98047 fix typos (#26462)
Another round of typos being fixed 

<img width="300" height="200" alt="image"
src="https://github.com/user-attachments/assets/acdaea05-0d28-4537-94db-9a809d76e00c"
/>

Co-authored-by: Samuel Rigaud <rigaud@gmail.com>
2026-07-01 23:23:21 +01:00
Samuel RigaudandSamuel Rigaud d78b3d5f32 Fix typos (#26443)
FIix even more typos

Co-authored-by: Samuel Rigaud <rigaud@gmail.com>
2026-06-28 23:23:37 +01:00
Davis VaughanandMicha Reiser 63cef3fdaa Preserve whitespace for Quarto cell option comments (#25641)
Co-authored-by: Micha Reiser <micha@reiser.io>
2026-06-05 07:54:07 +01:00
Brent Westbrook 9c6536e73c Fix invalid IR when flattening nested BestFitting elements (#25398)
Summary
--

This PR fixes #25397 by dropping `StartBestFittingEntry` and
`EndBestFittingEntry` tags in
`RemoveSoftLinesBuffer`. #25144 changed `RemoveSoftLinesBuffer` to
inline the most-flat variant of
`BestFitting` elements, but it inlined them with their start and end
tags, which caused the problem
observed in #25397 when these tags became nested in another
`BestFitting` element, which interpreted
the first `End` tag as ending the outer `BestFitting` rather than the
nested one. You can see this
clearly in the IR. First, the diff between the 0.15.13 and 0.15.14 IR,
which reveals the unmatched
start tags:

<details>
<summary>0.15.13 vs main</summary>

```diff
@@ -38,29 +38,13 @@
                           "lambda ",
                           group(["arg"]),
                           ": ",
-                          best_fitting([
-                            [
-                              [
-                                <interned 1> [
-                                  "str(",
-                                  group([
-                                    indent([soft_line_break, group(["arg"])]),
-                                    soft_line_break
-                                  ]),
-                                  ")"
-                                ]
-                              ]
-                            ]
-                            [[group(expand: true, [<ref interned *1>])]]
-                            [
-                              [
-                                "(",
-                                indent([hard_line_break, <ref interned *1>]),
-                                hard_line_break,
-                                ")"
-                              ]
+                          [
+                            <interned 1> [
+                              "str(",
+                              group([indent([group(["arg"])])]),
+                              ")"
                             ]
-                          ]),
+                          ],
                           ", self.args"
                         ])
                       ])
@@ -105,27 +89,9 @@
                             "lambda ",
                             group(["arg"]),
                             ": ",
-                            best_fitting([
-                              [
-                                [
-                                  <interned 2> [
-                                    "str(",
-                                    group([
-                                      indent([soft_line_break, group(["arg"])]),
-                                      soft_line_break
-                                    ]),
-                                    ")"
-                                  ]
-                                ]
-                              ]
-                              [[group(expand: true, [<ref interned *2>])]]
-                              [
-                                [
-                                  "(",
-                                  indent([hard_line_break, <ref interned *2>]),
-                                  hard_line_break,
-                                  ")"
-                                ]
+                            [
+                              <interned 2> [
+                                "str(",
+                                group([indent([group(["arg"])])]),
+                                ")"
                               ]
-                            ]),
-                            ", self.args"
+                            ]
                           ])
+                          <START_WITHOUT_END<Group>>
                         ])
-                      ]),
-                      ")"
+                        <START_WITHOUT_END<Indent>>
+                      ])
+                      <START_WITHOUT_END<Group>>
                     ])
+                    <START_WITHOUT_END<Group>>
                   ])
-                ]),
-                ")})\""
-              ]),
-              hard_line_break,
-              ")"
+                  <START_WITHOUT_END<Indent>>
+                ])
+                <START_WITHOUT_END<Group>>
+              ])
+              <START_WITHOUT_END<Indent>>
             ])
-          ]
+            <START_WITHOUT_END<Group>>
+          ])
+          <START_WITHOUT_END<BestFittingEntry>>
         ]
       ])
     ]),
```

</details>

and then the diff between main and this PR, which shows the removed
brackets that represent the
nested tags:

<details>
<summary>main vs PR</summary>

```diff
@@ -38,12 +38,10 @@
                           "lambda ",
                           group(["arg"]),
                           ": ",
-                          [
-                            <interned 1> [
-                              "str(",
-                              group([indent([group(["arg"])])]),
-                              ")"
-                            ]
+                          <interned 1> [
+                            "str(",
+                            group([indent([group(["arg"])])]),
+                            ")"
                           ],
                           ", self.args"
                         ])
@@ -89,24 +87,15 @@
                             "lambda ",
                             group(["arg"]),
                             ": ",
-                            [
-                              <interned 2> [
-                                "str(",
-                                group([indent([group(["arg"])])]),
-                                ")"
-                              ]
-                            ]
+                            <interned 2> [
+                              "str(",
+                              group([indent([group(["arg"])])]),
+                              ")"
+                            ],
+                            ", self.args"
                           ])
-                          <START_WITHOUT_END<Group>>
                         ])
-                        <START_WITHOUT_END<Indent>>
-                      ])
-                      <START_WITHOUT_END<Group>>
+                      ]),
+                      ")"
                     ])
-                    <START_WITHOUT_END<Group>>
                   ])
-                  <START_WITHOUT_END<Indent>>
-                ])
-                <START_WITHOUT_END<Group>>
-              ])
-              <START_WITHOUT_END<Indent>>
+                ]),
+                ")})\""
+              ]),
+              hard_line_break,
+              ")"
             ])
-            <START_WITHOUT_END<Group>>
-          ])
-          <START_WITHOUT_END<BestFittingEntry>>
+          ]
         ]
       ])
     ]),
```

</details>

I think the diff between 0.15.13 and this PR is also nice to see because
it makes it a bit more
clear that the `BestFitting` element has been inlined:

<details>
<summary>0.15.13 vs this PR</summary>

```diff
@@ -38,29 +38,11 @@
                           "lambda ",
                           group(["arg"]),
                           ": ",
-                          best_fitting([
-                            [
-                              [
-                                <interned 1> [
-                                  "str(",
-                                  group([
-                                    indent([soft_line_break, group(["arg"])]),
-                                    soft_line_break
-                                  ]),
-                                  ")"
-                                ]
-                              ]
-                            ]
-                            [[group(expand: true, [<ref interned *1>])]]
-                            [
-                              [
-                                "(",
-                                indent([hard_line_break, <ref interned *1>]),
-                                hard_line_break,
-                                ")"
-                              ]
-                            ]
-                          ]),
+                          <interned 1> [
+                            "str(",
+                            group([indent([group(["arg"])])]),
+                            ")"
+                          ],
                           ", self.args"
                         ])
                       ])
@@ -105,29 +87,11 @@
                             "lambda ",
                             group(["arg"]),
                             ": ",
-                            best_fitting([
-                              [
-                                [
-                                  <interned 2> [
-                                    "str(",
-                                    group([
-                                      indent([soft_line_break, group(["arg"])]),
-                                      soft_line_break
-                                    ]),
-                                    ")"
-                                  ]
-                                ]
-                              ]
-                              [[group(expand: true, [<ref interned *2>])]]
-                              [
-                                [
-                                  "(",
-                                  indent([hard_line_break, <ref interned *2>]),
-                                  hard_line_break,
-                                  ")"
-                                ]
-                              ]
-                            ]),
+                            <interned 2> [
+                              "str(",
+                              group([indent([group(["arg"])])]),
+                              ")"
+                            ],
                             ", self.args"
                           ])
                         ])
@@ -155,7 +119,6 @@

     def __str__(self) -> str:
         return (
-            f"first stringlist of args long={list(map(lambda arg: str(
-                        arg
-                    ), self.args))})"
+            "first string"
+            f"list of args long={list(map(lambda arg: str(arg), self.args))})"
         )
```

</details>

And you can also see at the bottom here that the actual formatting that
#25144 sought to resolve
is still fixed.

Test Plan
--

New regression test based on the code in the issue
2026-05-26 13:54:41 -07:00
Jelle Zijlstra 409c13f3ec Add full PEP 798 support (#25104)
Fixes #25098.

This adds full support for PEP 798 through the parser, linter,
formatter, and type checker.
There are a lot of changes but they are mostly straightforward. Let me
know though if it's better to split this up.
2026-05-16 16:32:57 +01:00
Brent Westbrook 25af028f5d Fix lambda formatting in interpolated string expressions (#25144)
Summary
--

This PR fixes #24807 by making `RemoveSoftLinesBuffer` aware of
`BestFitting` and always selecting the most-flat variant, as suggested
in [Micha's comment], assuming I interpreted it correctly.

As I noted on the issue, I was hoping I could get away with making a
local change to the lambda formatting, but this turned out to affect
the two other uses of `RemoveSoftLinesBuffer` too.

The formatting of the new regression tests:

```py
def foo():
    subprocess.check_call(f"rm -rf {' '.join(map(lambda object_name: os.path.join(dest_path, object_name), objects_to_remove))}", shell=True)

def bar():
    lambda x=" ".join(
        map(lambda object_name: os.path.join(dest_path, object_name), objects_to_remove)
    ): ...

value = f"prefix {
    ' '.join(map(lambda object_name: os.path.join(dest_path, object_name), objects_to_remove))
} suffix"
```

now (nearly) matches the output before #21385:

```console
❯ git diff <(uvx ruff@0.14.10 format - < fmt.py) <(./target/debug/ruff format - < fmt.py)
 def bar():
-    lambda x=" ".join(
-        map(lambda object_name: os.path.join(dest_path, object_name), objects_to_remove)
-    ): ...
+    lambda x=" ".join(map(lambda object_name: os.path.join(dest_path, object_name), objects_to_remove)): (
+        ...
+    )

 value = f"prefix {
```

with the only difference being that the body of this second lambda
still wraps in the new formatting.

Test Plan
--

New tests based on the reported issue and possibly the ecosystem check
on this PR

[Micha's comment]:
https://github.com/astral-sh/ruff/issues/24807#issuecomment-4314406097
2026-05-15 13:44:15 +00:00
Eyüp Can AkmanandMicha Reiser f32733c063 [flake8-pyi] Fix PYI016 false positive for f-string debug specifier (#24098)
Co-authored-by: Micha Reiser <micha@reiser.io>
2026-04-30 11:14:25 +02:00
Alex Waygood 581b3fa230 Update Rust toolchain to 1.95 and MSRV to 1.93 (#24677) 2026-04-17 11:39:14 +01:00
renovate[bot]andMicha Reiser 0b4603530e Update Rust crate similar to v3 (#24598)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Micha Reiser <micha@reiser.io>
2026-04-13 08:01:53 +00:00
Micha Reiser ad30af4cd0 Force update insta snapshots (#24495) 2026-04-08 17:33:19 +02:00
Charlie Marsh 1fe1c5f32c Avoid emitting multi-line f-string elements before Python 3.12 (#24377)
## Summary

See:
https://github.com/astral-sh/ruff/pull/24355#discussion_r3026446640.
Prior to Python 3.12, we need to avoid emitting formatted expressions
that span multiple lines in non-triple quoted f-strings.
2026-04-05 15:14:39 +00:00
Charlie Marsh 9a55bc6568 Reject multi-line f-string elements before Python 3.12 (#24355)
## Summary

Before Python 3.12, a replacement field in an f-string can span multiple
lines only if the outer f-string is triple-quoted. This was relaxed in
Python 3.12, but we weren't rejecting these as syntactically invalid on
earlier versions.

Closes https://github.com/astral-sh/ruff/issues/24348.
2026-04-05 15:09:26 +00:00
Charlie Marsh 2fb7c8ddd8 Sort formatter diagnostics in snapshots (#24375)
## Summary

Right now these tests are dependent on input order, so changes in the
underlying hash can lead to churn in the fixtures. See, e.g.:
https://github.com/astral-sh/ruff/pull/24355#discussion_r3026451902.
2026-04-03 09:59:41 -04:00
Matthew Lloyd 6468a35e9b Add nested-string-quote-style formatting option (#24312) 2026-03-31 18:48:24 +02:00
Daniil SivakandMicha Reiser 9d2b16029a E501/W505/formatter: Exclude nested pragma comments from line width calculation (#24071)
Closes #18470

## Summary

Exclude nested pragma comments like the `# noqa` in `test # comment 1 # noqa` from `E501`, `W505` and the formatter's line width calculation. 


## Test Plan

- Added linter test fixture `E501_5.py` with preview test cases for
nested pragmas (`# comment #noqa`, `## noqa`, `# comment # type:
ignore`)
- Added formatter test fixture `trailing_pragma_nested.py` demonstrating
stable vs preview reserved width behavior
- Doctests for `find_trailing_pragma_offset` in `pragmas.rs`

---------

Co-authored-by: Micha Reiser <micha@reiser.io>
2026-03-24 10:08:00 +00:00
Brent Westbrook 37cdd61406 Fix lambda body formatting for multiline calls and subscripts (#23866)
## Summary

This PR fixes https://github.com/astral-sh/ruff/issues/23851 by
expanding the special case for fluent call chains that previously used
`parenthesize_if_expands` to include any calls or subscripts with
`OptionalParentheses::Multiline`. The minimal change is in the third
commit (just swapping out the fluent chain check for
`matches!(needs_parentheses, OptionalParentheses::Multiline)`), but then
I reordered the conditional branches in a way that seemed to make more
sense.

## Test Plan

New tests based on the issue
2026-03-12 13:37:10 +00:00
Charlie Marsh 02f81b1ad7 Preserve type-expression parentheses in the formatter (#23867)
## Summary

This is a follow-up to #23865 generated by prompted Codex as follows:

> Can you find another bug like
https://github.com/astral-sh/ruff/issues/23864 in the formatter for us
to fix?

Here are some examples that previously had their parentheses stripped
(producing syntax errors) but now format correctly:

```python
# Annotated assignments with named expressions
x: (value := int) = 1     # was formatted as: x: value := int = 1

# Annotated assignments with yield
def f():
    y: (yield 1) = 1       # was formatted as: y: yield 1 = 1
    z: (yield 1)            # was formatted as: z: yield 1

# Annotated assignments with await
async def g():
    a: (await g()) = 1      # was formatted as: a: await g() = 1
    b: (await g())           # was formatted as: b: await g()

# Return annotations with await
async def h() -> (await g()):  # was formatted as: async def h() -> await g():
    pass

# Type aliases
type X = (value := int)    # was formatted as: type X = value := int
```

In each case, the parentheses are required for the expression to be
syntactically valid, but the formatter was previously stripping them.
2026-03-11 19:08:54 +00:00
Charlie Marsh 4f836b46fc Preserve required annotation parentheses in annotated assignments (#23865) 2026-03-10 14:34:22 +01:00
Charlie Marsh 8bc437ef90 Add support for star-unpacking of comprehensions (PEP 798) (#23788)
## Summary

In Python 3.15, you can star-unpack comprehensions (e.g., `[*x for x in
y]`) via [PEP
798](https://docs.python.org/3.15/whatsnew/3.15.html#whatsnew315-unpacking-in-comprehensions).
This PR adds support to the parser.
2026-03-09 10:23:52 -04:00
Charlie Marsh 8a10b6743c Add support for lazy import parsing (#23755)
## Summary

This PR adds `is_lazy` to the AST and parser, and supports `lazy` import
formatting in the formatter. The name `is_lazy` matches the [CPython
AST](https://github.com/python/cpython/blob/c3fb0d9d96902774c08b199dda0479a8d31398a5/Parser/Python.asdl#L48),
though we could modify it if there's demand.

There are no further changes to Ruff rules or even to Ruff import
sorting, since those deserve separate design discussions.

I've also omitted support for flagging semantic syntax errors (e.g.,
`lazy import` inside `def`).

See: https://github.com/astral-sh/ruff/issues/21305.
2026-03-06 09:12:21 -05:00
VasiliosandCharlie Marsh 8cfeae8955 JUnit output for ty lints (#22125)
## Summary

Adding JUnit XML output for `ty`

## Test Plan

Reusing existing tests, still working this out, will amend this
description later when I figure out how to specifically test `ty` lints,
it seems they already are but will verify

---------

Co-authored-by: Charlie Marsh <charlie.r.marsh@gmail.com>
2026-02-11 15:22:32 -05:00
Brent Westbrook f14fd5d885 Fix exception handler parenthesis removal for Python 3.14+ (#23126)
Summary
--

Fixes #23125. We were missing handling for starred elements within a
tuple of
exceptions. This produces valid syntax but changes the AST from a
non-starred `except` handler to a starred `except` handler (which can be
invalid
if there are other non-starred handlers):

```diff
 try: ...
-except (*exceptions, ValueError): ...
+except *exceptions, ValueError: ...
```

Starred expressions in other positions are outright invalid:

```pycon
>>> try: ...
... except Exception, *exceptions: ...
...
  File "<python-input-8>", line 2
    except Exception, *exceptions: ...
                      ^
SyntaxError: invalid syntax
```

We now preserve parentheses in these cases as well.

Test Plan
--

New tests derived from the issue
2026-02-06 16:48:22 -05:00
DylanandBrent Westbrook b53460799b 2026 Ruff Formatter Style (#22735)
Styles stabilized:

-
[`avoid_parens_for_long_as_captures`](https://github.com/astral-sh/ruff/pull/22743)
-
[`remove_parens_around_except_types`](https://github.com/astral-sh/ruff/pull/22741)
-
[`allow_newline_after_block_open`](https://github.com/astral-sh/ruff/pull/22742)
- [`no_chaperone_for_escaped_quote_in_triple_quoted_docstring
`](https://github.com/astral-sh/ruff/pull/22739)
- [`blank_line_before_decorated_class_in_stub
`](https://github.com/astral-sh/ruff/pull/22740)
-
[`parenthesize_lambda_bodies`](https://github.com/astral-sh/ruff/pull/22744)

To-do:

- [x] Change target branch to 0.15 release branch
- [x] Update documentation
- [x] Remove empty commit

---------

Co-authored-by: Brent Westbrook <brentrwestbrook@gmail.com>
2026-02-03 10:01:37 -05:00
Brent Westbrook 5c441523e3 Preserve required parentheses in lambda bodies (#22747)
Summary
--

This PR fixes the issues revealed in #22744 by adding an additional
branch to
the lambda body formatting that checks if the body `needs_parentheses`
before
falling back on the `Parentheses::Never` case. I also updated the
`ExprNamed::needs_parentheses` implementation to match the one from
#8465.

Test Plan
--

New test based on the failing cases in #22744. I also checked out #22744
and
checked that the tests pass after applying the changes from this PR.
2026-01-20 10:28:29 -05:00
Dylan 880513a013 Respect fmt: skip for multiple statements on same logical line (#22119)
This PR adjusts the logic for skipping formatting so that a `fmt: skip`
can affect multiple statements if they lie on the same line.

Specifically, a `fmt: skip` comment will now suppress all the statements
in the suite in which it appears whose range intersects the line
containing the skip directive. For example:

```python
x=[
'1'
];x=2 # fmt: skip
```

remains unchanged after formatting.

(Note that compound statements are somewhat special and were handled in
a previous PR - see #20633).


Closes #17331 and #11430.

Simplest to review commit by commit - the key diffs of interest are the
commit introducing the core logic, and the diff between the snapshots
introduced in the last commit (compared to the second commit).

# Implementation

On `main` we format a suite of statements by iterating through them. If
we meet a statement with a leading or trailing (own-line)`fmt: off`
comment, then we suppress formatting until we meet a `fmt: on` comment.
Otherwise we format the statement using its own formatting rule.

How are `fmt: skip` comments handled then? They are handled internally
to the formatting of each statement. Specifically, calling `.fmt` on a
statement node will first check to see if there is a trailing,
end-of-line `fmt: skip` (or `fmt: off`/`yapf: off`), and if so then
write the node with suppressed formatting.

In this PR we move the responsibility for handling `fmt: skip` into the
formatting logic of the suite itself. This is done as follows:

- Before beginning to format the suite, we do a pass through the
statements and collect the data of ranges with skipped formatting. More
specifically, we create a map with key given by the _first_ skipped
statement in a block and value a pair consisting of the _last_ skipped
statement and the _range_ to write verbatim.
- We iterate as before, but if we meet a statement that is a key in the
map constructed above, we pause to write the associated range verbatim.
We then advance the iterator to the last statement in the block and
proceed as before.

## Addendum on range formatting

We also had to make some changes to range formatting in order to support
this new behavior. For example, we want to make sure that

```python
<RANGE_START>x=1<RANGE_END>;x=2 # fmt: skip
```

formats verbatim, rather than becoming 

```python
x = 1;x=2 # fmt: skip
```

Recall that range formatting proceeds in two steps:
1. Find the smallest enclosing node containing the range AND that has
enough info to format the range (so it may be larger than you think,
e.g. a docstring has enclosing node given by the suite, not the string
itself.)
2. Carve out the formatted range from the result of formatting that
enclosing node.

We had to modify (1), since the suite knows how to format skipped nodes,
but nodes may not "know" they are skipped. To do this we altered the
`visit_body` bethod of the `FindEnclosingNode` visitor: now we iterate
through the statements and check for skipped ranges intersecting the
format range. If we find them, we return without descending. The result
is to consider the statement containing the suite as the enclosing node
in this case.
2026-01-10 14:56:58 +00:00
Dylan 924b2972f2 Update Black tests (#22405)
I am updating these because we didn't have test coverage for the
different handling of `fmt: skip` comments applied to multiple
statements on the same line. This is in preparation for #22119 (to show
before/after deviations).

Follows the same procedure as in #20794

Edit: As it happens, the new fixtures do not even cover the case
relevant to #22119 - they just deal with the already handled case of a
one-line compound statement. Nevertheless, it seems worthwhile to make
this update, especially since it uncovered a (possible?) bug.
2026-01-06 09:09:05 -06:00
Jack O'Connor 4712503c6d [ty] cargo insta test --force-update-snapshots (#22313)
Snapshot tests recently started reporting this warning:

> Snapshot test passes but the existing value is in a legacy format.
> Please run cargo insta test --force-update-snapshots to update to a
> newer format.

This PR is the result of that forced update.

One file (crates/ruff_db/src/diagnostic/render/full.rs) seems to get
corrupted, because it contains strings with unprintable characters that
trigger some bug in cargo-insta. I've manually reverted that file, and
also manually reverted the `input_file:` lines, which we like.
2026-01-05 07:55:47 -08:00
DylanandBrent Westbrook 4e1cf5747a Fluent formatting of method chains (#21369)
This PR implements a modification (in preview) to fluent formatting for
method chains: We break _at_ the first call instead of _after_.

For example, we have the following diff between `main` and this PR (with
`line-length=8` so I don't have to stretch out the text):

```diff
 x = (
-    df.merge()
+    df
+    .merge()
     .groupby()
     .agg()
     .filter()
 )
```

## Explanation of current implementation

Recall that we traverse the AST to apply formatting. A method chain,
while read left-to-right, is stored in the AST "in reverse". So if we
start with something like

```python
a.b.c.d().e.f()
```

then the first syntax node we meet is essentially `.f()`. So we have to
peek ahead. And we actually _already_ do this in our current fluent
formatting logic: we peek ahead to count how many calls we have in the
chain to see whether we should be using fluent formatting or now.

In this implementation, we actually _record_ this number inside the enum
for `CallChainLayout`. That is, we make the variant `Fluent` hold an
`AttributeState`. This state can either be:

- The number of call-like attributes preceding the current attribute
- The state `FirstCallOrSubscript` which means we are at the first
call-like attribute in the chain (reading from left to right)
- The state `BeforeFirstCallOrSubscript` which means we are in the
"first group" of attributes, preceding that first call.

In our example, here's what it looks like at each attribute:

```
a.b.c.d().e.f @ Fluent(CallsOrSubscriptsPreceding(1))
a.b.c.d().e @ Fluent(CallsOrSubscriptsPreceding(1))
a.b.c.d @ Fluent(FirstCallOrSubscript)
a.b.c @ Fluent(BeforeFirstCallOrSubscript)
a.b @ Fluent(BeforeFirstCallOrSubscript)
```

Now, as we descend down from the parent expression, we pass along this
little piece of state and modify it as we go to track where we are. This
state doesn't do anything except when we are in `FirstCallOrSubscript`,
in which case we add a soft line break.

Closes #8598

---------

Co-authored-by: Brent Westbrook <36778786+ntBre@users.noreply.github.com>
2025-12-15 09:29:50 -06:00
Micha Reiser e2ec2bc306 Use datatest for formatter tests (#21933) 2025-12-13 08:02:22 +00:00
Brent WestbrookandMicha Reiser 0ebdebddd8 Keep lambda parameters on one line and parenthesize the body if it expands (#21385)
## Summary

This PR makes two changes to our formatting of `lambda` expressions:
1. We now parenthesize the body expression if it expands
2. We now try to keep the parameters on a single line

The latter of these fixes #8179:

Black formatting and this PR's formatting:

```py
def a():
    return b(
        c,
        d,
        e,
        f=lambda self, *args, **kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
            *args, **kwargs
        ),
    )
```

Stable Ruff formatting

```py
def a():
    return b(
        c,
        d,
        e,
        f=lambda self,
        *args,
        **kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs),
    )
```

We don't parenthesize the body expression here because the call to
`aaaa...` has its own parentheses, but adding a binary operator shows
the new parenthesization:

```diff
@@ -3,7 +3,7 @@
         c,
         d,
         e,
-        f=lambda self, *args, **kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
-            *args, **kwargs
-        ) + 1,
+        f=lambda self, *args, **kwargs: (
+            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs) + 1
+        ),
     )
```

This is actually a new divergence from Black, which formats this input
like this:

```py
def a():
    return b(
        c,
        d,
        e,
        f=lambda self, *args, **kwargs: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(
            *args, **kwargs
        )
        + 1,
    )
```

But I think this is an improvement, unlike the case from #8179.

One other, smaller benefit is that because we now add parentheses to
lambda bodies, we also remove redundant parentheses:

```diff
 @pytest.mark.parametrize(
     "f",
     [
-        lambda x: (x.expanding(min_periods=5).cov(x, pairwise=True)),
-        lambda x: (x.expanding(min_periods=5).corr(x, pairwise=True)),
+        lambda x: x.expanding(min_periods=5).cov(x, pairwise=True),
+        lambda x: x.expanding(min_periods=5).corr(x, pairwise=True),
     ],
 )
 def test_moment_functions_zero_length_pairwise(f):
```

## Test Plan

New tests taken from #8465 and probably a few more I should grab from
the ecosystem results.

---------

Co-authored-by: Micha Reiser <micha@reiser.io>
2025-12-12 12:02:25 -05:00
Brent Westbrook f3714fd3c1 Fix leading comment formatting for lambdas with multiple parameters (#21879)
## Summary

This is a follow-up to #21868. As soon as I started merging #21868 into
#21385, I realized that I had missed a test case with `**kwargs` after
the `*args` parameter. Such a case is supposed to be formatted on one
line like:

```py
# input
(
    lambda
    # comment
    *x,
    **y: x
)

# output
(
    lambda
    # comment
    *x, **y: x
)
```

which you can still see on the
[playground](https://play.ruff.rs/bd88d339-1358-40d2-819f-865bfcb23aef?secondary=Format),
but on `main` after #21868, this was formatted as:

```py
(
    lambda
    # comment
    *x,
    **y: x
)
```

because the leading comment on the first parameter caused the whole
group around the parameters to break.

Instead of making these comments leading comments on the first
parameter, this PR makes them leading comments on the parameters list as
a whole.

## Test Plan

New tests, and I will also try merging this into #21385 _before_ opening
it for review this time.

<hr>

(labeling `internal` since #21868 should not be released before some
kind of fix)
2025-12-09 18:15:12 -05:00
Brent Westbrook 0bec5c0362 Fix comment placement in lambda parameters (#21868)
Summary
--

This PR makes two changes to comment placement in lambda parameters.
First, we
now insert a line break if the first parameter has a leading comment:

```py
# input
(
    lambda
    * # comment 2
    x:
    x
)

# main
(
    lambda # comment 2
    *x: x
)

# this PR
(
    lambda
	# comment 2
    *x: x
)
```

Note the missing space in the output from main. This case is currently
unstable
on main. Also note that the new formatting is more consistent with our
stable
formatting in cases where the lambda has its own dangling comment:

```py
# input
(
    lambda # comment 1
    * # comment 2
    x:
    x
)

# output
(
    lambda  # comment 1
    # comment 2
    *x: x
)
```

and when a parameter without a comment precedes the split `*x`:

```py
# input
(
    lambda y,
    * # comment 2
    x:
    x
)

# output
(
    lambda y,
    # comment 2
    *x: x
)
```

This does change the stable formatting, but I think such cases are rare
(expecting zero hits in the ecosystem report), this fixes an existing
instability, and it should not change any code we've previously
formatted.

Second, this PR modifies the comment placement such that `# comment 2`
in these
outputs is still a leading comment on the parameter. This is also not
the case
on main, where it becomes a [dangling lambda
comment](https://play.ruff.rs/3b29bb7e-70e4-4365-88e0-e60fe1857a35?secondary=Comments).
This doesn't cause any
instability that I'm aware of on main, but it does cause problems when
trying to
adjust the placement of dangling lambda comments in #21385. Changing the
placement in this way should not affect any formatting here.

Test Plan
--

New lambda tests, plus existing tests covering the cases above with
multiple
comments around the parameters (see lambda.py 122-143, and 122-205 or so
more
broadly)

I also checked manually that the comments are now leading on the
parameter:

```shell
❯ cargo run --bin ruff_python_formatter -- --emit stdout --target-version 3.10 --print-comments <<EOF
(
    lambda
        # comment 2
    *x: x
)
EOF
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.15s
     Running `target/debug/ruff_python_formatter --emit stdout --target-version 3.10 --print-comments`
# Comment decoration: Range, Preceding, Following, Enclosing, Comment
21..32, None, Some((Parameters, 37..39)), (ExprLambda, 6..42), "# comment 2"
{
    Node {
        kind: Parameter,
        range: 37..39,
        source: `*x`,
    }: {
        "leading": [
            SourceComment {
                text: "# comment 2",
                position: OwnLine,
                formatted: true,
            },
        ],
        "dangling": [],
        "trailing": [],
    },
}
(
    lambda
    # comment 2
    *x: x
)
```

But I didn't see a great place to put a test like this. Is there
somewhere I can assert this comment placement since it doesn't affect
any formatting yet? Or is it okay to wait until we use this in #21385?
2025-12-09 14:07:48 -05:00
Dylan 62343a101a Respect fmt: skip for compound statements on single line (#20633)
Closes #11216

Essentially the approach is to implement `Format` for a new struct
`FormatClause` which is just a clause header _and_ its body. We then
have the information we need to see whether there is a skip suppression
comment on the last child in the body and it all fits on one line.
2025-11-18 12:02:09 -06:00
cbc6863b8c Fix panic when formatting comments in unary expressions (#21501)
## Summary

This is another attempt at https://github.com/astral-sh/ruff/pull/21410
that fixes https://github.com/astral-sh/ruff/issues/19226.

@MichaReiser helped me get something working in a very helpful pairing
session. I pushed one additional commit moving the comments back from
leading comments to trailing comments, which I think retains more of the
input formatting.

I was inspired by Dylan's PR (#21185) to make one of these tables:

<table>
                <thead>
                    <tr>
                    <th scope="col">Input</th>
                    <th scope="col">Main</th>
                    <th scope="col">PR</th>
                    </tr>
                </thead>
                <tbody>
<tr>
<td><pre lang="python">
if (
    not
    # comment
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
):
    pass
</pre></td>
<td><pre lang="python">
if (
    # comment
    not aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
):
    pass

</pre></td>
<td><pre lang="python">
if (
    not
    # comment
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
):
    pass

</pre></td>
</tr>
<tr>
<td><pre lang="python">
if (
    # unary comment
    not
    # operand comment
    (
        # comment
        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
    )
):
    pass
</pre></td>
<td><pre lang="python">
if (
    # unary comment
    # operand comment
    not (
        # comment
        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
    )
):
    pass

</pre></td>
<td><pre lang="python">
if (
    # unary comment
    not
    # operand comment
    (
        # comment
        aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
        + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
    )
):
    pass

</pre></td>
</tr>
<tr>
<td><pre lang="python">
if (
    not # comment
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
):
    pass
</pre></td>
<td><pre lang="python">
if (  # comment
    not aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
):
    pass

</pre></td>
<td><pre lang="python">
if (
    not aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa  # comment
    + bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
):
    pass

</pre></td>
</tr>
</tbody>
            </table>

hopefully it helps even though the snippets are much wider here.

The two main differences are (1) that we now retain own-line comments
between the unary operator and its operand instead of moving these to
leading comments on the operator itself, and (2) that we move
end-of-line comments between the operator and operand to dangling
end-of-line comments on the operand (the last example in the table).

## Test Plan

Existing tests, plus new ones based on the issue. As I noted below, I
also ran the output from main on the unary.py file back through this
branch to check that we don't reformat code from main. This made me feel
a bit better about not preview-gating the changes in this PR.

```shell
> git show main:crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/unary.py | ruff format - | ./target/debug/ruff format --diff -
> echo $?
0
```

---------

Co-authored-by: Micha Reiser <micha@reiser.io>
Co-authored-by: Takayuki Maeda <takoyaki0316@gmail.com>
2025-11-18 10:48:14 -05:00
Dylan 8156b45173 Avoid syntax error when formatting attribute expressions with outer parentheses, parenthesized value, and trailing comment on value (#20418)
Closes #19350 

This fixes a syntax error caused by formatting. However, the new tests reveal that there are some cases where formatting attributes with certain comments behaves strangely, both before and after this PR, so some more polish may be in order.

For example, without parentheses around the value, and both before and after this PR, we have:

```python
# unformatted
variable = (
    something # a comment
    .first_method("some string")
)

# formatted
variable = something.first_method("some string")  # a comment
```

which is probably not where the comment ought to go.
2025-11-17 09:11:36 -06:00
Dylan 04a3ec3689 Adjust own-line comment placement between branches (#21185)
This PR attempts to improve the placement of own-line comments between
branches in the setting where the comment is more indented than the
preceding node.

There are two main changes.

### First change: Preceding node has leading content

If the preceding node has leading content, we now regard the comment as
automatically _less_ indented than the preceding node, and format
accordingly.

For example, 

```python
if True: preceding_node
# leading on `else`, not trailing on `preceding_node`
else: ...
```

This is more compatible with `black`, although there is a (presumably
very uncommon) edge case:

```python
if True:
    this;that
    # leading on `else`, but trailing in `black`
else: ...
```

I'm sort of okay with this - presumably if one wanted a comment for
those semi-colon separated statements, one should have put it _above_
them, and one wanted a comment only for `that` then it ought to have
been on the same line?

### Second change: searching for last child in body

While searching for the (recursively) last child in the body of the
preceding _branch_, we implicitly assumed that the preceding node had to
have a body to begin the recursion. But actually, in the base case, the
preceding node _is_ the last child in the body of the preceding branch.
So, for example:

```python
if True:
    something
    last_child_but_no_body
    # leading on else for `main` but trailing in this PR
else: ...
```

### More examples

The table below is an attempt to summarize the changes in behavior. The
rows alternate between an example snippet with `while` and the same
example with `if` - in the former case we do _not_ have an `else` node
and in the latter we do.

Notice that:

1. On `main` our handling of `if` vs. `while` is not consistent, whereas
it is consistent in the present PR
2. We disagree with `black` in all cases except that last example on
`main`, but agree in all cases for the present PR (though see above for
a wonky edge case where we disagree).

<table>
<tr>
<th>Original
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>

<th><code>main</code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>
<th>This
PR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>

<th><code>black</code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</th>
</tr>
<tr>
<td>

<pre lang="python">
while True: 
    pass
        # comment
else:
    pass
</pre>

</td>
<td>

<pre lang="python">
while True:
    pass
else:
    # comment
    pass
</pre>

</td>
<td>

<pre lang="python">
while True:
    pass
    # comment
else:
    pass
</pre>

</td>
<td>

<pre lang="python">
while True:
    pass
    # comment
else:
    pass
</pre>

</td>
</tr>
<tr>
<td>

<pre lang="python">
if True:
    pass
        # comment
else:
    pass
</pre>

</td>
<td>

<pre lang="python">
if True:
    pass
# comment
else:
    pass
</pre>

</td>
<td>

<pre lang="python">
if True:
    pass
    # comment
else:
    pass
</pre>

</td>
<td>

<pre lang="python">
if True:
    pass
    # comment
else:
    pass
</pre>

</td>
</tr>
<tr>
<td>

<pre lang="python">
while True: pass
# comment
else:
    pass
</pre>

</td>
<td>

<pre lang="python">
while True:
    pass
    # comment
else:
    pass
</pre>

</td>
<td>

<pre lang="python">
while True:
    pass
# comment
else:
    pass
</pre>

</td>
<td>

<pre lang="python">
while True:
    pass
# comment
else:
    pass
</pre>

</td>
</tr>
<tr>
<td>

<pre lang="python">
if True: pass
# comment
else:
    pass
</pre>

</td>
<td>

<pre lang="python">
if True:
    pass
    # comment
else:
    pass
</pre>

</td>
<td>

<pre lang="python">
if True:
    pass
# comment
else:
    pass
</pre>

</td>
<td>

<pre lang="python">
if True:
    pass
# comment
else:
    pass
</pre>

</td>
</tr>
<tr>
<td>

<pre lang="python">
while True: pass
    # comment
else:
    pass
</pre>

</td>
<td>

<pre lang="python">
while True:
    pass
else:
    # comment
    pass
</pre>

</td>
<td>

<pre lang="python">
while True:
    pass
# comment
else:
    pass
</pre>

</td>
<td>

<pre lang="python">
while True:
    pass
# comment
else:
    pass
</pre>

</td>
</tr>
<tr>
<td>

<pre lang="python">
if True: pass
    # comment
else:
    pass
</pre>

</td>
<td>

<pre lang="python">
if True:
    pass
# comment
else:
    pass
</pre>

</td>
<td>

<pre lang="python">
if True:
    pass
# comment
else:
    pass
</pre>

</td>
<td>

<pre lang="python">
if True:
    pass
# comment
else:
    pass
</pre>

</td>
</tr>
</table>
2025-11-17 07:30:34 -06:00
Brent Westbrook 63b1c1ea8b Avoid extra parentheses for long match patterns with as captures (#21176)
Summary
--

This PR fixes #17796 by taking the approach mentioned in
https://github.com/astral-sh/ruff/issues/17796#issuecomment-2847943862
of simply recursing into the `MatchAs` patterns when checking if we need
parentheses. This allows us to reuse the parentheses in the inner
pattern before also breaking the `MatchAs` pattern itself:

```diff
 match class_pattern:
     case Class(xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx) as capture:
         pass
-    case (
-        Class(xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx) as capture
-    ):
+    case Class(
+        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+    ) as capture:
         pass
-    case (
-        Class(
-            xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-        ) as capture
-    ):
+    case Class(
+        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+    ) as capture:
         pass
     case (
         Class(
@@ -685,13 +683,11 @@
 match sequence_pattern_brackets:
     case [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] as capture:
         pass
-    case (
-        [xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx] as capture
-    ):
+    case [
+        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+    ] as capture:
         pass
-    case (
-        [
-            xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-        ] as capture
-    ):
+    case [
+        xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
+    ] as capture:
         pass
```

I haven't really resolved the question of whether or not it's okay
always to recurse, but I'm hoping the ecosystem check on this PR might
shed some light on that.

Test Plan
--

New tests based on the issue and then reviewing the ecosystem check here
2025-11-03 17:06:52 -05:00
Brent Westbrook 827d8ae5d4 Allow newlines after function headers without docstrings (#21110)
Summary
--

This is a first step toward fixing #9745. After reviewing our open
issues and several Black issues and PRs, I personally found the function
case the most compelling, especially with very long argument lists:

```py
def func(
	self,
	arg1: int,
	arg2: bool,
	arg3: bool,
	arg4: float,
	arg5: bool,
) -> tuple[...]:
	if arg2 and arg3:
		raise ValueError
```

or many annotations:

```py
def function(
    self, data: torch.Tensor | tuple[torch.Tensor, ...], other_argument: int
) -> torch.Tensor | tuple[torch.Tensor, ...]:
    do_something(data)
    return something
```

I think docstrings help the situation substantially both because syntax
highlighting will usually give a very clear separation between the
annotations and the docstring and because we already allow a blank line
_after_ the docstring:

```py
def function(
    self, data: torch.Tensor | tuple[torch.Tensor, ...], other_argument: int
) -> torch.Tensor | tuple[torch.Tensor, ...]:
    """
	A function doing something.

	And a longer description of the things it does.
	"""

    do_something(data)
    return something
```

There are still other comments on #9745, such as [this one] with 9
upvotes, where users specifically request blank lines in all block
types, or at least including conditionals and loops. I'm sympathetic to
that case as well, even if personally I don't find an [example] like
this:

```py
if blah:

    # Do some stuff that is logically related
    data = get_data()

    # Do some different stuff that is logically related
    results = calculate_results()

    return results
```

to be much more readable than:

```py
if blah:
    # Do some stuff that is logically related
    data = get_data()

    # Do some different stuff that is logically related
    results = calculate_results()

    return results
```

I'm probably just used to the latter from the formatters I've used, but
I do prefer it. I also think that functions are the least susceptible to
the accidental introduction of a newline after refactoring described in
Micha's [comment] on #8893.

I actually considered further restricting this change to functions with
multiline headers. I don't think very short functions like:

```py
def foo():

    return 1
```

benefit nearly as much from the allowed newline, but I just went with
any function without a docstring for now. I guess a marginal case like:

```py
def foo(a_long_parameter: ALongType, b_long_parameter: BLongType) -> CLongType:

    return 1
```

might be a good argument for not restricting it.

I caused a couple of syntax errors before adding special handling for
the ellipsis-only case, so I suspect that there are some other
interesting edge cases that may need to be handled better.

Test Plan
--

Existing tests, plus a few simple new ones. As noted above, I suspect
that we may need a few more for edge cases I haven't considered.

[this one]:
https://github.com/astral-sh/ruff/issues/9745#issuecomment-2876771400
[example]:
https://github.com/psf/black/issues/902#issuecomment-1562154809
[comment]:
https://github.com/astral-sh/ruff/issues/8893#issuecomment-1867259744
2025-10-31 14:53:40 -04:00
Dylan 116611bd39 Fix finding keyword range for clause header after statement ending with semicolon (#21067)
When formatting clause headers for clauses that are not their own node,
like an `else` clause or `finally` clause, we begin searching for the
keyword at the end of the previous statement. However, if the previous
statement ended in a semicolon this caused a panic because we only
expected trivia between the end of the last statement and the keyword.

This PR adjusts the starting point of our search for the keyword to
begin after the optional semicolon in these cases.

Closes #21065
2025-10-27 09:52:17 -05:00
Micha Reiser 3c7f56f582 Restore indent.py (#21094) 2025-10-27 10:34:29 +00:00
Brent Westbrook 4b0fa5f270 Render a diagnostic for syntax errors introduced in formatter tests (#21021)
## Summary

I spun this out from #21005 because I thought it might be helpful
separately. It just renders a nice `Diagnostic` for syntax errors
pointing to the source of the error. This seemed a bit more helpful to
me than just the byte offset when working on #21005, and we had most of
the code around after #20443 anyway.

## Test Plan

This doesn't actually affect any passing tests, but here's an example of
the additional output I got when I broke the spacing after the `in`
token:

```
    error[internal-error]: Expected 'in', found name
      --> /home/brent/astral/ruff/crates/ruff_python_formatter/resources/test/fixtures/black/cases/cantfit.py:50:79
       |
    48 |     need_more_to_make_the_line_long_enough,
    49 | )
    50 | del ([], name_1, name_2), [(), [], name_4, name_3], name_1[[name_2 for name_1 inname_0]]
       |                                                                               ^^^^^^^^
    51 | del ()
       |
```

I just appended this to the other existing output for now.
2025-10-21 13:47:26 -04:00
Brent Westbrook 0115fd3757 Avoid reusing nested, interpolated quotes before Python 3.12 (#20930)
## Summary

Fixes #20774 by tracking whether an `InterpolatedStringState` element is
nested inside of another interpolated element. This feels like kind of a
naive fix, so I'm welcome to other ideas. But it resolves the problem in
the issue and clears up the syntax error in the black compatibility
test, without affecting many other cases.

The other affected case is actually interesting too because the
[input](https://github.com/astral-sh/ruff/blob/96b156303b81c5114e8375a6ffd467fb638c3963/crates/ruff_python_formatter/resources/test/fixtures/ruff/expression/fstring.py#L707)
is invalid, but the previous quote selection fixed the invalid syntax:

```pycon
Python 3.11.13 (main, Sep  2 2025, 14:20:25) [Clang 20.1.4 ] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> f'{1: abcd "{'aa'}" }'  # input
  File "<stdin>", line 1
    f'{1: abcd "{'aa'}" }'
                  ^^
SyntaxError: f-string: expecting '}'
>>> f'{1: abcd "{"aa"}" }'  # old output
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Invalid format specifier ' abcd "aa" ' for object of type 'int'
>>> f'{1: abcd "{'aa'}" }'  # new output
  File "<stdin>", line 1
    f'{1: abcd "{'aa'}" }'
                  ^^
SyntaxError: f-string: expecting '}'
```

We now preserve the invalid syntax in the input.

Unfortunately, this also seems to be another edge case I didn't consider
in https://github.com/astral-sh/ruff/pull/20867 because we don't flag
this as a syntax error after 0.14.1:

<details><summary>Shell output</summary>
<p>

```
> uvx ruff@0.14.0 check --ignore ALL --target-version py311 - <<EOF
f'{1: abcd "{'aa'}" }'
EOF
invalid-syntax: Cannot reuse outer quote character in f-strings on Python 3.11 (syntax was added in Python 3.12)
 --> -:1:14
  |
1 | f'{1: abcd "{'aa'}" }'
  |              ^
  |

Found 1 error.
> uvx ruff@0.14.1 check --ignore ALL --target-version py311 - <<EOF
f'{1: abcd "{'aa'}" }'
EOF
All checks passed!
> uvx python@3.11 -m ast <<EOF
f'{1: abcd "{'aa'}" }'
EOF
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/brent/.local/share/uv/python/cpython-3.11.13-linux-x86_64-gnu/lib/python3.11/ast.py", line 1752, in <module>
    main()
  File "/home/brent/.local/share/uv/python/cpython-3.11.13-linux-x86_64-gnu/lib/python3.11/ast.py", line 1748, in main
    tree = parse(source, args.infile.name, args.mode, type_comments=args.no_type_comments)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/brent/.local/share/uv/python/cpython-3.11.13-linux-x86_64-gnu/lib/python3.11/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<stdin>", line 1
    f'{1: abcd "{'aa'}" }'
                  ^^
SyntaxError: f-string: expecting '}'
```

</p>
</details> 


I assumed that was the same `ParseError` as the one caused by
`f"{1:""}"`, but this is a nested interpolation inside of the format
spec.

## Test Plan

New test copied from the black compatibility test. I guess this is a
duplicate now, I started working on this branch before the new black
tests were imported, so I could delete the separate test in our fixtures
if that's preferable.
2025-10-17 08:49:16 -04:00