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

respect magic trailing commas in return types #3916

Merged
merged 3 commits into from Oct 4, 2023

Conversation

jakkdl
Copy link
Contributor

@jakkdl jakkdl commented Oct 3, 2023

Description

Fixes #3018

I started out with modifying left_hand_split, doing a comprehensive rewrite of it to fully handle all manner of possible cases (see the length of the test file). But after reading more of the existing source code I started realizing that I was duplicating functionality, and in fact the only real thing that needed fixing was making sure that right_hand_split was applied to the function definition first when there's a trailing comma.
I also found a bug in the string splitting of return type and attempted to fix that at the same time ... but I ended up concluding that it's probably better solved in a separate PR (if at all, who even writes their return types like that??) with a very different approach.
I think there's a couple cases that can get improved, mainly cases like:

def aaaaaaaaaaaaaaaaa(
    bbbbbbbbbbbbbbbbbb,
) -> list[Ccccccccccccccccccccccccccccccccccccccccccccccccccc, Dddddd]: ...

would look much better if split on the right hand first. Detecting it also isn't terribly complicated ("split on rhs if the return type is longer than the param list"), I had it working in my original solution that rewrote left_hand_split.

Ended up doing some code duplication due to bracket_split_build_line having the side effect of normalizing the indent, which led to foo def() ->int: .... There's maybe better solutions doing stuff like adding a dont_normalize parameter to it.

Checklist - did you ...

  • Add an entry in CHANGES.md if necessary?
  • Add / update tests if necessary?
  • Add new / update outdated documentation? (Probably not needed)

@github-actions
Copy link

github-actions bot commented Oct 3, 2023

diff-shades results comparing this PR (be48f7b) to main (9e9fdce). The full diff is available in the logs under the "Generate HTML diff report" step.

╭─────────────────────── Summary ────────────────────────╮
│ 1 projects & 4 files changed / 16 changes [+4/-12]     │
│                                                        │
│ ... out of 2 493 972 lines, 11 741 files & 23 projects │
╰────────────────────────────────────────────────────────╯

Differences found.

What is this? | Workflow run | diff-shades documentation

@JelleZijlstra JelleZijlstra self-requested a review October 3, 2023 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Black collapses lists in return type annotations onto one line, regardless of the "magic" trailing comma.
2 participants