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

Break long property chains #9999

Open
winged opened this issue Feb 15, 2024 · 2 comments
Open

Break long property chains #9999

winged opened this issue Feb 15, 2024 · 2 comments
Labels
formatter Related to the formatter style How should formatted code look

Comments

@winged
Copy link

winged commented Feb 15, 2024

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.

@dhruvmanila dhruvmanila added formatter Related to the formatter style How should formatted code look labels Feb 16, 2024
@dhruvmanila
Copy link
Member

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
)

@MichaReiser
Copy link
Member

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter style How should formatted code look
Projects
None yet
Development

No branches or pull requests

3 participants