# mypy and type checking

**URL:** <https://forum.djangoproject.com/t/mypy-and-type-checking/15787>\
**Category:** Getting Started\
**Created:** [September 2, 2022, 12:35pm UTC](https://forum.djangoproject.com/t/mypy-and-type-checking/15787 "2022-09-02T12:35:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![samul-1](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/samul-1/32/4767_2.png) [@samul-1](https://forum.djangoproject.com/u/samul-1)\
**Post date:** [September 2, 2022, 12:35pm UTC](https://forum.djangoproject.com/t/mypy-and-type-checking/15787/1 "2022-09-02T12:35:29Z")

</div>

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:

```auto
[mypy]
plugins =
    mypy_django_plugin.main
    mypy_drf_plugin.main

[mypy.plugins.django-stubs]
django_settings_module = "core.settings.base"

```

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:

```auto
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:

```auto
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?

---

<div class="post-metadata">

**Author:** ![developer992](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/developer992/32/20194_2.png) [@developer992](https://forum.djangoproject.com/u/developer992)\
**Post date:** [March 27, 2024, 12:37pm UTC](https://forum.djangoproject.com/t/mypy-and-type-checking/15787/2 "2024-03-27T12:37:59Z")

</div>

There’s a million threads about this and no solution?

Nobody at DJANGO cares enough to support MYPY the industry standard for python?

Amazing …

---

<div class="post-metadata">

**Author:** ![developer992](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/developer992/32/20194_2.png) [@developer992](https://forum.djangoproject.com/u/developer992)\
**Post date:** [March 27, 2024, 12:40pm UTC](https://forum.djangoproject.com/t/mypy-and-type-checking/15787/4 "2024-03-27T12:40:01Z")

</div>

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]

and many, _many_ more.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 27, 2024, 1:48pm UTC](https://forum.djangoproject.com/t/mypy-and-type-checking/15787/5 "2024-03-27T13:48:58Z")

</div>

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.

Additionally, there has been some work in this direction - see [GitHub - typeddjango/django-stubs: PEP-484 stubs for Django](https://github.com/typeddjango/django-stubs)

In other words, if it is so important to _you_ that Mypy be supported, then I encourage _you_ to work on it! **That** is how Django moves forward.

---

<div class="post-metadata">

**Author:** ![developer992](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/developer992/32/20194_2.png) [@developer992](https://forum.djangoproject.com/u/developer992)\
**Post date:** [March 27, 2024, 2:01pm UTC](https://forum.djangoproject.com/t/mypy-and-type-checking/15787/6 "2024-03-27T14:01:11Z")

</div>

yes, this is only important _to me_ lmaoooo

why does mypy exist then huh?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 27, 2024, 2:28pm UTC](https://forum.djangoproject.com/t/mypy-and-type-checking/15787/7 "2024-03-27T14:28:15Z")

</div>

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.

> [@developer992](#):
>
> why does mypy exist then huh?

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>`

---

<div class="post-metadata">

**Author:** ![bllendev](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/bllendev/32/24699_2.png) [@bllendev](https://forum.djangoproject.com/u/bllendev)\
**Post date:** [May 20, 2025, 1:07am UTC](https://forum.djangoproject.com/t/mypy-and-type-checking/15787/8 "2025-05-20T01:07:42Z")

</div>

Ken i think you are making complete sense and spoke well, I feel encouraged to look into this further, thanks for pointing in the right direction!
