add_imports re-adds the import on every run when the file contains an `# isort: off` block (not idempotent)

Open
#2,799 2 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 2 days

@Tanvi-007 is already working on this.

Since Nov 19, 2026.

  • #2801 by @Tanvi-007 — open

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
72/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
tooling

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: on lines (just import b and
    import a), import foo is added once, later runs change nothing, and
    --check-only exits 0.
  • # isort: split and # isort: skip_file do not trigger the problem.
  • Workaround: putting # isort: dont-add-imports at 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

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from PyCQA/isort

All issues in PyCQA/isort

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.