-
-
Notifications
You must be signed in to change notification settings - Fork 207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should we raise a KeyError if the env var is missing and default is None? #114
Comments
Hm, I'm not sure - there might be reasons that failing silently is ok. Maybe a warning, or something using the checks framework? What do other folks think? |
@jacobian I agree that maybe a warning should be logged in an instance where parse is not called due to environment variables not being read. I do not think that a KeyError should be raised mainly because this function should fall back onto the default argument value provided. An option could be to add a raises_exception kwarg (defaulted to False) into the function which can be set if the users want an explicit exception to be raised. This option would keep backwards compatibility whilst giving users a more explicit error option. |
I think a warning when DATABASES = dj_database_url.config(default="postgres://local-db-name") This uses a hardcoded fallback locally, but allows I think what we're talking about is this situation, when DATABASES = dj_database_url.config() i.e. no default, and no environ. In that case, I'm leaning towards a warning in this specific situation -- I think something like "warning: env[DATABASE_URL] is blank, and so is default, so you have no databases. you might want to fix this" would be helpful. I do like your idea of a flag to turn this warning into an actual exception. So, if DATABASES = dj_database_url.config() --> warning - something about setting environ or default DATABASES = dj_database_url.config(requires_db=True) ---> exception - something about environ being required Thoughts? |
Completely agree with this. We would only want to raise a warning if the code is called with only default arguments: DATABASES = dj_database_url.config() or if a key miss occurs with no default option set. DATABASES = dj_database_url.config(env='DATABASE_STRING') |
#199 implements this |
Reading this, it does feel "obvious" that if you're calling the config function you really probably do not want an empty dictionary. I would say enough so that raising an exception seems right! A major-version bump change for sure but major versions functionally grow on trees. So the logic is simply I ... I really can't think of a scenario where you want an empty dictionary in the end by default. I do of course don't think we should be warning when using a specified default. |
you might want to generate docs or run CI or something, which involves importing settings.py, from an environment which does not contain runtime environment variables: in our case we build a docker image with |
I think we're converging to where I got to on #199 (comment) so to try and double-check:
Thoughts folks? |
SGTM, a |
I've updated #199 in line with that. |
Shouldn't this function raise a KeyError if s is None?
If the user has not properly configured his environment settings (most likely), and not provided a default fallback value either (also likely), the config will return an empty dictionary, implicitly silencing the issue... (speaking of experience here...)
https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-python
The text was updated successfully, but these errors were encountered: