ruff/crates/ruff_python_formatter/src/statement
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
..
clause.rs Ruff 2024.2 style (#9639) 2024-02-29 09:30:54 +01:00
mod.rs Preserve trailing statement semicolons when using `fmt: skip` (#8273) 2023-10-30 00:07:14 +00:00
stmt_ann_assign.rs Ruff 2024.2 style (#9639) 2024-02-29 09:30:54 +01:00
stmt_assert.rs Implement RUF028 to detect useless formatter suppression comments (#9899) 2024-02-28 19:21:06 +00:00
stmt_assign.rs Ruff 2024.2 style (#9639) 2024-02-29 09:30:54 +01:00
stmt_aug_assign.rs Ruff 2024.2 style (#9639) 2024-02-29 09:30:54 +01:00
stmt_break.rs Implement RUF028 to detect useless formatter suppression comments (#9899) 2024-02-28 19:21:06 +00:00
stmt_class_def.rs Implement `blank_line_after_nested_stub_class` preview style (#9155) 2024-01-31 00:09:38 +05:30
stmt_continue.rs Implement RUF028 to detect useless formatter suppression comments (#9899) 2024-02-28 19:21:06 +00:00
stmt_delete.rs Implement RUF028 to detect useless formatter suppression comments (#9899) 2024-02-28 19:21:06 +00:00
stmt_expr.rs Implement RUF028 to detect useless formatter suppression comments (#9899) 2024-02-28 19:21:06 +00:00
stmt_for.rs Introduce Token element (#7048) 2023-09-02 10:05:47 +02:00
stmt_function_def.rs Implement `blank_line_after_nested_stub_class` preview style (#9155) 2024-01-31 00:09:38 +05:30
stmt_global.rs Implement RUF028 to detect useless formatter suppression comments (#9899) 2024-02-28 19:21:06 +00:00
stmt_if.rs Range formatting API (#9635) 2024-01-31 11:13:37 +01:00
stmt_import.rs Implement RUF028 to detect useless formatter suppression comments (#9899) 2024-02-28 19:21:06 +00:00
stmt_import_from.rs Implement RUF028 to detect useless formatter suppression comments (#9899) 2024-02-28 19:21:06 +00:00
stmt_ipy_escape_command.rs Implement RUF028 to detect useless formatter suppression comments (#9899) 2024-02-28 19:21:06 +00:00
stmt_match.rs Introduce Token element (#7048) 2023-09-02 10:05:47 +02:00
stmt_nonlocal.rs Implement RUF028 to detect useless formatter suppression comments (#9899) 2024-02-28 19:21:06 +00:00
stmt_pass.rs Implement RUF028 to detect useless formatter suppression comments (#9899) 2024-02-28 19:21:06 +00:00
stmt_raise.rs Implement RUF028 to detect useless formatter suppression comments (#9899) 2024-02-28 19:21:06 +00:00
stmt_return.rs Implement RUF028 to detect useless formatter suppression comments (#9899) 2024-02-28 19:21:06 +00:00
stmt_try.rs Introduce Token element (#7048) 2023-09-02 10:05:47 +02:00
stmt_type_alias.rs Ruff 2024.2 style (#9639) 2024-02-29 09:30:54 +01:00
stmt_while.rs Move {AnyNodeRef, AstNode} to ruff_python_ast crate root (#8030) 2023-10-18 00:01:18 +00:00
stmt_with.rs Formatter: Improve single-with item formatting for Python 3.8 or older (#10276) 2024-03-08 23:56:02 +00:00
suite.rs Ruff 2024.2 style (#9639) 2024-02-29 09:30:54 +01:00