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

New version removes blank in doctest, so all doctests fail. #1654

Open
henrifroese opened this issue Aug 29, 2020 · 7 comments
Open

New version removes blank in doctest, so all doctests fail. #1654

henrifroese opened this issue Aug 29, 2020 · 7 comments
Labels
F: docstrings How we format docstrings T: bug Something isn't working

Comments

@henrifroese
Copy link

henrifroese commented Aug 29, 2020

Description

After updating from 19.10b0 to 20.8, black removes trailing whitespaces in our doctests (that Pandas returns), so all the doctests fail. In the example below, the trailing blank is after "Texthero" in the doctest's output.

To Reproduce

Create a file

def f(s):
    """
    Remove content within parentheses '()' and the parentheses by themself.

    Examples
    --------
    >>> import pandas as pd
    >>> s = pd.Series("Texthero (is not a superhero!)")
    >>> f(s)
    0    Texthero 
    dtype: object
    """
    return s.str.replace(r"\([^()]*\)", "")


if __name__ == "__main__":
    import doctest
    doctest.testmod()
  1. run doctests through e.g. python3 main.py -v -> they pass
  2. format file with old black version 19 -> they still pass
  3. format file with new black version 20 -> they fail

Expected behavior

Black should not remove the trailing blank in the doctests.

Environment.

  • Version: master
  • OS and Python version: Happens on Windows/Mac/Linux locally and in our Travis-CI builds.

Does this bug also happen on master?

Yes

@ambv
Copy link
Collaborator

ambv commented Aug 29, 2020

Thanks for your report! I agree Black should do better here but bear in mind that your doctests are very brittle as is. If you tell your text editor (or .editorconfig) to automatically remove trailing spaces on save, the test will fail, too. I'd suggest using >>> repr(f(s)) instead of just >>> f(s) in your doctests. This way you're no longer relying on trailing invisible characters.

@henrifroese
Copy link
Author

That makes sense. However, we love using doctests for both documentation and very simple testing. Using repr(f(s)) makes the docstring less intuitive/readable for users, and we'd really like to be able to show users how to use the function while still profiting from the testing.

@dlax
Copy link
Contributor

dlax commented Oct 9, 2020

Another option is the NORMALIZE_WHITESPACE doctest flag. With that, you can remove trailing whitespaces while keeping a doctest pass:

    >>> f(s)  # doctest: +NORMALIZE_WHITESPACE
    0    Texthero
    dtype: object

or

if __name__ == "__main__":
    import doctest
    doctest.testmod(optionflags=doctest.NORMALIZE_WHITESPACE)

@JelleZijlstra JelleZijlstra added the F: strings Related to our handling of strings label May 30, 2021
@felix-hilden felix-hilden added F: docstrings How we format docstrings and removed F: strings Related to our handling of strings labels Aug 29, 2022
@pawamoy
Copy link

pawamoy commented Oct 17, 2022

This prevents using two trailing spaces (line break) in Markdown docstrings 😕

@pawamoy
Copy link

pawamoy commented Oct 17, 2022

Ah, found a more recent issue for this: #3306

@peterjc
Copy link

peterjc commented Mar 7, 2024

Curiously black 24.2.0 does not treat trailing whitespace in all doctests equally, consider this test case:

test_case.py.txt

black 24.2.0 does not alter the module level doctest, just the function doctest.

ruff lint 0.3.1 alters both, reported as astral-sh/ruff#10275

@MichaReiser
Copy link

@peterjc Black doesn't format module docstrings in their stable style (it's a recent preview style addition), whereas Ruff shipped module docstring formatting in the latest stable release.

# Input

"""test
"""

# Black stable
"""test
"""

# Black preview
"""test"""

# Ruff
"""test"""

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

No branches or pull requests

8 participants