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

Correctly handle inline tabs in docstrings #1810

Merged
merged 2 commits into from
Nov 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/black/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6748,9 +6748,10 @@ def fix_docstring(docstring: str, prefix: str) -> str:
# https://www.python.org/dev/peps/pep-0257/#handling-docstring-indentation
if not docstring:
return ""
# Convert tabs to spaces (following the normal Python rules)
# Convert leading tabs to spaces (following the normal Python rules)
# and split into a list of lines:
lines = docstring.expandtabs().splitlines()
docstring_expanded = re.sub(r"^\t+", "\t".expandtabs(), docstring, flags=re.M)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are multiple tabs at the beginning, shouldn't they be replaced by multiple instances of "\t".expandtabs()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, duh. Will fix.

lines = docstring_expanded.splitlines()
# Determine minimum indentation (first line doesn't count):
indent = sys.maxsize
for line in lines[1:]:
Expand Down
18 changes: 17 additions & 1 deletion tests/data/docstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ def ignored_docstring():
"""a => \
b"""


def docstring_with_inline_tabs():
"""hey

tab separated value
tab at a start of line and then a tab separated value
"""

# output

class MyClass:
Expand Down Expand Up @@ -222,4 +230,12 @@ def believe_it_or_not_this_is_in_the_py_stdlib():

def ignored_docstring():
"""a => \
b"""
b"""


def docstring_with_inline_tabs():
"""hey

tab separated value
tab at a start of line and then a tab separated value
"""