add_imports re-adds the import on every run when the file contains an `# isort: off` block (not idempotent)
Los mantenedores suelen responder en 2 días
Evaluación
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Aptitud para principiantes
- 72/100
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: 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
- 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
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de PyCQA/isort
-
separate_packages splits imports from the same explicitly configured packagePosiblemente ocupada @rahul05ranjan la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
Los mantenedores suelen responder en 2 días
-
force_sort_within_sections does not sort lazy imports by module namePosiblemente ocupada @rahul05ranjan la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
Los mantenedores suelen responder en 2 días
-
length_sort with native sort order puts a longer import firstPosiblemente ocupada @simpleqt la tomó hace 24 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
PyCQA/isort#2698 · 2 comentarios ·
Los mantenedores suelen responder en 2 días
-
Cache compiled known-module patterns across Config instancesPosiblemente ocupada @Yin123-ybh la tomó hace 2 días. Abierto
Dificultad 3/5 Medio día Aptitud para principiantes 30/100
PyCQA/isort#2805 · 1 comentario ·
Los mantenedores suelen responder en 2 días
-
multi_line_output=1 with include_trailing_comma leaves a dangling comma on a single importPosiblemente ocupada @beduldul la tomó hace 29 días. Abierto
Dificultad 1/5 1-3 horas Aptitud para principiantes 15/100
PyCQA/isort#2752 · 1 comentario ·
Los mantenedores suelen responder en 2 días
Todos los issues de PyCQA/isort
Issues similares
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
-
Library sweep logs an ERROR at start for the valkey instrumentor when valkey-py is not installedAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
Los mantenedores suelen responder en 1 día
-
bug good first issue
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
JoahanMorales/upiixia-agentic-kit#99 ·
Los mantenedores suelen responder en 1 día
-
bug
Dificultad 1/5 Menos de una hora Aptitud para principiantes 85/100
NatLabRockies/openstudio-mcp#286 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100