You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+23-18Lines changed: 23 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,28 +8,26 @@
8
8
9
9
This package contains type stubs and mypy plugin to provide more precise static types and type inference for Django framework. Django uses some Python "magic" that makes having precise types for some code patterns problematic. This is why we need to accompany the stubs with mypy plugins. The final goal is to be able to get precise types for most common patterns.
10
10
11
-
Supports Python 3.6/3.7, and Django 2.1/2.2.
12
-
13
11
Could be run on earlier versions of Django, but expect some missing imports warnings.
14
12
13
+
15
14
## Installation
16
15
17
16
```bash
18
17
pip install django-stubs
19
18
```
20
19
21
-
## Mypy compatibility
22
20
23
-
| django-stubs | mypy version | django version |
24
-
| ------------ | ---- | ---- |
25
-
| 1.2.0 | 0.730 | 2.2.x
26
-
| 1.1.0 | 0.720 | 2.2.x
27
-
| 0.12.x | old semantic analyzer (<0.711), dmypy support | 2.1.x
21
+
## Mypy compatibility
28
22
23
+
| django-stubs | mypy version | django version | python version
24
+
| ------------ | ---- | ---- | ---- |
25
+
| 1.2.0 | 0.730 | 2.2.x | ^3.6
26
+
| 1.1.0 | 0.720 | 2.2.x | ^3.6
27
+
| 0.12.x | old semantic analyzer (<0.711), dmypy support | 2.1.x | ^3.6
29
28
30
-
### WARNING: All configuration from pre-1.0.0 versions is dropped, use one below.
31
29
32
-
### WARNING: 1.0.0 breaks `dmypy`, if you need it, stay on the 0.12.x series.
30
+
##Configuration
33
31
34
32
To make mypy aware of the plugin, you need to add
35
33
@@ -39,37 +37,43 @@ plugins =
39
37
mypy_django_plugin.main
40
38
```
41
39
42
-
in your `mypy.ini` file.
40
+
in your `mypy.ini`or `setup.cfg`[file](https://mypy.readthedocs.io/en/latest/config_file.html).
43
41
44
-
Plugin requires Django settings module (what you put into `DJANGO_SETTINGS_MODULE` variable) to be specified inside `mypy.ini` file.
42
+
Plugin also requires Django settings module (what you put into `DJANGO_SETTINGS_MODULE` variable) to be specified.
45
43
46
44
```ini
47
45
[mypy]
48
46
strict_optional = True
49
47
50
-
; this one is new
48
+
# This one is new:
51
49
[mypy.plugins.django-stubs]
52
50
django_settings_module = mysettings
53
51
```
54
52
55
-
where`mysettings` is a value of `DJANGO_SETTINGS_MODULE` (with or without quotes)
53
+
Where`mysettings` is a value of `DJANGO_SETTINGS_MODULE` (with or without quotes)
56
54
57
-
Do you have trouble with mypy / the django plugin not finding your settings module? Try adding the root path of your project to your PYTHONPATH environment variable. If you use pipenv you can add the following to an `.env` file in your project root which pipenv will run automatically before executing any commands.:
58
-
```
55
+
You might also need to explicitly tweak your `PYTHONPATH` the very same way `django` does it internally in case you have troubles with mypy / django plugin not finding your settings module. Try adding the root path of your project to your `PYTHONPATH` environment variable like so:
56
+
57
+
```bash
59
58
PYTHONPATH=${PYTHONPATH}:${PWD}
60
59
```
61
60
62
-
New implementation uses Django runtime to extract models information, so it will crash, if your installed apps `models.py` is not correct. For this same reason, you cannot use `reveal_type` inside global scope of any Python file that will be executed for `django.setup()`.
61
+
Current implementation uses Django runtime to extract models information, so it will crash, if your installed apps `models.py` is not correct. For this same reason, you cannot use `reveal_type` inside global scope of any Python file that will be executed for `django.setup()`.
63
62
64
63
In other words, if your `manage.py runserver` crashes, mypy will crash too.
65
64
65
+
This fully working [typed boilerplate](https://github.com/wemake-services/wemake-django-template) can serve you as an example.
66
+
67
+
66
68
## Notes
67
69
68
-
Implementation monkey-patches Django to add `__class_getitem__` to the `Manager` class. If you'd use Python3.7 and do that too in your code, you can make things like
70
+
Type implementation monkey-patches Django to add `__class_getitem__` to the `Manager` class.
71
+
If you would use Python3.7 and do that too in your code, you can make things like
69
72
70
73
```python
71
74
classMyUserManager(models.Manager['MyUser']):
72
75
pass
76
+
73
77
classMyUser(models.Model):
74
78
objects = UserManager()
75
79
```
@@ -78,6 +82,7 @@ work, which should make a error messages a bit better.
78
82
79
83
Otherwise, custom type will be created in mypy, named `MyUser__MyUserManager`, which will rewrite base manager as `models.Manager[User]` to make methods like `get_queryset()` and others return properly typed `QuerySet`.
80
84
85
+
81
86
## To get help
82
87
83
88
We have Gitter here: <https://gitter.im/mypy-django/Lobby>
0 commit comments