ruff/crates/ruff_python_formatter/src
Micha Reiser b64f2ea401
Formatter: Improve single-with item formatting for Python 3.8 or older (#10276)
## Summary

This PR changes how we format `with` statements with a single with item
for Python 3.8 or older. This change is not compatible with Black.

This is how we format a single-item with statement today 

```python
def run(data_path, model_uri):
    with pyspark.sql.SparkSession.builder.config(
        key="spark.python.worker.reuse", value=True
    ).config(key="spark.ui.enabled", value=False).master(
        "local-cluster[2, 1, 1024]"
    ).getOrCreate():
        # ignore spark log output
        spark.sparkContext.setLogLevel("OFF")
        print(score_model(spark, data_path, model_uri))
```

This is different than how we would format the same expression if it is
inside any other clause header (`while`, `if`, ...):

```python
def run(data_path, model_uri):
    while (
        pyspark.sql.SparkSession.builder.config(
            key="spark.python.worker.reuse", value=True
        )
        .config(key="spark.ui.enabled", value=False)
        .master("local-cluster[2, 1, 1024]")
        .getOrCreate()
    ):
        # ignore spark log output
        spark.sparkContext.setLogLevel("OFF")
        print(score_model(spark, data_path, model_uri))

```

Which seems inconsistent to me. 

This PR changes the formatting of the single-item with Python 3.8 or
older to match that of other clause headers.

```python
def run(data_path, model_uri):
    with (
        pyspark.sql.SparkSession.builder.config(
            key="spark.python.worker.reuse", value=True
        )
        .config(key="spark.ui.enabled", value=False)
        .master("local-cluster[2, 1, 1024]")
        .getOrCreate()
    ):
        # ignore spark log output
        spark.sparkContext.setLogLevel("OFF")
        print(score_model(spark, data_path, model_uri))
```

According to our versioning policy, this style change is gated behind a
preview flag.

## Test Plan

See added tests.

Added
2024-03-08 23:56:02 +00:00
..
comments Remove Expr postfix from ExprNamed, ExprIf, and ExprGenerator (#10229) 2024-03-04 12:55:01 +01:00
expression Remove Expr postfix from ExprNamed, ExprIf, and ExprGenerator (#10229) 2024-03-04 12:55:01 +01:00
module Attach dangling comments to the comprehension instead of the if or iter nodes (#7693) 2023-09-29 10:45:01 +01:00
other Formatter: Improve single-with item formatting for Python 3.8 or older (#10276) 2024-03-08 23:56:02 +00:00
pattern New Singleton enum for PatternMatchSingleton node (#8063) 2023-10-30 05:48:53 +00:00
snapshots
statement Formatter: Improve single-with item formatting for Python 3.8 or older (#10276) 2024-03-08 23:56:02 +00:00
string Ruff 2024.2 style (#9639) 2024-02-29 09:30:54 +01:00
type_param Don't move type param opening parenthesis comment (#8163) 2023-10-24 12:02:27 +00:00
builders.rs Refactor with statement formatting to have explicit layouts (#10296) 2024-03-08 18:40:39 -05:00
cli.rs Remove type parameter from parse_* methods (#9466) 2024-01-11 19:41:19 +01:00
context.rs Ruff 2024.2 style (#9639) 2024-02-29 09:30:54 +01:00
generated.rs Remove Expr postfix from ExprNamed, ExprIf, and ExprGenerator (#10229) 2024-03-04 12:55:01 +01:00
lib.rs Implement RUF028 to detect useless formatter suppression comments (#9899) 2024-02-28 19:21:06 +00:00
main.rs Formatter and parser refactoring (#7569) 2023-09-26 15:29:43 +02:00
options.rs Preview minimal f-string formatting (#9642) 2024-02-16 20:28:11 +05:30
prelude.rs
preview.rs Formatter: Improve single-with item formatting for Python 3.8 or older (#10276) 2024-03-08 23:56:02 +00:00
range.rs Remove Expr postfix from ExprNamed, ExprIf, and ExprGenerator (#10229) 2024-03-04 12:55:01 +01:00
shared_traits.rs ruff_python_formatter: copy and inline shared traits (#8656) 2023-11-13 12:16:04 -05:00
verbatim.rs Preview minimal f-string formatting (#9642) 2024-02-16 20:28:11 +05:30