django-compressor / django-appconf

An app to handle configuration defaults of packaged Django apps gracefully
https://django-appconf.readthedocs.io
BSD 3-Clause "New" or "Revised" License
350 stars 47 forks source link

Private settings #98

Open paduszyk opened 2 weeks ago

paduszyk commented 2 weeks ago

Hi 👋🏻

I'd to share an idea for private settings, a new feature for the django-appconf.

Consider the following example:

class MyAppConf(AppConf):
    PUBLIC = ...

    _PRIVATE = ...

    class Meta:
        prefix = "my_app"

In the current implementation, PUBLIC and _PRIVATE are accessible fromdjango.conf.settings via MY_APP_PUBLIC, whereas MY_APP__PRIVATE. My idea is to make _PRIVATE inaccessible as it is considered to be private.

I see two ways we could implement this.

  1. Treat names prefixed with an underscore as private (as in the example above; this is Python's way...). However, the issue with this approach is that some linters may complain that other parts of the codebase use private attributes (e.g. Ruff, the rule SLF001).
  2. Add an extra attribute to Meta (private, let us say) that could be a list of names for private settings.
class MyAppConf(AppConf):
    PUBLIC = ...

    PRIVATE = ...

    class Meta:
        prefix = "my_app"
        private = ["private"]  # uppercase is implicit

This could be useful (and elegant/Pythonic IMHO) for adding configurations specific to the configured app. What's your opinion?

If you find this idea interesting, I can open a PR.

carltongibson commented 2 weeks ago

Hi @paduszyk. Django settings doesn't really have any concept of private settings. AppConf already scopes settings to a particular app, but I'm not sure going further than that is really in scope here.

paduszyk commented 2 weeks ago

@carltongibson OK 👍🏻 Maybe the name "private settings" is misleading... By private I meant "seen" only by the AppConf instance. You can access them from myapp.conf.settings, but not from django.conf.settings.

carltongibson commented 2 weeks ago

But why not just regular attributes in that case?

I'm not really seeing how the need here ties in with AppConf's role as providing scoped Django settings, which are globals essentially.