29 ms·
This looks neat. Does anyone have experience with using type hints and mypy with Django? Any gotchas?
by Pandabob 7y ago
This looks neat. Does anyone have experience with using type hints and mypy with Django? Any gotchas?
- truebosko 7y agoNo specific gotchas with type hinting, but be aware that Django has not implemented any notion of type hints. So, you lack quite the benefit of type hinting
- henbruas 7y agoI'm currently experimenting with it, so I don't have a lot to say yet. But you'll want to use this: https://pypi.org/project/django-stubs/ https://pypi.org/project/django-stubs/
- emptysea 7y agoI gave ‘django-stubs’ a spin on a Django 2.2 project. The ORM typings and mypy plug-in worked great. ‘.filter().first()’ and similar all returned the correct types. Ran into a couple issues with the typing of ‘client.force_login()’ where the ‘user’ parameter was typed as the ‘get_django_user()’ which wasn’t the same as the custom user model I defined in the ‘models.py’. I also had issues with ‘from django.conf import settings’ not being the correct type so I’d suggest changing that to ‘Any’. You might be able to override the few types that don’t work for your given setup or you can just fork the repo. Overall, I think the pain is worth the benefits.
- mkurnikov 7y agoHi, I'm a primary maintainer of 'django-stubs'. We're going to release new version with mypy==0.750 and Django 3.0 support soon. Could you create an issue for your 'client.force_login' problem? > I also had issues with ‘from django.conf import settings’ not being the correct type so I’d suggest changing that to ‘Any’. This one also has plugin support, and should be working mostly correct. Fill the issue if you can reproduce, I'd love to help with that. What's your experience with that whole "plugin has to actually run django.setup() before typecheck" thing?
- silviogutierrez 7y agoPainful at first, but well worth it. You can ignore all existing code and only use it going forward. django-stubs is black magic and helps a lot. Here's an example setup that I use for my library: https://github.com/silviogutierrez/reactivated/ https://github.com/silviogutierrez/reactivated/ the "sample" site is 100% type checked. Look at scripts/test.sh for running mypy in ci (and .github for the CI config)
- stefano 7y agoDjango (and DRF if you use it), uses too much reflection, which doesn't play nice with type annotations. The strategy that works for me is to annotate my application code, and keep using dynamic typing when interacting with the framework.
- tclancy 7y agoDo the stubs for each not work for you?
- stefano 7y agoWhen I tried them a year ago, they used "Any" a lot, especially around the ORM code (Django) and the serializers (DRF). Since those are also the parts where type checking would be the most interesting, I didn't find a lot of value in using them.
- vishnu_ks 7y agoWe (Zulip) added type hints to most of our Python Django codebase a while back. The blog post[1] below has a lot of details on this. Also included a link to the hacker news discussion of the blog post[2]. Feel free to come and ask in https://chat.zulip.org https://chat.zulip.org community server if you have any further queries :) 1. https://blog.zulip.org/2016/10/13/static-types-in-python-oh-mypy/ https://blog.zulip.org/2016/10/13/static-types-in-python-oh-... 2. https://news.ycombinator.com/item?id=12703008 https://news.ycombinator.com/item?id=12703008
- mkurnikov 7y agoThere's a DEP discussion going on at https://github.com/django/deps/pull/65 https://github.com/django/deps/pull/65. Though I kind of gave up on it for now, I don't believe it'll be accepted in the current state of mypy and typing ecosystem. Try 'django-stubs' mentioned above too https://github.com/typeddjango/django-stubs https://github.com/typeddjango/django-stubs, it's mostly complete. Mypy plugin inside takes care of a lot of stuff. Disclaimer: I'm a creator of the 'django-stubs'