Skip to content

Feature request: Allow for mixed case keys when configuring from environment variables #94

@arcanerr

Description

@arcanerr

Current Situation

A configuration setup for myproject, that contains uppercase letters like

ABC: "an uppercase value"
abc: "a lowercase value"

currently cannot be configured via environment variables, as the keys encoded into the environment variable names are always converted to lowercase.

Requested Improvement

Allow to configure myproject for above configuration structure via

export myproject_ABC="another uppercase value"
export myproject_abc="another lowercase value"

Implementation

Assuming that the (undocumented) env_prefix parameter of the class Config is rarely used in client code I suggest, that if it is explicitly set to a value, that contains lowercase letters:

from donfig import Config

config = Config('myproject', env_prefix='myproject')

the conversion of found environment variables to lowercase is skipped:

...
if any(char.islower() for char in prefix):
    varname = name[prefix_len:].replace("__", ".")
else:
    varname = name[prefix_len:].lower().replace("__", ".")
...

Analysis

The mentioned approach is not 100% backwards compatible: If any client code uses the following:

from donfig import Config

config = Config('myproject', env_prefix='myProject')

to be able to use mixed case environment Variables

export myProject_ABC="a value"

to read in a configuration equivalent to

abc: "a value"

the change would break this.

Alternative

Add a boolean env_case_sensitive keyword parameter to the class donfig.Config and use it:

class Config:
    def __init__(
        self,
        name: str,
        defaults: list[Mapping[str, Any]] | None = None,
        paths: list[str] | None = None,
        env: Mapping[str, str] | None = None,
        env_var: str | None = None,
        root_env_var: str | None = None,
        env_prefix: str | None = None,
        deprecations: Mapping[str, str | None] | None = None,
        env_case_sensitive: bool = False,
    ):

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions