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

Fix bug regarding multiline docstrings #1695

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion isort/core.py
Expand Up @@ -13,7 +13,8 @@

CIMPORT_IDENTIFIERS = ("cimport ", "cimport*", "from.cimport")
IMPORT_START_IDENTIFIERS = ("from ", "from.import", "import ", "import*") + CIMPORT_IDENTIFIERS
COMMENT_INDICATORS = ('"""', "'''", "'", '"', "#")
DOCSTRING_INDICATORS = ('"""', "'''")
COMMENT_INDICATORS = DOCSTRING_INDICATORS + ("'", '"', "#")
CODE_SORT_COMMENTS = (
"# isort: list",
"# isort: dict",
Expand Down Expand Up @@ -317,6 +318,7 @@ def process(
and not in_quote
and not import_section
and not line.lstrip().startswith(COMMENT_INDICATORS)
and not line.rstrip().endswith(DOCSTRING_INDICATORS)
):
import_section = line_separator.join(add_imports) + line_separator
if end_of_file and index != 0:
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/test_isort.py
Expand Up @@ -873,6 +873,41 @@ def test_add_imports() -> None:
" pass\n"
)

# On a file that has no pre-existing imports and a multiline docstring
test_input = (
'"""Module docstring\n\nWith a second line\n"""\n' "class MyClass(object):\n pass\n"
)
test_output = isort.code(code=test_input, add_imports=["from __future__ import print_function"])
assert test_output == (
'"""Module docstring\n'
"\n"
"With a second line\n"
'"""\n'
"from __future__ import print_function\n"
"\n"
"\n"
"class MyClass(object):\n"
" pass\n"
)

# On a file that has no pre-existing imports and a multiline docstring.
# In this example, the closing quotes for the docstring are on the final
# line rather than a separate one.
test_input = (
'"""Module docstring\n\nWith a second line"""\n' "class MyClass(object):\n pass\n"
)
test_output = isort.code(code=test_input, add_imports=["from __future__ import print_function"])
assert test_output == (
'"""Module docstring\n'
"\n"
'With a second line"""\n'
"from __future__ import print_function\n"
"\n"
"\n"
"class MyClass(object):\n"
" pass\n"
)

# On a file that has no pre-existing imports, and no doc-string
test_input = "class MyClass(object):\n pass\n"
test_output = isort.code(code=test_input, add_imports=["from __future__ import print_function"])
Expand Down