-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Issue 1155: Handle LHS and RHS values differently #1164
Conversation
ec3df6d
to
42923a2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As with your other PR, this is a very large change, which makes it difficult for me to find the time to give it a thorough review.
I noticed that there are significant changes in the formatting output for common cases. We generally avoid making such changes, so that codebases that are already Black-formatted don't see unnecessary change.
Makes sense. What do you think about leaving only part that explodes lines on trailing commas in nested collections, e.g. def test(
[
1,
2,
]
): ... Then we need only changes in |
@vemel, makes sense to leave only the case you show in your last comment. |
Okay, I will prepare a new PR then. |
Hi @ambv, @JelleZijlstra & @vemel, I am very interested in this change, as I have been running
Even if the longer version fits within the line length, I do not want black to collapse the list of arguments, and I don't expect it to since there is a trailing comma (I believe the trailing comma rule only applies to collections that are not nested). This PR appears to solve my problem, but the comments here indicate that it's too radical of a change:
While I certainly understand this sentiment, I would argue that the current behavior is sub-optimal. Taking an intentionally broken apart collection (or call signature) and collapsing it seems to be optimizing for fewer vertical lines, but imo harms code readability and especially harms diff readability. In addition, having distinct behavior around how black interprets trailing commas feels counter-intuitive (e.g. if black treats collections with trailing commas one way, why does it treat call signatures with trailing commas differently?). I was also going to mention the bug where trailing commas are preserved after collapsing call signatures, but I believe that has fixed. Is the implication here that once a style decision has been merged into black's codebase, it can never be changed in order to avoid unexpected changes in existing codebases that are formatted with black? Are there any precedents? Perhaps this sort of thing can be gated behind a major or minor version increment? Thanks for reading! |
This is resolved via other pull requests now. Thank you for the comprehensive contribution but we won't be able to merge this. |
Idea
black
already hasleft_side_split
function, but it is only used for function definitions. The idea is to extract LHS and RHS values from a string, split them separately and join them back to a final result.I added logic for splitting LHS and RHS and updated
SplitFunc
to always be aware of a target line max length. The downside of this approach is that now if LHS can be rendered to a single line - it always does. I need an advice on how to handle it correctly. Probably, we should explode LHS line if RHS has unsplittable type ignore, but it is not always safe to do this.Changes
line_str
argument fromis_line_short_enough
function - not usedLine.is_collection_with_optional_trailing_comma
methodLine.contains_unsplittable_type_ignore
methodLine.get_optional_trailing_comma_index
methodLine.get_leaf_index
methodLine._get_collection_comma_indexes
method - helper forget_optional_trailing_comma_index
Line.get_unsplittable_type_ignore
methodLine._get_assignment_index
method - helper to splitLine.get_left_hand_side
andLine.get_right_hand_side
Line._get_top_level_collection_indexes
method - helper forLine.get_optional_trailing_comma_index
Line.get_left_hand_side
method - returns line with leaves and comment for the LHS valueLine.get_left_right_side
method - returns line with leaves and comment for the RHS value and assignmentLine.should_be_rendered_as_single_line
method - check if line should not be exploded anf fits desired line lengthLine.is_short_enough
method - potential replacement foris_line_short_enough
function, but can handle multilineLine
as wellright_hand_split_with_omit
function - replacement forrhs
function that was created dynamicallyLine.get_source_line_numbers
method to check unsplittable type ignore is on the same lines as LHSsplit_line_side
function -split_line
uses it for LHS and RHSSplitFunc
now haveline_length
andfirst_line_length
argumentsOutput result changes