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

Insert newline between docstring and following own line comment #8216

Merged
merged 2 commits into from
Oct 30, 2023

Conversation

konstin
Copy link
Member

@konstin konstin commented Oct 25, 2023

Summary Previously, own line comment following after a docstring followed by newline(s) before the first content statement were treated as trailing on the docstring and we didn't insert a newline after the docstring as black would.

Before:

class ModuleBrowser:
    """Browse module classes and functions in IDLE."""
    # This class is also the base class for pathbrowser.PathBrowser.

    def __init__(self, master, path, *, _htest=False, _utest=False):
        pass

After:

class ModuleBrowser:
    """Browse module classes and functions in IDLE."""

    # This class is also the base class for pathbrowser.PathBrowser.

    def __init__(self, master, path, *, _htest=False, _utest=False):
        pass

I'm not entirely happy about hijacking handle_own_line_comment_between_statements, but i don't know a better spot to put it.

Fixes #7948

Test Plan Fixtures

@konstin konstin added the formatter Related to the formatter label Oct 25, 2023
@konstin konstin requested a review from MichaReiser October 25, 2023 13:44
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.

I like the solution

//
// def __init__(self, master, path, *, _htest=False, _utest=False):
// pass
// ```
Copy link
Member

@charliermarsh charliermarsh Oct 26, 2023

Choose a reason for hiding this comment

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

What if there's no statement in the body after the comment? Like:

class ModuleBrowser:
    """Browse...."""
    # Insert a newline above

Black inserts a newline there -- does this handle that case?

Copy link
Member

Choose a reason for hiding this comment

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

Looks like it doesn't support that case yet. How else could we approach it? 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed, manually inserting a newline seems to be the best option

}
}

trailing_comments(node_comments.trailing).fmt(f)
Copy link
Member

Choose a reason for hiding this comment

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

This should only be applied to class docstrings, right? (Wouldn't it also apply to function docstrings as written?)

Copy link
Member

Choose a reason for hiding this comment

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

@konstin - Sorry to ping, did this get addressed? I don't see test cases for it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Addressed in #8375

)?;

// Comments after docstrings need a newline between the docstring and the comment, so we attach
// it as leading on the first statement after the docstring.
Copy link
Member

Choose a reason for hiding this comment

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

I think this comment is stale.

@konstin konstin force-pushed the insert-newline-between-docstring-and-comment branch from 9b1698f to 532014b Compare October 30, 2023 13:06
@konstin konstin force-pushed the insert-newline-between-docstring-and-comment branch from 532014b to 4ff458a Compare October 30, 2023 13:07
@konstin konstin enabled auto-merge (squash) October 30, 2023 13:09
@konstin konstin merged commit b6c4074 into main Oct 30, 2023
16 checks passed
@konstin konstin deleted the insert-newline-between-docstring-and-comment branch October 30, 2023 13:18
@github-actions
Copy link
Contributor

PR Check Results

Ecosystem

✅ ecosystem check detected no format changes.

@MichaReiser
Copy link
Member

It seems that this PR regressed the Black compatibility for django, home-assistan, transformers, and zullip. The index is unchanged, but each project has significantly more changed files:

Before

project similarity index total files changed files
django 0.99984 2772 33
home-assistant 0.99963 10596 148
transformers 0.99967 2657 328
zulip 0.99970 1459 22

After

project similarity index total files changed files
django 0.99984 2772 48
home-assistant 0.99963 10596 181
transformers 0.99967 2657 339
zulip 0.99970 1459 23

I'm not sure why this wasn't catched by the ecosystem check (CC: @zanieb) but we should make sure that this doesn't get released by either reverting the change, or following up with a follow up PR before the next release goes out.

konstin added a commit that referenced this pull request Oct 31, 2023
Fixup for #8216 to not apply to function docstrings.

Main before #8216:

| project        | similarity index  | total files       | changed files     |
|----------------|------------------:|------------------:|------------------:|
| cpython        |           0.75804 |              1799 |              1648 |
| django         |           0.99984 |              2772 |                33 |
| home-assistant |           0.99963 |             10596 |               148 |
| poetry         |           0.99925 |               317 |                12 |
| transformers   |           0.99967 |              2657 |               328 |
| twine          |           1.00000 |                33 |                 0 |
| typeshed       |           0.99980 |              3669 |                18 |
| warehouse      |           0.99977 |               654 |                13 |
| zulip          |           0.99970 |              1459 |                22 |

main now:

| project        | similarity index  | total files       | changed files     |
|----------------|------------------:|------------------:|------------------:|
| cpython        |           0.75804 |              1799 |              1648 |
| django         |           0.99984 |              2772 |                48 |
| home-assistant |           0.99963 |             10596 |               181 |
| poetry         |           0.99925 |               317 |                12 |
| transformers   |           0.99967 |              2657 |               339 |
| twine          |           1.00000 |                33 |                 0 |
| typeshed       |           0.99980 |              3669 |                18 |
| warehouse      |           0.99977 |               654 |                13 |
| zulip          |           0.99970 |              1459 |                23 |

PR:

