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
+47Lines changed: 47 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -311,6 +311,53 @@ If you encounter this error in your own code, you can either cast the `Promise`
311
311
312
312
If this is reported on Django code, please report an issue or open a pull request to fix the type hints.
313
313
314
+
### How to use a custom library to handle Django settings?
315
+
316
+
Using something like [`django-split-settings`](https://github.com/wemake-services/django-split-settings) or [`django-configurations`](https://github.com/jazzband/django-configurations) will make it hard for mypy to infer your settings.
317
+
318
+
This might also be the case when using something like:
319
+
320
+
```python
321
+
try:
322
+
from .local_settings import*
323
+
exceptException:
324
+
pass
325
+
```
326
+
327
+
So, mypy would not like this code:
328
+
329
+
```python
330
+
from django.conf import settings
331
+
332
+
settings.CUSTOM_VALUE# E: 'Settings' object has no attribute 'CUSTOM_SETTING'
333
+
```
334
+
335
+
To handle this corner case we have a special setting `strict_settings` (`True` by default),
336
+
you can switch it to `False` to always return `Any` and not raise any errors if runtime settings module has the given value,
337
+
for example `pyproject.toml`:
338
+
339
+
```toml
340
+
[tool.django-stubs]
341
+
strict_settings = false
342
+
```
343
+
344
+
or `mypy.ini`:
345
+
346
+
```ini
347
+
[mypy.plugins.django-stubs]
348
+
strict_settings = false
349
+
```
350
+
351
+
And then:
352
+
353
+
```python
354
+
# Works:
355
+
reveal_type(settings.EXISTS_IN_RUNTIME) # N: Any
356
+
357
+
# Errors:
358
+
reveal_type(settings.MISSING) # E: 'Settings' object has no attribute 'MISSING'
359
+
```
360
+
314
361
## Related projects
315
362
316
363
-[`awesome-python-typing`](https://github.com/typeddjango/awesome-python-typing) - Awesome list of all typing-related things in Python.
0 commit comments