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

Abierto
#2,799 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 2 días

@Tanvi-007 ya está trabajando en esto.

Desde el 19/11/2026.

  • #2801 de @Tanvi-007 — abierto

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
72/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python
Área
tooling

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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
Lenguaje dominante
Python
Estrellas
7k
Forks
838
Merge medio
2 d 6 h
PR fusionados (30 d)
19

Preparar el entorno

  • Incluye un Dockerfile o un archivo de Docker Compose
  • Sin plantilla de pull request
  • Sin guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de PyCQA/isort

Todos los issues de PyCQA/isort

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.