Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format: different behavior with black for hug_parens_with_braces_and_square_brackets and generators #11375

Open
trim21 opened this issue May 12, 2024 · 6 comments
Labels
formatter Related to the formatter preview Related to preview mode features

Comments

@trim21
Copy link

trim21 commented May 12, 2024

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
@charliermarsh charliermarsh added the formatter Related to the formatter label May 13, 2024
@153957
Copy link
Contributor

153957 commented May 13, 2024

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

@trim21
Copy link
Author

trim21 commented May 13, 2024

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

@153957
Copy link
Contributor

153957 commented May 13, 2024

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

@trim21
Copy link
Author

trim21 commented May 13, 2024

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

thanks

@trim21 trim21 changed the title format: different behavior with black for sole function parameter format: different behavior with black for hug_parens_with_braces_and_square_brackets May 13, 2024
@dhruvmanila
Copy link
Member

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.

@MichaReiser MichaReiser added the preview Related to preview mode features label May 27, 2024
@MichaReiser MichaReiser changed the title format: different behavior with black for hug_parens_with_braces_and_square_brackets format: different behavior with black for hug_parens_with_braces_and_square_brackets and generators May 27, 2024
@MichaReiser
Copy link
Member

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter preview Related to preview mode features
Projects
None yet
Development

No branches or pull requests

5 participants