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

Commits on Mar 22, 2021

  1. 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.
    jonafato committed Mar 22, 2021
    Copy the full SHA
    2ccb497 View commit details
    Browse the repository at this point in the history