-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-indent the contents of docstrings (#1053)
* Re-indent the contents of docstrings when indentation changes Keeping the contents of docstrings completely unchanged when re-indenting (from 2-space intents to 4, for example) can cause incorrect docstring indentation: ``` class MyClass: """Multiline class docstring """ def method(self): """Multiline method docstring """ pass ``` ...becomes: ``` class MyClass: """Multiline class docstring """ def method(self): """Multiline method docstring """ pass ``` This uses the PEP 257 algorithm for determining docstring indentation, and adjusts the contents of docstrings to match their new indentation after `black` is applied. A small normalization is necessary to `assert_equivalent` because the trees are technically no longer precisely equivalent -- some constant strings have changed. When comparing two ASTs, whitespace after newlines within constant strings is thus folded into a single space. Co-authored-by: Luka Zakrajšek <[email protected]> * Extract the inner `_v` method to decrease complexity This reduces the cyclomatic complexity to a level that makes flake8 happy. * Blacken blib2to3's docstring which had an over-indent Co-authored-by: Luka Zakrajšek <[email protected]> Co-authored-by: Zsolt Dollenstein <[email protected]>
- Loading branch information
1 parent
9938c92
commit a4c11a7
Showing
4 changed files
with
251 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
class MyClass: | ||
"""Multiline | ||
class docstring | ||
""" | ||
|
||
def method(self): | ||
"""Multiline | ||
method docstring | ||
""" | ||
pass | ||
|
||
|
||
def foo(): | ||
"""This is a docstring with | ||
some lines of text here | ||
""" | ||
return | ||
|
||
|
||
def bar(): | ||
'''This is another docstring | ||
with more lines of text | ||
''' | ||
return | ||
|
||
|
||
def baz(): | ||
'''"This" is a string with some | ||
embedded "quotes"''' | ||
return | ||
|
||
|
||
def troz(): | ||
'''Indentation with tabs | ||
is just as OK | ||
''' | ||
return | ||
|
||
|
||
def zort(): | ||
"""Another | ||
multiline | ||
docstring | ||
""" | ||
pass | ||
|
||
def poit(): | ||
""" | ||
Lorem ipsum dolor sit amet. | ||
Consectetur adipiscing elit: | ||
- sed do eiusmod tempor incididunt ut labore | ||
- dolore magna aliqua | ||
- enim ad minim veniam | ||
- quis nostrud exercitation ullamco laboris nisi | ||
- aliquip ex ea commodo consequat | ||
""" | ||
pass | ||
|
||
|
||
def over_indent(): | ||
""" | ||
This has a shallow indent | ||
- But some lines are deeper | ||
- And the closing quote is too deep | ||
""" | ||
pass | ||
|
||
# output | ||
|
||
class MyClass: | ||
"""Multiline | ||
class docstring | ||
""" | ||
|
||
def method(self): | ||
"""Multiline | ||
method docstring | ||
""" | ||
pass | ||
|
||
|
||
def foo(): | ||
"""This is a docstring with | ||
some lines of text here | ||
""" | ||
return | ||
|
||
|
||
def bar(): | ||
"""This is another docstring | ||
with more lines of text | ||
""" | ||
return | ||
|
||
|
||
def baz(): | ||
'''"This" is a string with some | ||
embedded "quotes"''' | ||
return | ||
|
||
|
||
def troz(): | ||
"""Indentation with tabs | ||
is just as OK | ||
""" | ||
return | ||
|
||
|
||
def zort(): | ||
"""Another | ||
multiline | ||
docstring | ||
""" | ||
pass | ||
|
||
|
||
def poit(): | ||
""" | ||
Lorem ipsum dolor sit amet. | ||
Consectetur adipiscing elit: | ||
- sed do eiusmod tempor incididunt ut labore | ||
- dolore magna aliqua | ||
- enim ad minim veniam | ||
- quis nostrud exercitation ullamco laboris nisi | ||
- aliquip ex ea commodo consequat | ||
""" | ||
pass | ||
|
||
|
||
def over_indent(): | ||
""" | ||
This has a shallow indent | ||
- But some lines are deeper | ||
- And the closing quote is too deep | ||
""" | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters