Skip to content

Comments

Exclude pragma comments from measured line width#7008

Merged
MichaReiser merged 7 commits intoastral-sh:mainfrom
cnpryer:exclude-pragmas
Sep 1, 2023
Merged

Exclude pragma comments from measured line width#7008
MichaReiser merged 7 commits intoastral-sh:mainfrom
cnpryer:exclude-pragmas

Conversation

@cnpryer
Copy link
Contributor

@cnpryer cnpryer commented Aug 30, 2023

Closes #6772

Summary

If the comment doesn't include a NBSP and starts with noqa, type:, pyright:, or pylint: then we assign it 0 reserved width for its line suffix. Otherwise measure width.

See #6772 (comment)

This PR aligns with ruff linting behavior in that it will disregard the kind of white-space in order to identify pragmas to exclude from measured line width.

Test Plan

Current fixtures and new trailing comments fixtures.

@cnpryer cnpryer marked this pull request as ready for review August 30, 2023 22:33
@MichaReiser MichaReiser added the formatter Related to the formatter label Aug 31, 2023
Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Looking good. I've one small nit and would like to ask you to add a few pragma comment-specific tests that test the behavior outside of black's test suite (you may want to include some of your interesting NBSP cases too!)

This PR

project similarity index total files changed files
cpython 0.76079 1789 1634
django 0.99948 2760 85
transformers 0.99925 2587 495
twine 0.99982 33 2
typeshed 0.99978 3496 2172
warehouse 0.99661 648 39
zulip 0.99942 1437 42

Base

project similarity index total files changed files
cpython 0.76079 1789 1635
django 0.99948 2760 85
transformers 0.99925 2587 495
twine 0.99965 33 3
typeshed 0.99947 3496 2173
warehouse 0.99659 648 41
zulip 0.99938 1437 47

Funny how typeshed is close to a similarity index of 1 but every other file gets reformatted 🙈

@cnpryer
Copy link
Contributor Author

cnpryer commented Aug 31, 2023

NBSP is such a niche thing to focus on, so I'll move on soon, but while adding these fixtures I noticed the following isn't aligned between the formatter and black:

# Input: \u{a0}\u{a0}type
i = ""  #  type: Add space before two leading NBSP

# Ruff: \u{a0}type
i = ""  #  type: Add space before two leading NBSP

# Black: \u{a0}\u{a0}type
i = ""  #   type: Add space before two leading NBSP

Should be simple, so I'll submit it in a follow up PR and correct any snapshots.

i = "" #type: A space is added
i = "" #  type: Add space before leading NBSP followed by a space
i = "" # type: Add space before leading NBSP
i = "" #  type: Add space before two leading NBSP
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note I'll address this in a follow up PR. See #7008 (comment)



# A noqa as `#\u{A0}\u{A0}noqa` becomes `# \u{A0}noqa`
i = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" #  noqa
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same comment about aligning with black for NBSP edge cases

# Input: #\u{a0}\u{a0}noqa
i = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"  #  noqa

# Black: # \u{a0}noqa
i = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"  #  noqa

# Ruff: # noqa
i = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"  # noqa

@cnpryer
Copy link
Contributor Author

cnpryer commented Aug 31, 2023

I'm going to mark as a draft and do another review of this during lunch today (~3 hours from now; would review my fixtures and the resolved merge conflict again).

If you decide to merge anyway I'm cool with that too.

@cnpryer cnpryer marked this pull request as draft August 31, 2023 13:31
@codspeed-hq
Copy link

codspeed-hq bot commented Aug 31, 2023

CodSpeed Performance Report

Merging #7008 will degrade performances by 1.52%

Comparing cnpryer:exclude-pragmas (a8c1f55) with main (f4ba0ea)

Summary

❌ 1 regressions
✅ 19 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main cnpryer:exclude-pragmas Change
linter/all-rules[pydantic/types.py] 71.8 ms 72.9 ms -1.52%

@cnpryer cnpryer marked this pull request as ready for review August 31, 2023 16:45
@cnpryer cnpryer changed the title Exclude pragma comments from measured width Exclude pragma comments from measured line width Aug 31, 2023
@MichaReiser MichaReiser enabled auto-merge (squash) September 1, 2023 06:25
@MichaReiser MichaReiser merged commit 17a44c0 into astral-sh:main Sep 1, 2023
@cnpryer cnpryer deleted the exclude-pragmas branch September 1, 2023 16:32
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.

Exclude pragma comments from measured line width

2 participants