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

Correctly handle inline tabs in docstrings #1810

merged 2 commits into from
Nov 9, 2020

Conversation

density
Copy link
Contributor

@density density commented Nov 8, 2020

The fix_docstring function expanded all tabs, which caused a
difference in the AST representation when those tabs were inline and not
leading. This changes the function to only expand leading tabs so inline
tabs are preserved.

Fixes #1601.

The `fix_docstring` function expanded all tabs, which caused a
difference in the AST representation when those tabs were inline and not
leading. This changes the function to only expand leading tabs so inline
tabs are preserved.

Fixes #1601.
@density density marked this pull request as ready for review November 8, 2020 02:54
# 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.

@density density requested a review from JelleZijlstra November 9, 2020 14:59
for line in s.splitlines():
# Find the index of the first non-whitespace character after a string of
# whitespace that includes at least one tab
match = re.match(r"\s*\t+\s*(\S)", line)
Copy link
Collaborator

Choose a reason for hiding this comment

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

What if there's something like space + tab + space + tab? It's probably easiest to just grab all leading whitespace and expandtabs() it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup this covers that case and I added tests that cover it. It effectively does what you described, but only for lines where there is at least one tab. Removing the requirement that there be at least one tab isn't a big deal since the string is likely to be short anyway, so I'm open to doing it that way if you prefer!

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh you're right, I forgot that \s also matches tabs.

@JelleZijlstra JelleZijlstra merged commit 1d8b4d7 into psf:master Nov 9, 2020
@density density deleted the fix_inline_tabs_in_docstring branch November 9, 2020 20:56
noxan pushed a commit to noxan/black that referenced this pull request Jun 6, 2021
The `fix_docstring` function expanded all tabs, which caused a
difference in the AST representation when those tabs were inline and not
leading. This changes the function to only expand leading tabs so inline
tabs are preserved.

Fixes psf#1601.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash on docstring with tabs
2 participants