wemake-services/django-split-settings

Django-split-settings and settings type inference

sondrelg opened this issue ยท 3 comments

Hi!

First of all, thanks for creating this @sobolevn ๐ŸŽ‰

I've just implemented django-split-settings and have run into one small issue: it messes up type inference of django.conf.settings a little bit.

Just to be clear: I'm more than happy to accept that this is a trade-off I make when using the package. I just wanted to ask if you knew of a way to resolve either of these. If you don't, no problem ๐Ÿ‘

First issue: Pycharm (semi-resolved)

After splitting settings, Pycharm fails to understand the type of any attribute loaded only with include. This makes sense, since these attributes are not discovered when loading the module. To resolve this I've just done this:

# myproj.settings.__init__.py

from myproj.settings.settings import *  # this contains my django-split-settings code

if TYPE_CHECKING:
    from myproj.settings.components.auth import *  
    from myproj.settings.components.celery import * 
    from myproj.settings.components.credentials import * 
    from myproj.settings.components.database import * 
    from myproj.settings.components.i18n import * 
    from myproj.settings.components.logging import * 
    from myproj.settings.components.sentry import * 
    from myproj.settings.components.staticfiles import *

After this, Pycharm is back to normal.

Second issue: Mypy (unresolved)

In my project I have a bunch of properties like this:

@property
def attribute_name_setting(self) -> dict[str, str]:
    return settings.SOME_SETTING['attribute-name']

After implementing split-settings, mypy gets angry, and I honestly don't know exactly how to interpret the errors:

image


Do you have thoughts about the way we resolve the first issue, or any idea about how to resolve the second? If not, I'll just ignore them ๐Ÿ‘

Hi!

Thanks a lot for the warm feedback ๐Ÿ˜Š

I just wanted to ask if you knew of a way to resolve either of these

No, I don't know any specifics. But, I still want to share my thoughts on this.

First issue: Pycharm (semi-resolved)

Great hack! I will keep it in mind.

Second issue: Mypy (unresolved)

Do you use django-stubs? Or just plain mypy?
We think about adding settings provider support to djangp-stubs, but no work has been done yet.

Yes this is with django-stubs (and drf-stubs) ๐Ÿ™‚ if it's helpful I could try and create a demo project to replicate the issue

I was able to resolve my second issue by adding annotations to the setting I was returning.

In one of my components I had

from decouple import config  # python-decouple

SOME_SETTING = {
    'demo': {
        'API_KEY': config('API_KEY'),
        ...
    },
}

When I added correct annotations, all 40 errors resolved

SOME_SETTING: dict[str, dict[str, str]] = {
    'demo': {
        'API_KEY': config('API_KEY'),
        ...
    },
}

In other words, this might not even have anything to do with splitting the settings ๐Ÿ™‚ I wasn't able to reproduce easily, so think this might have just been a random error that just happened to surface when I was implementing split-settings ๐Ÿคท

Thanks ๐Ÿ‘