Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which one is the correct way of using Python type hints with Django Models?

Which one of these is the correct way to use type annotations for django models?

from typing import TypeVar, Generic
from app.models import User

_T = TypeVar("_T", bound=User)

def func(user: Generic[_T]):
    user.name = 'John'
    user.save()

Or

from typing import Type
from app.models import User

def func(user: Type(User)):
    user.name = 'John'
    user.save()

Or

from app.models import User

def func(user: User):
    user.name = 'John'
    user.save()
like image 409
scaryhamid Avatar asked Oct 19 '25 11:10

scaryhamid


1 Answers

If arg should be a MyModel object, then you use:

from app.models import User


def func(arg: User):
    user.name = 'John'
    user.save()

or you can avoid importing the MyModel if you do not need to work with it with:

from typing import TYPE_CHECKING
from __future__ import annotations

if TYPE_CHECKING:
    from app.models import User


def func(arg: 'User'):
    user.name = 'John'
    user.save()

since importing only for type hints can easily result in cyclic import errors.

These will also accept subclasses, so if MyModel is for example an abstract base model, then submodels are also valid for the type hints.

Using Generic itself makes not much sense: this is used to define a specific type hint itself, for example a Dict. Furthermore Type(MyModel) means that you would pass the type itself, which is a subclass of MyModel, this thus means that you pass a reference to the MyModel class for example, not a MyModel object.

like image 138
Willem Van Onsem Avatar answered Oct 21 '25 00:10

Willem Van Onsem



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!