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

Fix multiline lambda expression statement formating #8466

Merged
merged 1 commit into from
Nov 5, 2023

Conversation

MichaReiser
Copy link
Member

Summary

This PR fixes a bug in our formatter where a multiline lambda expression statement was formatted over multiple lines without adding parentheses.

The PR "fixes" the problem by not splitting the lambda parameters if it is not parenthesized

Test Plan

Added test

@MichaReiser
Copy link
Member Author

MichaReiser commented Nov 3, 2023

Current dependencies on/for this PR:

This stack of pull requests is managed by Graphite.

@MichaReiser MichaReiser added the formatter Related to the formatter label Nov 3, 2023
@MichaReiser MichaReiser force-pushed the fix-multiline-statement-lambda branch from a35b27e to e7fb0ac Compare November 3, 2023 08:10

lambda self, araa, kkkwargs=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(*args, **kwargs), e=1, f=2, g=2: d

# Regression tests for https://github.com/astral-sh/ruff/issues/8179
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR does not fix this formatting. It only adds tests for it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this PR fix? Sorry, trying to parse!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait sorry, I now understand.

let separator = format_with(|f: &mut PyFormatter| {
token(",").fmt(f)?;

if f.context().node_level().is_parenthesized() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testing for self.parentheses == ParameterParentheses::Never is, unfortunately, not sufficient because it doesn't handle comments:

a = [
	lambda x # comment
		x 
		# comment
	: b
]

This uses the Never layout but the lambda has comments. The example also shows that testing whether the lambda expression itself is parenthesized isn't sufficient.

@MichaReiser MichaReiser force-pushed the fix-multiline-statement-lambda branch from e7fb0ac to 3e218fa Compare November 3, 2023 09:25
Copy link
Contributor

github-actions bot commented Nov 3, 2023

ruff-ecosystem results

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

@charliermarsh charliermarsh merged commit e57bccd into main Nov 5, 2023
17 checks passed
@charliermarsh charliermarsh deleted the fix-multiline-statement-lambda branch November 5, 2023 14:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants