Fix bug regarding multiline docstrings
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.
Loading
Please register or sign in to comment