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

Conversation

jonafato
Copy link
Contributor

If a multiline docstring has its closing quotes at the end of a line
instead of on a separate line, isort fails to properly add imports using
the add_imports config option; instead, it adds the desired imports
into the middle of the docstring as illustrated below. While PEP 257
(and other guides) advises that closing quotes appear on their own line,
isort should not fail here.

This change adds a check for closing docstrings at the end of a line in
addition to the existing line start check for all comment indicators. A
new section of the test_add_imports test explicitly tests multiline
imports and this failure scenario specifically.


A working example:

"""My module.

Provides example functionality.
"""

print("hello, world")

Running isort --add-import "from __future__ import annotations"
produces the following as expected:

"""My module.

Provides example functionality.
"""

from __future__ import annotations

print("hello, world")

The failure behavior described:

"""My module.

Provides example functionality."""

print("hello, world")

Running isort --add-import "from __future__ import annotations" as
above produces the following result:

"""My module.

from __future__ import annotations

Provides example functionality."""

print("hello, world")

Subsequent executions add more import lines into the docstring. This
behavior occurs even if the file already has the desired imports.

If a multiline docstring has its closing quotes at the end of a line
instead of on a separate line, isort fails to properly add imports using
the `add_imports` config option; instead, it adds the desired imports
into the middle of the docstring as illustrated below. While PEP 257
(and other guides) advises that closing quotes appear on their own line,
`isort` should not fail here.

This change adds a check for closing docstrings at the end of a line in
addition to the existing line start check for all comment indicators. A
new section of the `test_add_imports` test explicitly tests multiline
imports and this failure scenario specifically.

---

A working example:

```python
"""My module.

Provides example functionality.
"""

print("hello, world")
```

Running `isort --add-import "from __future__ import annotations"`
produces the following as expected:

```python
"""My module.

Provides example functionality.
"""

from __future__ import annotations

print("hello, world")
```

---

The failure behavior described:

```python
"""My module.

Provides example functionality."""

print("hello, world")
```

Running `isort --add-import "from __future__ import annotations"` as
above produces the following result:

```python
"""My module.

from __future__ import annotations

Provides example functionality."""

print("hello, world")
```

Subsequent executions add more import lines into the docstring. This
behavior occurs even if the file already has the desired imports.
@jonafato jonafato force-pushed the fix-multiline-docstring-ending-on-line-add_import branch from c03a35d to 2ccb497 Compare March 22, 2021 17:55
@timothycrosley
Copy link
Member

Thanks for finding and fixing this issue!

@timothycrosley timothycrosley merged commit f5e0447 into PyCQA:develop Mar 23, 2021
@jonafato jonafato deleted the fix-multiline-docstring-ending-on-line-add_import branch March 23, 2021 12:03
This was referenced Jun 21, 2021
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.

2 participants