Skip to content
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

Add Config.env_prefix option #1990

Merged
merged 6 commits into from Jan 22, 2023

Conversation

alex-oleshkevich
Copy link
Member

This PR adds Config.env_prefix argument.

import os
from starlette.config import Config

os.environ['APP_DEBUG'] = 'yes'
os.environ['ENVIRONMENT'] = 'dev'

config = Config(env_prefix='APP_')

DEBUG = config('DEBUG') # lookups APP_DEBUG, returns "yes"
ENVIRONMENT = config('ENVIRONMENT') # lookups APP_ENVIRONMENT, raises KeyError as variable is not defined

Copy link
Sponsor Member

@Kludex Kludex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alex-oleshkevich If you don't mind, would you be able to give a bit more about the motivation here?

This is a pretty straightforward feature, but no one ever asked before.

@alex-oleshkevich
Copy link
Member Author

alex-oleshkevich commented Jan 8, 2023

This is a common practice to avoid naming conflicts.
E.g. there is ENV variable defined by the OS, and the app itself may require ENV like "dev" or "prod". So you can prefix your envvars to separate app's ones and third-party.

This situation pretty common in non-docker environments. Or, it may happen in docker-compose when 2 services require the same variable but with different value.

These are basically two examples I recall so far.

Copy link
Sponsor Member

@Kludex Kludex left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let's go. 🤷

docs/config.md Outdated Show resolved Hide resolved
alex-oleshkevich and others added 2 commits January 22, 2023 18:06
Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
@Kludex
Copy link
Sponsor Member

Kludex commented Jan 22, 2023

Please check if the documentation changes that I suggested work, it may need a plugin. 👀

@alex-oleshkevich
Copy link
Member Author

Please check if the documentation changes that I suggested work, it may need a plugin. eyes

it looks good
image

@alex-oleshkevich alex-oleshkevich merged commit 7822568 into encode:master Jan 22, 2023
@alex-oleshkevich alex-oleshkevich deleted the prefixed-env branch January 22, 2023 21:24
aminalaee pushed a commit that referenced this pull request Feb 13, 2023
* add Config.env_prefix option

* fix variable name in docs

* simplify test case

* rollback markdown formatting

* Update docs/config.md

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Viicos pushed a commit to Viicos/starlette that referenced this pull request Feb 16, 2023
* add Config.env_prefix option

* fix variable name in docs

* simplify test case

* rollback markdown formatting

* Update docs/config.md

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>

Co-authored-by: Marcelo Trylesinski <marcelotryle@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants