pygmt icon indicating copy to clipboard operation
pygmt copied to clipboard

Remove trailing white spaces automatically

Open michaelgrund opened this issue 4 years ago • 11 comments

So far trailing white spaces in files are not automatically detected when running make format, make check and make lint (see #890) .

Do you think we should add such check, if possible, to follow the PEP8 guidelines?

Are you willing to help implement and maintain this feature? Discuss first

michaelgrund avatar Feb 19 '21 18:02 michaelgrund

I tried the following script (with invisible trailing whitespaces in many lines):

"""
This is a test.   
"""

def add(x, y):   
    """
    >>> add(5, 4)   
    9   
    """
    # this is a comment   
    return x + y   

Running black removes all trailing whitespaces for me.

Not sure why it hehaves differently in #890.

seisman avatar Feb 19 '21 21:02 seisman

Running black removes all trailing whitespaces for me.

No, black doesn't remove the trailing whitespace in the docstrings of the file.

seisman avatar Feb 19 '21 22:02 seisman

flake8 would check for whitespace and lines >80 (both not caught by black in examples); currently flake8 is only used for pygmt, setup.py, and doc/conf.py.

maxrjones avatar Feb 19 '21 22:02 maxrjones

Running black removes all trailing whitespaces for me.

No, black doesn't remove the trailing whitespace in the docstrings of the file.

Does blackdoc not remove the trailing whitespace in the docstrings? Interesting :thinking: Might be worth filing an issue on https://github.com/keewis/blackdoc if we can confirm this.

weiji14 avatar Feb 19 '21 22:02 weiji14

Does blackdoc not remove the trailing whitespace in the docstrings?

None of black, blackdoc, and docformatter does to the file-scope docstrings. It's possible these are not "docstrings" https://www.python.org/dev/peps/pep-0257/.

seisman avatar Feb 19 '21 22:02 seisman

Related issue: https://github.com/psf/black/issues/318

seisman avatar Feb 20 '21 16:02 seisman

Related issue: psf/black#318

Means it is not possible to do any automatic check and everybody has to care about the spaces before committing, right?

michaelgrund avatar Feb 20 '21 18:02 michaelgrund

Perhaps docformatter should remove the trailing whitespace? It says:

docformatter also handles some of the PEP 8 conventions.

Don't write string literals that rely on significant trailing whitespace. Such trailing whitespace is visually indistinguishable and some editors (or more recently, reindent.py) will trim them.

seisman avatar Feb 20 '21 18:02 seisman

docformatter can remove trailing whitespaces from the following script:

"""
A docstring with some invisible whitespace.     

More description with some invisible trailing whitespaces.   
"""

but does nothing to:

"""
A docstring with some invisible whitespace.     
=================================================

More description with some invisible trailing whitespaces.   
"""

Reported a bug to docformatter at https://github.com/myint/docformatter/issues/71.

seisman avatar Feb 20 '21 22:02 seisman

docformatter can remove trailing whitespaces from the following script:

"""
A docstring with some invisible whitespace.     

More description with some invisible trailing whitespaces.   
"""

but does nothing to:

"""
A docstring with some invisible whitespace.     
=================================================

More description with some invisible trailing whitespaces.   
"""

Reported a bug to docformatter at myint/docformatter#71.

Interesting but good to know that we can differentiate between normal scripts and sphinx-gallery style header docstrings.

michaelgrund avatar Feb 23 '21 07:02 michaelgrund

Perhaps we should try https://github.com/DanielNoord/pydocstringformatter.

seisman avatar Jun 07 '22 01:06 seisman

Trailing spaces in docstrings sometimes have syntactical meaning (e.g., forcing a markdown line break). As questionable as it is to have some count of non-printing characters trigger a material document change, we are where we are (and Markdown is extremely prevalent). Stripping those trailing spaces in docstrings could break the formatting of parameter lists in IDE code hints (and probably some other stuff I haven't thought of).

brnt avatar May 28 '23 23:05 brnt

I'm going to close the issue mainly because: (1) it's a minor issue, (2) it does't bother us in the last two years, (3) modern text editors and IDEs have configurations to do it automatically.

seisman avatar Nov 23 '23 06:11 seisman