mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-08 13:04:47 +08:00
move configuration to [mypy.plugins.django-stubs] inside mypy config file
This commit is contained in:
@@ -3,26 +3,24 @@ from pytest_mypy.item import YamlTestItem
|
||||
|
||||
|
||||
def django_plugin_hook(test_item: YamlTestItem) -> None:
|
||||
settings = {
|
||||
'SECRET_KEY': '"1"',
|
||||
}
|
||||
additional_settings = test_item.parsed_test_data.get('additional_settings')
|
||||
if additional_settings:
|
||||
for item in additional_settings:
|
||||
name, _, val = item.partition('=')
|
||||
settings[name] = val
|
||||
|
||||
custom_settings = test_item.parsed_test_data.get('custom_settings', '')
|
||||
installed_apps = test_item.parsed_test_data.get('installed_apps', None)
|
||||
|
||||
if installed_apps and custom_settings:
|
||||
raise ValueError('"installed_apps" and "custom_settings" are not compatible, please use one or the other')
|
||||
|
||||
if installed_apps is not None:
|
||||
# custom_settings is empty, add INSTALLED_APPS
|
||||
installed_apps += ['django.contrib.contenttypes']
|
||||
installed_apps_as_str = '(' + ','.join([repr(app) for app in installed_apps]) + ',)'
|
||||
custom_settings += 'INSTALLED_APPS = ' + installed_apps_as_str
|
||||
|
||||
pyproject_toml_file = File(path='pyproject.toml',
|
||||
content='[tool.django-stubs]\ndjango_settings_module=\'mysettings\'')
|
||||
test_item.files.append(pyproject_toml_file)
|
||||
if 'SECRET_KEY' not in custom_settings:
|
||||
custom_settings = 'SECRET_KEY = "1"\n' + custom_settings
|
||||
|
||||
settings_contents = f'INSTALLED_APPS={installed_apps_as_str}\n'
|
||||
settings_contents += '\n'.join([f'{key}={val}' for key, val in settings.items()])
|
||||
if not test_item.additional_mypy_config:
|
||||
test_item.additional_mypy_config = "[mypy.plugins.django-stubs]\n" \
|
||||
"django_settings_module = mysettings"
|
||||
|
||||
mysettings_file = File(path='mysettings.py', content=settings_contents)
|
||||
test_item.files.append(mysettings_file)
|
||||
mysettings_file = File(path='mysettings.py', content=custom_settings)
|
||||
test_item.files.append(mysettings_file)
|
||||
|
||||
Reference in New Issue
Block a user