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

Behavior on all-whitespace files differs from Black #166

Closed
akaihola opened this issue Jul 19, 2021 · 2 comments · Fixed by #197
Closed

Behavior on all-whitespace files differs from Black #166

akaihola opened this issue Jul 19, 2021 · 2 comments · Fixed by #197
Assignees
Labels
bug Something isn't working
Milestone

Comments

@akaihola
Copy link
Owner

As discovered in #164, Darker strips all-whitespace Python files which have been modified. This is different from Black which leaves them untouched. See also this comment to psf/black#779.

We could replace (in darker.black_diff.run_black()):

    return TextDocument.from_str(
        format_str(contents_for_black, mode=mode),
        encoding=src_contents.encoding,
        override_newline=src_contents.newline,
    )

with

    try:
        reformatted = format_file_contents(contents_for_black, fast=True, mode=mode)
    except NothingChanged:
        reformatted = contents_for_black
    return TextDocument.from_str(
        reformatted,
        encoding=src_contents.encoding,
        override_newline=src_contents.newline,
    )

to match black's behavior on all-whitespace files. The other option is to handle all-whitespace files in darker just like black does:

    contents_for_black = src_contents.string_with_newline("\n")
    if not contents_for_black.strip():
        return src_contents
    return TextDocument.from_str(
        format_str(contents_for_black, mode=mode),
        encoding=src_contents.encoding,
        override_newline=src_contents.newline,
    )

Which approach to choose should depend on Black's possible choice of a public Python API (psf/black#779).

@akaihola akaihola added the bug Something isn't working label Jul 19, 2021
@akaihola akaihola added this to the 1.3.0 milestone Jul 19, 2021
@akaihola akaihola self-assigned this Jul 19, 2021
@akaihola akaihola modified the milestones: 1.3.0, 1.3.1 Sep 4, 2021
@akaihola
Copy link
Owner Author

akaihola commented Sep 5, 2021

Actually, Black developers seem to agree in psf/black#2382 that the behavior is currently wrong, and Black should format all-whitespace files as just a single newline.

So instead of matching Black's current behavior, we could just fix Darker to truncate all-whitespace files to a single newline instead of a zero-length file.

@akaihola
Copy link
Owner Author

akaihola commented Sep 7, 2021

I've submitted pull request psf/black#2484 to Black. It fixes Black so it always reformats zero-length and all-whitespace files into one empty line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant