Improve function declaration wrapping when it contains generic type definitions #4553
+206
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR addresses #4071 by improving how Black wraps function definitions that include generic type declarations.
Current Behavior
When a function with generic type declarations is formatted, Black prioritizes wrapping the generic type definitions, even in cases where splitting the arguments would result in a cleaner layout. For example:
Input:
Current Output:
New Behavior
With this improvement, Black prioritizes wrapping the arguments instead of the generic type definitions, resulting in more readable code:
Improved Output:
However, Black will still wrap the generic type declarations if:
For example:
Input:
Output:
Implementation details:
In the
left_hand_split
function, black would search for an opening brackets leaf in function declaration. It was not differentiating between squared brackets and parenthesis, thus it was treating function parameters and generic type definitions the same way. Since generic type definitions comes first, it would always wrap them first.The solution was to just make it ignore the opening of square brackets in all cases, expect:
Checklist
CHANGES.md
if necessary.