News for hackers. Stuff that matters.
Re:Django 3 by Pandabob on Monday December 02, 2019 @08:29AM (#21682107)
This looks neat. Does anyone have experience with using type hints and mypy with Django? Any gotchas?
Re:Django 3 by silviogutierrez on Monday December 02, 2019 @10:11AM (#21682997)
Painful 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/

the "sample" site is 100% type checked. Look at scripts/test.sh for running mypy in ci (and .github for the CI config)

Re:Django 3 by emptysea on Monday December 02, 2019 @08:45AM (#21682220)
I 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.

Re:Django 3 by mkurnikov on Monday December 02, 2019 @12:27PM (#21684508)
Hi, 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?

Re:Django 3 by vishnu_ks on Monday December 02, 2019 @12:16PM (#21684404)
We (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 community server if you have any further queries :)

1. https://blog.zulip.org/2016/10/13/static-types-in-python-oh-...

2. https://news.ycombinator.com/item?id=12703008

Re:Django 3 by mkurnikov on Monday December 02, 2019 @12:34PM (#21684574)
There's a DEP discussion going on at 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, it's mostly complete. Mypy plugin inside takes care of a lot of stuff.

Disclaimer: I'm a creator of the 'django-stubs'

Re:Django 3 by henbruas on Monday December 02, 2019 @08:43AM (#21682205)
I'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/
Re:Django 3 by stefano on Monday December 02, 2019 @10:54AM (#21683472)
Django (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.
Re:Django 3 by tclancy on Monday December 02, 2019 @11:53AM (#21684173)
Do the stubs for each not work for you?
Re:Django 3 by stefano on Monday December 02, 2019 @12:30PM (#21684543)
When 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.
Re:Django 3 by truebosko on Monday December 02, 2019 @08:42AM (#21682199)
No 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