Opened 4 years ago

Closed 4 years ago

#24514 closed Cleanup/optimization (fixed)

Unused import on some auto generated migrations

Reported by: Hugo Tácito Owned by: Chris Luc
Component: Migrations Version: 1.7
Severity: Normal Keywords: unused import migrations
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Some auto generated migrations don't use django.db.models. Especially those that only change the Meta class. It would be nice to change the django.db.migrations.writer.MIGRATION_TEMPLATE to only add the from django.db import models if its really needed.

Example of auto generated migration with unused import:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

    dependencies = [
        ('city', '0003_auto_20150305_0928'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='city',
            options={'verbose_name': 'City', 'verbose_name_plural': 'Cities'},
        ),
    ]

Change History (7)

comment:1 Changed 4 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

comment:2 Changed 4 years ago by Chris Luc

Owner: changed from nobody to Chris Luc
Status: newassigned

comment:3 Changed 4 years ago by Markus Holtermann

I think models is a convenience import and should stay around.

comment:4 Changed 4 years ago by Chris Luc

I think having models in a migration that doesn't use the import might spark confusion for why it's being imported in the first place. I think it makes the migrations cleaner by not having unused imports. I don't see many situations where someone would manually need the models import. But, in the rare occasion that they do need it, manually adding it is not that bad. @MarkusH

comment:5 Changed 4 years ago by Hugo Tácito

Personally I'm having problems when I do automated code analysis with pylint. I know this can be solved just ignoring the migrations folder but it would be nice if django only do this import when necessary on auto generated migrations.

comment:6 Changed 4 years ago by Chris Luc

Has patch: set

comment:7 Changed 4 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In a7bc00e1:

Fixed #24514 -- Made migration writer omit models import if it's unused.

Note: See TracTickets for help on using tickets.
Back to Top