python-dependency-injector
python-dependency-injector copied to clipboard
Type casting in dependency_injector.providers.Configuration.from_env
It would be useful to be able to cast env variables to a specific type, for example:
def setup_config(config: Configuration):
config.foo.from_env("VAR_FOO", cast=int, default=1)
config.bar.from_env("VAR_BAR", cast=bool, default=True)
Inspired by https://www.starlette.io/config/ and https://github.com/sloria/environs
Not a bad idea. At the moment it's done in the different place:
api_client_factory = providers.Factory(
ApiClient,
api_key=config.api.key,
timeout=config.api.timeout.as_int(), # <--- type case here
# timeout=config.api.timeout.as_(Decimal), # <--- or like this
)
It should be more convenient to specify it once while calling .from_env().
Thanks @gtors , will add it to the backlog.
Hm, maybe we name it as_, not cast? Considering speed Python adds new features, I wonder if cast becomes a system keyword one day :) Also, it aligns better with current API (as_*() methods).
def setup_config(config: Configuration):
config.foo.from_env("VAR_FOO", as_=int, default=1)
config.bar.from_env("VAR_BAR", as_=bool, default=True)
What do you think @gtors ?
Looks acceptable 👍🏻