astral-sh / ruff

An extremely fast Python linter and code formatter, written in Rust.

Home Page:https://docs.astral.sh/ruff

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

format: different behavior with black for `hug_parens_with_braces_and_square_brackets` and generators

trim21 opened this issue · comments

I'm using ruff format with --preview enable, and it generate different output in this case.

ruff:

import shutil
import asyncio


async def main():
    by_path = {}
    loop = asyncio.get_event_loop()

    if loop:
        value = await asyncio.gather(
            *(
                loop.run_in_executor(None, lambda pp: (pp, shutil.disk_usage(pp).free), p)
                for p in by_path
            )
        )
        return value

    return None

black:

import shutil
import asyncio


async def main():
    by_path = {}
    loop = asyncio.get_event_loop()

    if loop:
        value = await asyncio.gather(*(
            loop.run_in_executor(None, lambda pp: (pp, shutil.disk_usage(pp).free), p)
            for p in by_path
        ))
        return value

    return None

This is not listed in "Known Deviations from Black", is this unexpected behavoir?

pyproject.toml

[tool.black]
line-length = 100
target-version = ['py311']
#future = true

unstable = true
preview = true

[tool.ruff]
cache-dir = ".venv/.cache/ruff"
line-length = 100
target-version = 'py311'

exclude = ['dist', '.venv']

[tool.ruff.format]
preview = true

I think this is related; psf/black#3964 (comment)
And this in black is the part which may be missing in ruff: psf/black#3992

title of this issue is not very accurate and I don't know how black call this feature...

I think it is called hug_parens_with_braces_and_square_brackets:
#9945 (comment)

I think it is called hug_parens_with_braces_and_square_brackets: #9945 (comment)

thanks

It seems like we don't consider generator to be huggable:

I'm not sure if it's intentional or not but Micha might be able to provide more context on this once he's back from his PTO.

Although it does seem that a tuple expression is huggable, so I'd say that the generator should probably be as well.

This is not listed in "Known Deviations from Black", is this unexpected behavior?

We don't track differences for preview styles because both Black's and Ruff's preview styles are in flux, making it difficult to track the differences over time.

I'm not sure if it's intentional or not but Micha might be able to provide more context on this once he's back from his PTO.

I don't think this is intentional. The only thing we need to be mindful of is that generator parentheses in call expressions are optional. Meaning, we should only hug if the generator already has parentheses (the logic must be in sync with the parentheses logic in FormatExprGenerator)