isort icon indicating copy to clipboard operation
isort copied to clipboard

add_import adds imports above module docstring when docstring is raw string

Open Skylion007 opened this issue 3 years ago • 1 comments

  • I tried to use the add_import functionality like so: add_imports = "from __future__ import annotations"

However, it kept destroying my module level docstrings by adding an import before the """ string. In the project I was working on, we use raw multiline stringsr""" which may be messing with the heuristics to detect module level docstrings here: https://github.com/PyCQA/isort/blob/c6a41965247a858a0afd848fbebfca18b8983917/isort/parse.py#L218

As a temporary workaround, adding the append_only flag has seemed to fix it.

In short, isort will format my code like the following:

from __future__ import annotations
r"""module docstring
"""
from __future__ import annotations

Here is my isort config:

add_imports = "from __future__ import annotations"
# append_only = true # Enabling this is the workaround
profile = 'black'
treat_comments_as_code = "# %%"

Skylion007 avatar Feb 03 '22 18:02 Skylion007

Just ran into this very exact problem today. Unfortunately we run isort through gray which does not support passing the append_only config.

Our workaround was to add # isort: dont-add-imports in this file since it already has the import we need.

sodul avatar May 31 '24 19:05 sodul