mirror of
https://github.com/astral-sh/ruff
synced 2026-01-20 21:10:48 -05:00
## 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
25 lines
1.1 KiB
Rust
25 lines
1.1 KiB
Rust
//! Helpers to test if a specific preview style is enabled or not.
|
|
//!
|
|
//! The motivation for these functions isn't to avoid code duplication but to ease promoting preview styles
|
|
//! to stable. The challenge with directly using [`is_preview`](PyFormatContext::is_preview) is that it is unclear
|
|
//! for which specific feature this preview check is for. Having named functions simplifies the promotion:
|
|
//! Simply delete the function and let Rust tell you which checks you have to remove.
|
|
|
|
use crate::PyFormatContext;
|
|
|
|
/// Returns `true` if the [`hug_parens_with_braces_and_square_brackets`](https://github.com/astral-sh/ruff/issues/8279) preview style is enabled.
|
|
pub(crate) const fn is_hug_parens_with_braces_and_square_brackets_enabled(
|
|
context: &PyFormatContext,
|
|
) -> bool {
|
|
context.is_preview()
|
|
}
|
|
|
|
/// Returns `true` if the [`f-string formatting`](https://github.com/astral-sh/ruff/issues/7594) preview style is enabled.
|
|
pub(crate) fn is_f_string_formatting_enabled(context: &PyFormatContext) -> bool {
|
|
context.is_preview()
|
|
}
|
|
|
|
pub(crate) fn is_with_single_item_pre_39_enabled(context: &PyFormatContext) -> bool {
|
|
context.is_preview()
|
|
}
|