1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
- case: settings_loaded_from_different_files
disable_cache: true
main: |
from typing_extensions import reveal_type
from django.conf import settings
# standard settings
reveal_type(settings.AUTH_USER_MODEL) # N: Revealed type is "builtins.str"
reveal_type(settings.ROOT_DIR) # N: Revealed type is "builtins.str"
reveal_type(settings.APPS_DIR) # N: Revealed type is "pathlib.Path"
reveal_type(settings.NUMBERS) # N: Revealed type is "builtins.list[builtins.str]"
reveal_type(settings.DICT) # N: Revealed type is "builtins.dict[Any, Any]"
custom_settings: |
from base import *
SECRET_KEY = 112233
NUMBERS = ['one', 'two']
DICT = {} # type: ignore[var-annotated]
files:
- path: base.py
content: |
from pathlib import Path
ROOT_DIR = '/etc'
APPS_DIR = Path(ROOT_DIR)
- case: global_settings_are_always_loaded
main: |
from typing_extensions import reveal_type
from django.conf import settings
reveal_type(settings.AUTH_USER_MODEL) # N: Revealed type is "builtins.str"
reveal_type(settings.AUTHENTICATION_BACKENDS) # N: Revealed type is "typing.Sequence[builtins.str]"
installed_apps: []
- case: fail_if_there_is_no_setting
main: |
from django.conf import settings
settings.NOT_EXISTING # E: 'Settings' object has no attribute 'NOT_EXISTING' [misc]
- case: override_default_setting_with_different_type_in_the_different_module
custom_settings: |
from settings.basic_settings import *
main: |
from typing_extensions import reveal_type
from django.conf import settings
reveal_type(settings.MEDIA_ROOT) # N: Revealed type is "pathlib.Path"
reveal_type(settings.MEDIA_ROOT / 'part') # N: Revealed type is "pathlib.Path"
files:
- path: settings/__init__.py
- path: settings/basic_settings.py
content: |
from pathlib import Path
MEDIA_ROOT = Path()
- case: settings_hasattr
main: |
from typing_extensions import reveal_type
from django.conf import settings
if hasattr(settings, 'AUTH_USER_MODEL') and settings.AUTH_USER_MODEL:
reveal_type(settings.AUTH_USER_MODEL)
if hasattr(settings, 'NON_EXISTENT_SETTING') and settings.NON_EXISTENT_SETTING:
reveal_type(settings.NON_EXISTENT_SETTING)
out: |
main:4: note: Revealed type is "builtins.str"
main:5: error: 'Settings' object has no attribute 'NON_EXISTENT_SETTING' [misc]
main:6: error: 'Settings' object has no attribute 'NON_EXISTENT_SETTING' [misc]
main:6: note: Revealed type is "Any"
- case: settings_loaded_from_runtime_magic
disable_cache: true
main: |
from typing_extensions import reveal_type
from django.conf import settings
# Global:
reveal_type(settings.SECRET_KEY) # N: Revealed type is "builtins.str"
# Custom:
reveal_type(settings.A) # N: Revealed type is "Any"
reveal_type(settings.B) # E: 'Settings' object has no attribute 'B' [misc] # N: Revealed type is "Any"
custom_settings: |
# Some code that mypy cannot analyze, but values exist in runtime:
exec('A = 1')
mypy_config: |
[mypy.plugins.django-stubs]
django_settings_module = mysettings
strict_settings = false
- case: settings_loaded_from_runtime_magic_strict_default
disable_cache: true
main: |
from typing_extensions import reveal_type
from django.conf import settings
# Global:
reveal_type(settings.SECRET_KEY) # N: Revealed type is "builtins.str"
# Custom:
reveal_type(settings.A) # E: 'Settings' object has no attribute 'A' [misc] # N: Revealed type is "Any"
reveal_type(settings.B) # E: 'Settings' object has no attribute 'B' [misc] # N: Revealed type is "Any"
custom_settings: |
# Some code that mypy cannot analyze, but values exist in runtime:
exec('A = 1')
mypy_config: |
[mypy.plugins.django-stubs]
django_settings_module = mysettings
|