-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Correctly handle inline tabs in docstrings #1810
Conversation
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.
src/black/__init__.py
Outdated
# and split into a list of lines: | ||
lines = docstring.expandtabs().splitlines() | ||
docstring_expanded = re.sub(r"^\t+", "\t".expandtabs(), docstring, flags=re.M) |
There was a problem hiding this comment.
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()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, duh. Will fix.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.
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.
The
fix_docstring
function expanded all tabs, which caused adifference 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.