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

problematic formatting: indentation change in docstring #4276

Open
kaddkaka opened this issue Mar 15, 2024 · 7 comments
Open

problematic formatting: indentation change in docstring #4276

kaddkaka opened this issue Mar 15, 2024 · 7 comments
Labels
T: style What do we want Blackened code to look like?

Comments

@kaddkaka
Copy link

black might destroy ascii art in dosctring when re-indenting docstring lines.

Example code:

def dosctring_ascii_alignment():
    """Ascii art: [____,aaaa,bbbb]
                   ^         ^
    """
    pass

result after 'black`:

def dosctring_ascii_alignment():
    """Ascii art: [____,aaaa,bbbb]
    ^         ^
    """
    pass

as seen, the carets ^ are now misaligned. This can in problematic cases destroy information and be very confusing, misleading and lead to future mistakes.

In my mind it would be better to not touch docstrings - but I guess there might be some reason for it?

(original PR on ruff: astral-sh/ruff#10396)

@kaddkaka kaddkaka added the T: style What do we want Blackened code to look like? label Mar 15, 2024
@JelleZijlstra
Copy link
Collaborator

This is essentially a duplicate of #2150. We re-indent docstrings because it is sometimes necessary in cases where we re-indent the code itself, and we have previously rejected requests to make this configurable. I proposed a framework towards making this configurable (#3087), but for better or worse it was rejected.

@bryevdv
Copy link

bryevdv commented Mar 16, 2024

I would just re-iterate that docstring are data, not code, and as such should never be modified by a code formatter for any reason, whatsoever. This is certainly and by far the biggest mistake black has ever made, and also a main reason we abandoned it on our projects.

@kaddkaka
Copy link
Author

@bryevdv have you found some other usable formatter? I'm leaning towards autopep8 currently. (made a comparison tool for my own sake: https://github.com/kaddkaka/python-formatting-examples)

@bryevdv
Copy link

bryevdv commented Mar 16, 2024

@kaddkaka No, we simply do not use a formatter at this time.

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Mar 21, 2024

In case it's useful, it appears Black uses this logic https://peps.python.org/pep-0257/#handling-docstring-indentation when handling docstring whitespace. This means that if you have some other text in your docstring that's at the minimum indentation, Black will not destroy your ASCII art. The easiest way to do this would be to put a new line:

def docstring_ascii_alignment():
    """
    Ascii art: [____,aaaa,bbbb]
                ^         ^
    """
    pass

@bryevdv
Copy link

bryevdv commented Mar 21, 2024

Tools are supposed to work for you, not the other way around. 🙂

@kaddkaka
Copy link
Author

In case it's useful, it appears Black uses this logic https://peps.python.org/pep-0257/#handling-docstring-indentation when handling docstring whitespace. This means that if you have some other text in your docstring that's at the minimum indentation, Black will not destroy your ASCII art. The easiest way to do this would be to put a new line:

def docstring_ascii_alignment():
    """
    Ascii art: [____,aaaa,bbbb]
                ^         ^
    """
    pass

It's good to know, but it's no help since it doesn't magically fix all existing code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

4 participants