| project        | similarity index  | total files       | changed files     |
|----------------|------------------:|------------------:|------------------:|
| cpython        |           0.75804 |              1799 |              1648 |
| django         |           0.99984 |              2772 |                33 |
| home-assistant |           0.99963 |             10596 |               148 |
| poetry         |           0.99925 |               317 |                12 |
| transformers   |           0.99967 |              2657 |               328 |
| twine          |           1.00000 |                33 |                 0 |
| typeshed       |           0.99980 |              3669 |                18 |
| warehouse      |           0.99977 |               654 |                13 |
| zulip          |           0.99970 |              1459 |                22 |
konstin added a commit that referenced this pull request Oct 31, 2023
Fixup for #8216 to not apply to function docstrings.

Main before #8216:

| project        | similarity index  | total files       | changed files     |
|----------------|------------------:|------------------:|------------------:|
| cpython        |           0.75804 |              1799 |              1648 |
| django         |           0.99984 |              2772 |                33 |
| home-assistant |           0.99963 |             10596 |               148 |
| poetry         |           0.99925 |               317 |                12 |
| transformers   |           0.99967 |              2657 |               328 |
| twine          |           1.00000 |                33 |                 0 |
| typeshed       |           0.99980 |              3669 |                18 |
| warehouse      |           0.99977 |               654 |                13 |
| zulip          |           0.99970 |              1459 |                22 |

main now:

| project        | similarity index  | total files       | changed files     |
|----------------|------------------:|------------------:|------------------:|
| cpython        |           0.75804 |              1799 |              1648 |
| django         |           0.99984 |              2772 |                48 |
| home-assistant |           0.99963 |             10596 |               181 |
| poetry         |           0.99925 |               317 |                12 |
| transformers   |           0.99967 |              2657 |               339 |
| twine          |           1.00000 |                33 |                 0 |
| typeshed       |           0.99980 |              3669 |                18 |
| warehouse      |           0.99977 |               654 |                13 |
| zulip          |           0.99970 |              1459 |                23 |

PR:

| project        | similarity index  | total files       | changed files     |
|----------------|------------------:|------------------:|------------------:|
| cpython        |           0.75804 |              1799 |              1648 |
| django         |           0.99984 |              2772 |                33 |
| home-assistant |           0.99963 |             10596 |               148 |
| poetry         |           0.99925 |               317 |                12 |
| transformers   |           0.99967 |              2657 |               328 |
| twine          |           1.00000 |                33 |                 0 |
| typeshed       |           0.99980 |              3669 |                18 |
| warehouse      |           0.99977 |               654 |                13 |
| zulip          |           0.99970 |              1459 |                22 |
charliermarsh pushed a commit that referenced this pull request Oct 31, 2023
Fixup for #8216 to not apply to function docstrings.

Main before #8216:

| project | similarity index | total files | changed files |

|----------------|------------------:|------------------:|------------------:|
| cpython | 0.75804 | 1799 | 1648 |
| django | 0.99984 | 2772 | 33 |
| home-assistant | 0.99963 | 10596 | 148 |
| poetry | 0.99925 | 317 | 12 |
| transformers | 0.99967 | 2657 | 328 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99980 | 3669 | 18 |
| warehouse | 0.99977 | 654 | 13 |
| zulip | 0.99970 | 1459 | 22 |

main now:

| project | similarity index | total files | changed files |

|----------------|------------------:|------------------:|------------------:|
| cpython | 0.75804 | 1799 | 1648 |
| django | 0.99984 | 2772 | 48 |
| home-assistant | 0.99963 | 10596 | 181 |
| poetry | 0.99925 | 317 | 12 |
| transformers | 0.99967 | 2657 | 339 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99980 | 3669 | 18 |
| warehouse | 0.99977 | 654 | 13 |
| zulip | 0.99970 | 1459 | 23 |

PR:

| project | similarity index | total files | changed files |

|----------------|------------------:|------------------:|------------------:|
| cpython | 0.75804 | 1799 | 1648 |
| django | 0.99984 | 2772 | 33 |
| home-assistant | 0.99963 | 10596 | 148 |
| poetry | 0.99925 | 317 | 12 |
| transformers | 0.99967 | 2657 | 328 |
| twine | 1.00000 | 33 | 0 |
| typeshed | 0.99980 | 3669 | 18 |
| warehouse | 0.99977 | 654 | 13 |
| zulip | 0.99970 | 1459 | 22 |
zanieb added a commit that referenced this pull request Nov 2, 2023
Previously, the ecosystem checks formatted with the baseline then
formatted again with `--diff` to get the changed files.

Now, the ecosystem checks support a new mode where we:
- Format with the baseline
- Commit the changes
- Reset to the target ref
- Format again
- Check the diff from the baseline commit

This effectively tests Ruff changes on unformatted code rather than
changes in previously formatted code (unless, of course, the project is
already using Ruff).

While this mode is the new default, I've retained the old one for local
checks. The mode can be toggled with `--format-comparison <type>`.

Includes some more aggressive resetting of the GitHub repositories when
cached.

Here, I've also stubbed comparison modes in which `black` is used as the
baseline. While these do nothing here, #8419 adds support.

I tested this with the commit from #8216 and ecosystem changes appear
https://gist.github.com/zanieb/a982ec8c392939043613267474471a6e
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.

Insert newline after docstring even when there are comments
3 participants