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

Black deviation with if x in some comprehension #8090

Closed
philippgl opened this issue Oct 20, 2023 · 3 comments · Fixed by #8100
Closed

Black deviation with if x in some comprehension #8090

philippgl opened this issue Oct 20, 2023 · 3 comments · Fixed by #8100
Assignees
Labels
bug Something isn't working formatter Related to the formatter

Comments

@philippgl
Copy link

I just fund a small difference with the black formatter.

  • A minimal code snippet that reproduces the bug.

This is the black formatted example (imho, this also looks nicer):

def some_function():
    if "root" not in (
        long_tree_name_tree.split("/")[0]
        for long_tree_name_tree in really_really_long_variable_name
    ):
        msg = "Could not find root. Please try a different forest."
        raise ValueError(msg)

which gets reformatted to:

def some_function():
    if (
        "root"
        not in (
            long_tree_name_tree.split("/")[0]
            for long_tree_name_tree in really_really_long_variable_name
        )
    ):
        msg = "Could not find root. Please try a different forest."
        raise ValueError(msg)
  • The command you invoked (e.g., ruff /path/to/file.py --fix), ideally including the --isolated flag.
ruff format --isolated filename.py
  • The current Ruff settings (any relevant sections from your pyproject.toml).

None. This is a standalone script.

  • The current Ruff version (ruff --version).

ruff 0.0.292
ruff 0.1.0

(I tried it with both versions)

@MichaReiser MichaReiser added this to the Formatter: Stable milestone Oct 20, 2023
@MichaReiser MichaReiser added the formatter Related to the formatter label Oct 20, 2023
@MichaReiser
Copy link
Member

MichaReiser commented Oct 20, 2023

Yeah, Black's output certainly looks better. I think the issue is that our can_omit_optional_parentheses returns false for the condition where Black returns true

@charliermarsh
Copy link
Member

I'll take a quick look here.

@charliermarsh
Copy link
Member

Thanks for reporting, great issue.

charliermarsh added a commit that referenced this issue Oct 22, 2023
## Summary

When analyzing:

```python
if "root" not in (
    long_tree_name_tree.split("/")[0]
    for long_tree_name_tree in really_really_long_variable_name
):
    msg = "Could not find root. Please try a different forest."
    raise ValueError(msg)
```

We missed that the generator expression is parenthesized, because the
parentheses are _part_ of the generator -- so
`is_expression_parenthesized` returns `False`. We needed to take into
account that generators and tuples may or may not be parenthesized when
determining whether we can omit parentheses while splitting an
expression.

Closes #8090.

## Test Plan

No changes in similarity.

Before:

| project | similarity index | total files | changed files |

|----------------|------------------:|------------------:|------------------:|
| cpython | 0.75803 | 1799 | 1647 |
| django | 0.99983 | 2772 | 34 |
| home-assistant | 0.99953 | 10596 | 186 |
| poetry | 0.99891 | 317 | 17 |
| transformers | 0.99966 | 2657 | 330 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99978 | 3669 | 20 |
| warehouse | 0.99977 | 654 | 13 |
| zulip | 0.99970 | 1459 | 22 |

After:

| project | similarity index | total files | changed files |

|----------------|------------------:|------------------:|------------------:|
| cpython | 0.75803 | 1799 | 1647 |
| django | 0.99983 | 2772 | 34 |
| home-assistant | 0.99953 | 10596 | 186 |
| poetry | 0.99891 | 317 | 17 |
| transformers | 0.99966 | 2657 | 330 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99978 | 3669 | 20 |
| warehouse | 0.99977 | 654 | 13 |
| zulip | 0.99970 | 1459 | 22 |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working formatter Related to the formatter
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants