add_imports re-adds the import on every run when the file contains an `# isort: off` block (not idempotent)
Maintainers usually reply within 2 days
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 72/100
Research direction
Start by tracing how --add-import and the add_imports setting interact with # isort: off / # isort: on blocks; the issue gives a reproducible m.py example but names no source or test files. Run the reproduction, then check that repeated runs insert the import only once and --check-only exits 0 after the first insertion.
Written by the indexing model from the issue text.
Description
Description
When using isort --add-import on a Python file containing an # isort: off / # isort: on block, repeatedly executing the same command inserts duplicate copies of the specified import.
This behaviour does not occur when the file does not contain an isort: off block.
Steps to reproduce
python3 -m venv venv && source venv/bin/activate
pip install isort
printf '# isort: off\nimport b\nimport a\n# isort: on\n' > m.py
isort --add-import "import foo" m.py # run 1
isort --add-import "import foo" m.py # run 2
isort --add-import "import foo" m.py # run 3
cat m.py
isort --check-only --add-import "import foo" m.py; echo "exit code: $?"
The same happens with add_imports = ["import foo"] under [tool.isort]
in pyproject.toml and a plain isort m.py.
Expected behaviour
The import should be added only once. Subsequent executions should leave the file unchanged. Running --check-only with the same --add-import argument should exit with code 0 after the first successful insertion.
Actual behaviour
Each run adds another import foo line (isort prints "Fixing" every time):
# isort: off
import b
import foo
import foo
import foo
import a
# isort: on
--check-only exits 1 every time:
ERROR: m.py Imports are incorrectly sorted and/or formatted.
Control
- Without the
# isort: off/# isort: onlines (justimport band
import a),import foois added once, later runs change nothing, and
--check-onlyexits 0. # isort: splitand# isort: skip_filedo not trigger the problem.- Workaround: putting
# isort: dont-add-importsat the top of the file
stops it.
Impact
Files are silently modified on every run. With add_imports set in a
pre-commit hook or CI, affected files can never pass --check-only, and
duplicate imports accumulate.
Environment
- isort 9.0.2 (PyPI wheel) and 10.0.0b4.dev1 (
main, installed from git): both reproduce - Python 3.14.4
- Ubuntu 26.04.1 LTS on WSL2 (Linux 6.18.33.2-microsoft-standard-WSL2, x86_64)
- pip 25.1.1
- Dominant language
- Python
- Stars
- 7k
- Forks
- 838
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 19
Getting set up
- Ships a Dockerfile or Docker Compose file
- No pull request template
- No contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from PyCQA/isort
-
separate_packages splits imports from the same explicitly configured packagePossibly taken @rahul05ranjan claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 2 days
-
force_sort_within_sections does not sort lazy imports by module namePossibly taken @rahul05ranjan claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 2 days
-
length_sort with native sort order puts a longer import firstPossibly taken @simpleqt claimed this 24 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
PyCQA/isort#2698 · 2 comments ·
Maintainers usually reply within 2 days
-
Cache compiled known-module patterns across Config instancesPossibly taken @Yin123-ybh claimed this 2 days ago. Open
Difficulty 3/5 Half a day Newbie friendliness 30/100
PyCQA/isort#2805 · 1 comment ·
Maintainers usually reply within 2 days
-
multi_line_output=1 with include_trailing_comma leaves a dangling comma on a single importPossibly taken @beduldul claimed this 29 days ago. Open
Difficulty 1/5 1-3 hours Newbie friendliness 15/100
PyCQA/isort#2752 · 1 comment ·
Maintainers usually reply within 2 days
Similar issues
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 85/100
NatLabRockies/openstudio-mcp#286 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
intersystems/ipm#1338 ·
Maintainers usually reply within 1 day
-
The v0-through-v1 path derives ShutdownDuration its own way, silently overriding a user's definitionOpenv1
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
TigreGotico/text2spoken#626 ·
Maintainers usually reply within 1 day