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 type hints after comments #3362

Open
wants to merge 24 commits into
base: main
Choose a base branch
from

Conversation

charlie572
Copy link

Description

This fixes #3350.

The problem is that when Black encounters standalone comments on one line inside brackets, it splits the line using delimiter_split. This will split on the comment, but it will also split the line on the highest priority delimiter, which isn't always necessary.

I've added a comma_split function, which is the same as delimiter_split, but delimiter_priority = COMMA_PRIORITY. Black will now try comment_split before it tries delimiter_split. This means it will first split on standalone comments and commas, and only split on other delimiters if the lines are still too long after that.

Checklist - did you ...

  • Add an entry in CHANGES.md if necessary?
  • Add / update tests if necessary?
  • Add new / update outdated documentation?

@github-actions
Copy link

github-actions bot commented Oct 28, 2022

diff-shades results comparing this PR (029fce9) to main (3a2d76c). The full diff is available in the logs under the "Generate HTML diff report" step.

╭─────────────────────── Summary ────────────────────────╮
│ 6 projects & 10 files changed / 44 changes [+11/-33]   │
│                                                        │
│ ... out of 2 495 262 lines, 11 738 files & 23 projects │
╰────────────────────────────────────────────────────────╯

Differences found.

What is this? | Workflow run | diff-shades documentation

@charlie572
Copy link
Author

Sorry, I should have run diff-shades before submitting a pull request. I'll keep working on this, and see if I can fix this solution or think of a better one.

@JelleZijlstra
Copy link
Collaborator

It's fine to rely on CI to run diff-shades for you. I haven't looked closely here but probably you just need to gate your changes to the preview style.

This commit makes Black handle cases where there is a comment in the
middle of a statement in brackets. It also removes uses of comma_split()
This reverts commit c2eb395.
@charlie572
Copy link
Author

I think I've got a better solution now.

If there are comments at the beginning or end of the line, I call standalone_comment_split before delimiter_split. Else, I call delimiter_split first. I'm not using the comma_split function I wrote any more.

This means that now, when there is a comment in the middle of an expression or list, it will perform a delimiter split.

(
	a
	and b
	# comment
	and c
	and d
)

But when the comment is at the top or bottom, it won't perform a delimiter split (if it doesn't have to).

(
	# comment
	a and b and c and d
)

Currently, Black performs a delimiter split in both cases, which is unnecessary.

@charlie572
Copy link
Author

charlie572 commented Nov 3, 2022

Should I rebase main onto here to resolve the merge conflict, or will the maintainer do that? The only merge conflict is one line in CHANGES.md.
Also, why isn't CI running?

@JelleZijlstra
Copy link
Collaborator

Easiest to fix simple merge conflicts by going through GitHub's UI, I just did it for you.

@charlie572
Copy link
Author

Okay, thanks.

@aqeelat
Copy link

aqeelat commented Jun 29, 2023

@charlie572 I'm interested in this PR. Can you please rebase?

@charlie572
Copy link
Author

Great. I can do this tomorrow. Do I just need to rebase main onto this branch to fix merge conflicts?

# Conflicts:
#	CHANGES.md
#	src/black/mode.py
This test makes sure a trailing comma is added when there is a comment
in the brackets. However, a delimiter split is no longer performed in
these cases, so there is no need to add a trailing comma. This commit
makes the lines longer so a delimiter split is performed, and the
behaviour with the trailing comma can be tested.
@hauntsaninja
Copy link
Collaborator

Looking at the diff shades report https://github.com/psf/black/actions/runs/5431147643/jobs/9880366015?pr=3362 it looks this causes the "magic" trailing comma to not get respected inside collection literals if there's a comment

@charlie572
Copy link
Author

charlie572 commented Jul 2, 2023

I'm confused about the magic_trailing_comma attribute of the Line class. Why does it point at the closing bracket after the magic trailing comma, and not at the magic trailing comma token? This means that my code can't check for the magic trailing comma, since it is looking at a Line object containing just the comma-separated members, not the surrounding brackets.

I can think of two ways to make it handle magic trailing commas properly:
a. add another attribute that points at the magic trailing comma token
b. search the tokens for a magic trailing comma inside the transform_line function (around line 600).

Option a makes the most sense to me. Is that okay?

@charlie572
Copy link
Author

I implemented option a. But now the attribute names are weird. Maybe magic_trailing_comma should point at the comma, and bracket_after_magic_trailing_comma can point at the following bracket? Or maybe we can remove the latter attribute?

Magic_trailing_comma now points to the actual comma token, and
bracket_after_magic_trailing_comma now points to the following close
bracket.
@aqeelat
Copy link

aqeelat commented Jul 6, 2023

@charlie572 I understand nothing from the code in this PR (I'm not familiar with the internals of Black) but I want to thank you for your effort.

@charlie572
Copy link
Author

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Type hint wrapping after comment
4 participants