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

[563] Fix standalone comments inside complex blocks crashing Black #4016

Merged
merged 9 commits into from Nov 7, 2023

Conversation

henriholopainen
Copy link
Contributor

Description

As described here, and in referenced issues thereof, Black crashes when standalone comments are sprinkled inside complex multiline blocks. This PR takes a first step in fixing the most obvious cases and aims to get rid of blatant crashing. There are known issues with more nested expressions not being indented correctly, e.g.

[
    (
        lambda
        # a comment                                                                                                                     
        : None
    )
]

currently becomes

[
    (lambda
    # a comment                                                                                                                         
    : None)
]

I suggest we tackle those more cosmetic problems in a separate PR.

Checklist - did you ...

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

Bracket depth is not an accurate indicator of standalone comment position inside more complex blocks because bracket depth can be virtual (in loops' and lambdas' parameter blocks) or from optional parens. Here we try to stop cumulating lines upon standalone comments in complex blocks, and try to make standalone comment processing more simple. The fundamental idea is, that if we have a standalone comment, it needs to go on its own line, so we always have to split.

This is not perfect, but at least a first step.
@henriholopainen henriholopainen changed the title Fix standalone comments inside complex blocks crashing Black [563] Fix standalone comments inside complex blocks crashing Black Nov 1, 2023
@henriholopainen
Copy link
Contributor Author

henriholopainen commented Nov 1, 2023

...aaand there seems to be some formatting changes in the stable style, so I'll convert this into a draft until those are resolved.

edit: Resolved 🚀

@henriholopainen henriholopainen marked this pull request as draft November 1, 2023 23:47
Copy link

github-actions bot commented Nov 1, 2023

diff-shades reports zero changes comparing this PR (3b1d969) to main (46be1f8).


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

@henriholopainen henriholopainen marked this pull request as ready for review November 2, 2023 00:20
src/black/lines.py Outdated Show resolved Hide resolved
@JelleZijlstra JelleZijlstra merged commit 66008fd into psf:main Nov 7, 2023
41 checks passed
@henriholopainen
Copy link
Contributor Author

Can we also close the issue?

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.

None yet

2 participants