Skip to content

Comments

swev-id: django__django-16662 - group MigrationWriter imports by type#523

Open
casey-brooks wants to merge 1 commit intodjango__django-16662from
noa/issue-518
Open

swev-id: django__django-16662 - group MigrationWriter imports by type#523
casey-brooks wants to merge 1 commit intodjango__django-16662from
noa/issue-518

Conversation

@casey-brooks
Copy link

Fixes #518.

Reproduction and observed failure (on django__django-16662)

  1. Check out django__django-16662.
  2. Run:
    PYTHONPATH=$PWD:$HOME/.nix-profile/lib/python3.11/site-packages DJANGO_SETTINGS_MODULE=tests.test_sqlite python3 - <<'PY'
    import datetime
    import time
    
    from django.db import migrations, models
    from django.db.migrations.writer import MigrationWriter
    
    migration = type(
        "Migration",
        (migrations.Migration,),
        {
            "operations": [
                migrations.CreateModel(
                    "TimedModel",
                    [
                        ("id", models.AutoField(primary_key=True)),
                        ("created_at", models.DateTimeField(default=datetime.datetime.now)),
                        ("duration", models.FloatField(default=time.time)),
                    ],
                    {},
                    (models.Model,),
                )
            ]
        },
    )
    writer = MigrationWriter(migration)
    print(writer.as_string().split("\n\n", 2)[1])
    PY
  3. The generated import block is:
    import datetime
    from django.db import migrations, models
    import time
    
  4. Save the snippet above into tmp_imports.py and run:
    PYTHONPATH=$HOME/.local/lib/python3.11/site-packages:$HOME/.nix-profile/lib/python3.11/site-packages python3 -m isort --check-only --diff tmp_imports.py
    Output before this change:
    ERROR: /workspace/django/tmp_imports.py Imports are incorrectly sorted and/or formatted.
    --- /workspace/django/tmp_imports.py:before	2025-12-24 17:33:25.490772
    +++ /workspace/django/tmp_imports.py:after	2025-12-24 17:33:28.788401
    @@ -1,4 +1,4 @@
     import datetime
    -from django.db import migrations, models
     import time
    
    +from django.db import migrations, models
    

Summary

  • group migration imports into future, plain, and from blocks so plain imports precede from-imports
  • retain the combined from django.db import migrations, models special-case handling
  • add WriterTests.test_imports_grouped_plain_before_from covering the new ordering

Testing

  • PYTHONPATH=$PWD:$HOME/.nix-profile/lib/python3.11/site-packages DJANGO_SETTINGS_MODULE=tests.test_sqlite python3 tests/runtests.py migrations.test_writer --parallel=1 --verbosity=1
  • PYTHONPATH=$PWD:$HOME/.nix-profile/lib/python3.11/site-packages flake8 django/db/migrations/writer.py tests/migrations/test_writer.py

@casey-brooks casey-brooks requested a review from a team December 24, 2025 17:34
@casey-brooks
Copy link
Author

Local Verification

  • PYTHONPATH=$PWD:$HOME/.nix-profile/lib/python3.11/site-packages DJANGO_SETTINGS_MODULE=tests.test_sqlite python3 tests/runtests.py migrations.test_writer --parallel=1 --verbosity=1 → 53 passed, 0 failed, 0 skipped
  • PYTHONPATH=$PWD:$HOME/.nix-profile/lib/python3.11/site-packages flake8 django/db/migrations/writer.py tests/migrations/test_writer.py → lint clean (no issues)

Copy link

@noa-lucent noa-lucent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes look good. The new import grouping logic matches the requested ordering and the added test covers the regression scenario.

@rowan-stein rowan-stein changed the base branch from django__django-16662 to main January 11, 2026 20:11
@rowan-stein rowan-stein changed the base branch from main to django__django-16662 January 12, 2026 20:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix migration import ordering to match isort and Django style

2 participants