Skip to content

Add Config.env_prefix option#1990

Merged
alex-oleshkevich merged 6 commits intoKludex:masterfrom
alex-oleshkevich:prefixed-env
Jan 22, 2023
Merged

Add Config.env_prefix option#1990
alex-oleshkevich merged 6 commits intoKludex:masterfrom
alex-oleshkevich:prefixed-env

Conversation

@alex-oleshkevich
Copy link
Copy Markdown
Contributor

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
Copy Markdown
Owner

@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
Copy Markdown
Contributor 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
Copy Markdown
Owner

@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. 🤷

@Kludex
Copy link
Copy Markdown
Owner

Kludex commented Jan 22, 2023

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

@alex-oleshkevich
Copy link
Copy Markdown
Contributor 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 Kludex: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 <[email protected]>

Co-authored-by: Marcelo Trylesinski <[email protected]>
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 <[email protected]>

Co-authored-by: Marcelo Trylesinski <[email protected]>
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.

2 participants