Django Migrations Do I need type hints and how? [closed]

Python now has type hinting (or typehint? is it one word or two?). Some docs can be seen at peps.python.org.

The issue is that typehinting is made for devs to understand the program usage and avoid errors but what happens if it's code made for automation? i.e. Django migrations where the code isn't run by another dev/user but by another software?

Django creates its own migrations via manage.py makemigrations and this is the code that comes out:

class Migration(migrations.Migration):

    dependencies = [
        ('app', '0029_auto_20220601_1515'),
    ]

    operations = [
        migrations.AlterField(
            model_name='jobexecution',
            name='job_type',
            field=models.CharField(choices=[('foo', 'Foo'),('bar', 'Bar)], default='foo', max_length=30),
        ),
    ]

On the other hand when I want to do data migrations I create something like this:

def forwards(apps, schema_editor):
    Foo = apps.get_model('app', 'Foo')
    FixedResponse.objects.create(name='Bar')


def backwards(apps, schema_editor):
    Foo = apps.get_model('app', 'Foo')
    Foo.objects.filter(name='Bar').delete()


class Migration(migrations.Migration):

    dependencies = [
        ('app', '0035_fixedresponse'),
    ]

    operations = [
        migrations.RunPython(forwards, backwards)
    ]

Should I type hint both files? How do I typehint the latter (the data migration)?

Back to Top