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

formatter prefers to break annotation over arguments list #6352

Closed
davidszotten opened this issue Aug 4, 2023 · 11 comments · Fixed by #6410
Closed

formatter prefers to break annotation over arguments list #6352

davidszotten opened this issue Aug 4, 2023 · 11 comments · Fixed by #6410
Labels
formatter Related to the formatter

Comments

@davidszotten
Copy link
Contributor

input:

def foo(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) -> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:
    ...

black (better choice imo)

def foo(
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,
) -> aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa:
    ...

ruff

def foo(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) -> (
    aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
):
    ...
@tylerlaprade
Copy link
Contributor

I'm pretty sure Black is changing this in their next version. Try running Black with the --preview flag enabled.

@davidszotten
Copy link
Contributor Author

i see the same behaviour even with --preview

(black, 23.7.0 (compiled: yes))

@MichaReiser
Copy link
Member

This shouldn't be to hard to fix. The arguments group must enclose the arguments and the return type annotation. This will make the printer to expand from left to right (outer most before inner groups)

@tylerlaprade
Copy link
Contributor

https://black.readthedocs.io/en/stable/the_black_code_style/future_style.html#improved-line-breaks

Ah, I see, I guess this only applies to assignment. I assumed it would be similar for function definitions.

@davidszotten
Copy link
Contributor Author

This shouldn't be to hard to fix. The arguments group must enclose the arguments and the return type annotation. This will make the printer to expand from left to right (outer most before inner groups)

not sure i quote follow what you mean by "the arguments group". i tried wrapping

write!(f, [item.arguments().format()])?;
if let Some(return_annotation) = item.returns() {
in a group but that didn't do the trick

@MichaReiser
Copy link
Member

MichaReiser commented Aug 5, 2023

The formatting of arguments creates a group. We'll need to create that group on the function level instead so that it includes both the arguments and the returns, similar to Prettier

@MichaReiser MichaReiser added the formatter Related to the formatter label Aug 5, 2023
@davidszotten
Copy link
Contributor Author

sorry i can't get it to work. might have to leave for someone else

@charliermarsh
Copy link
Member

I'll give it a look.

@charliermarsh
Copy link
Member

I don't quite understand how to make the composition work here given that parenthesized and related helpers all create groups. IIUC we'd need to somehow pass the returns to the Parameters formatter, and then change parenthesized to allow for content after the maybe-parenthesized nodes that's part of the same group?

@charliermarsh
Copy link
Member

I will revisit this today.

@MichaReiser
Copy link
Member

You probably need to pull in the relevant parts from parenthesized into the function formatting. It's different enough. We did that previously with other formatting too (I can't recall out of my head which formatting it was but it manually creates the groups and sets the node level)

charliermarsh added a commit that referenced this issue Aug 9, 2023
)

## Summary

This PR removes the group around function definition parameters, instead
grouping the parameters with the type parameters and return type
annotation.

This increases Zulip's similarity score from 0.99385 to 0.99699, so it's
a meaningful improvement. However, there's at least one stability error
that I'm working on, and I'm really just looking for high-level feedback
at this point, because I'm not happy with the solution.

Closes #6352.

## Test Plan

Before:

- `zulip`: 0.99396
- `django`: 0.99784
- `warehouse`: 0.99578
- `build`: 0.75436
- `transformers`: 0.99407
- `cpython`: 0.75987
- `typeshed`: 0.74432

After:

- `zulip`: 0.99702
- `django`: 0.99784
- `warehouse`: 0.99585
- `build`: 0.75623
- `transformers`: 0.99470
- `cpython`: 0.75988
- `typeshed`: 0.74853
durumu pushed a commit to durumu/ruff that referenced this issue Aug 12, 2023
…tral-sh#6410)

## Summary

This PR removes the group around function definition parameters, instead
grouping the parameters with the type parameters and return type
annotation.

This increases Zulip's similarity score from 0.99385 to 0.99699, so it's
a meaningful improvement. However, there's at least one stability error
that I'm working on, and I'm really just looking for high-level feedback
at this point, because I'm not happy with the solution.

Closes astral-sh#6352.

## Test Plan

Before:

- `zulip`: 0.99396
- `django`: 0.99784
- `warehouse`: 0.99578
- `build`: 0.75436
- `transformers`: 0.99407
- `cpython`: 0.75987
- `typeshed`: 0.74432

After:

- `zulip`: 0.99702
- `django`: 0.99784
- `warehouse`: 0.99585
- `build`: 0.75623
- `transformers`: 0.99470
- `cpython`: 0.75988
- `typeshed`: 0.74853
@MichaReiser MichaReiser added this to the Formatter: Alpha milestone Aug 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants