I’m trying to enable type hints for my Django REST project. I installed django-stubs and djangorestframework-stubs and I have the following mypy.ini file:
Some of the type hints do work; for example, if I have a variable inside of a method whose type is a model class, I get hints when I try to access a field or method defined on it.
However, if I try to access a Django-specific field on the model, for example a reverse relation, that does not typecheck and gives me an error. Moreover, if I try to access fields on a related model of my model variable, the related object has type Any
For example, with these two models:
class User(models.Model):
name = models.TextField()
role = models.ForeignKey(Role) # definition of Role model not relevant
class Badge(models.Model):
user = models.ForeignKey(User, related_name="badges")
then this will happen:
u: User = get_user()
print(u.name) # correctly hinted and typechecks
print(u.badges.all()) # type error
print(u.role.pk) # no hint on role.pk, role is Any
How do I get my project to correclty type check all the Django-specific features such as foreign key fields, querysets, and reverse relationships?
Skipping analyzing “django.core.management”: module is installed, but missing library stubs or py.typed marker [import-untyped]
Skipping analyzing “django.core.wsgi”: module is installed, but missing library stubs or py.typed marker [import-untyped]
Skipping analyzing “django.urls”: module is installed, but missing library stubs or py.typed marker [import-untyped]
Skipping analyzing “django.conf”: module is installed, but missing library stubs or py.typed marker [import-untyped]
Skipping analyzing “django.conf.urls.static”: module is installed, but missing library stubs or py.typed marker
Skipping analyzing “django.core.asgi”: module is installed, but missing library stubs or py.typed marker [import-untyped]
I think you may have a misunderstanding about how the Django project is organized.
There is no “at Django”. Django is not a company or a product from a company. It is a completely community-driven project. The features that people choose to work on are what get added. It’s not directed by any one individual or group.
You have misinterpreted what I have said. Clearly I have acknowledged that there is interest by identifying the project that is working on it.
What I said was that people who are interested in it need to step up and work on it, because that’s the only way it’s going to get added.
It doesn’t help to sit back and say “Someone needs to work on this”. I’m pointing out that in Django, you are one of the “Someone”. I’m sure the django-stubs project would greatly appreciate your contributions.
Because a “Someone” thought it would be worthwhile and made the decision to work on it. Again, that’s how progress is made. Those individuals didn’t just write posts saying “Someone should implement this.” They’ve gone ahead and done it.
<opinion>
(Personally, I don’t care if it ever gets added. I can see value to adding it to Django core, but I don’t have any desire to implement it in any code that I write.) </opinion>