ruff
ruff copied to clipboard
Break long property chains
Given a code line like the following, Ruff and Black both don't break the line, even it would be possible (and make it more readable IMHO). Black kinda tries to shorten the line a bit, but it would still be too long for the given limit.
This happens for example when extracting data from deeply object data structure.
# Input:
foo = self.some_very.nested.data.which.is_really.really.deep_into_a_data_structure.far.beyond.line_length_limit
# Black 23.12.1:
foo = (
self.some_very.nested.data.which.is_really.really.deep_into_a_data_structure.far.beyond.line_length_limit
)
# Ruff 0.2.1
foo = self.some_very.nested.data.which.is_really.really.deep_into_a_data_structure.far.beyond.line_length_limit
What I would like to see is breaking the property chain into multiple lines, something like this:
# Desired output:
foo = (
self
.some_very
.nested
.data
.which
.is_really
.really
.deep_into_a_data_structure
.far
.beyond
.line_length_limit
)
# Alternative desired output (less readable, but also less wasted lines)
foo = (
self.some_very.nested.data.which.is_really.really
.deep_into_a_data_structure.far.beyond
.line_length_limit
)
Note: This is closely related to #8598, thanks to Micha in Discord for the pointer.
I like this and I'd prefer to break them all into individual lines like in your desired output.
There's the question of indentation as well. Is the following better?
foo = (
self
.some_very
.nested
.data
.which
.is_really
.really
.deep_into_a_data_structure
.far
.beyond
.line_length_limit
)
Thanks for opening this issue. We'll need to look at this holistically to ensure the formatting is consistent. E.g. how should attributes be broken in binary expressions? Should this style only apply in assignment positions or also in clause headers?