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

Format empty lines in stub files like black's preview style #7206

Merged
merged 3 commits into from Sep 11, 2023

Conversation

konstin
Copy link
Member

@konstin konstin commented Sep 6, 2023

Summary

Fix all but one empty line differences with the black preview style in typeshed. The remaining differences are breaking with type comments and trailing commas in function definitions.

I compared the empty line differences with the preview mode of black since stable has some oddities that would have been hard to replicate (psf/black#3861). Additionally, it assumes the style proposed in psf/black#3862.

An edge case that also surfaced with typeshed are newline before trailing module comments.

main

project similarity index total files changed files
cpython 0.76083 1789 1632
django 0.99966 2760 58
transformers 0.99930 2587 447
twine 1.00000 33 0
typeshed 0.99978 3496 2173
warehouse 0.99825 648 22
zulip 0.99950 1437 27

PR

project similarity index total files changed files
cpython 0.76083 1789 1632
django 0.99966 2760 58
transformers 0.99930 2587 447
twine 1.00000 33 0
typeshed 0.99983 3496 18
warehouse 0.99825 648 22
zulip 0.99950 1437 27

Closes #6723

Test Plan

The main driver was the typeshed diff. I added new test cases for all kinds of possible empty line combinations in stub files, test cases for newlines before trailing module comments.

@konstin konstin marked this pull request as draft September 6, 2023 17:18
@konstin konstin added the formatter Related to the formatter label Sep 6, 2023
@konstin konstin force-pushed the typeshed-empty-line-rules branch 3 times, most recently from db5de5d to 76244b7 Compare September 8, 2023 13:27
@konstin konstin changed the title Empty line rules for stub files Format empty lines in stub files closer to black's preview style Sep 8, 2023
@konstin konstin force-pushed the typeshed-empty-line-rules branch 2 times, most recently from d39b8ee to 64ee983 Compare September 8, 2023 15:21
Comment on lines 132 to 135
(PySourceType::Stub, NodeLevel::TopLevel) => 1,
(PySourceType::Stub, _) => 0,
(_, NodeLevel::TopLevel) => 2,
(_, _) => 1,
Copy link
Member Author

Choose a reason for hiding this comment

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

it's a bit counterintuitive that the exceptions are the named cases

Copy link
Member

Choose a reason for hiding this comment

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

Should empty_lines be inlined into empty_lines_before_trailing_comments. It seems the function is only used in stmt_function_def and stmt_class_def and both use the same implementation.

@konstin konstin force-pushed the typeshed-empty-line-rules branch 2 times, most recently from d656154 to 52a7ff9 Compare September 8, 2023 16:18
@konstin konstin marked this pull request as ready for review September 8, 2023 16:29
@konstin konstin changed the title Format empty lines in stub files closer to black's preview style Format empty lines in stub files like black's preview style Sep 8, 2023
Comment on lines 132 to 135
(PySourceType::Stub, NodeLevel::TopLevel) => 1,
(PySourceType::Stub, _) => 0,
(_, NodeLevel::TopLevel) => 2,
(_, _) => 1,
Copy link
Member

Choose a reason for hiding this comment

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

Should empty_lines be inlined into empty_lines_before_trailing_comments. It seems the function is only used in stmt_function_def and stmt_class_def and both use the same implementation.

crates/ruff_python_formatter/src/statement/suite.rs Outdated Show resolved Hide resolved
konstin and others added 2 commits September 11, 2023 09:44
@konstin konstin enabled auto-merge (squash) September 11, 2023 07:52
@konstin konstin merged commit 3a2c3a7 into main Sep 11, 2023
16 checks passed
@konstin konstin deleted the typeshed-empty-line-rules branch September 11, 2023 08:04
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.

Formatter: Empty lines in stub files
2 participants