Skip to main content

Environment variables parsing and validation using python type hints

Project description

Envarify

Environment variables parsing and validation using python type hints

Usage

Having some environment variables:

export TIMEOUT_S=2.5
export API_KEY=some_key
export ALLOWED_IDS=1,2,3
export ENABLE_FEATURE=true

We can create a config object in Python:

from envarify import BaseConfig, EnvVar, SecretString

class MyConfig(BaseConfig):
    timeout_s: float = EnvVar("TIMEOUT_S")
    api_key: SecretString = EnvVar("API_KEY")
    allowed_ids: set[int] = EnvVar("ALLOWED_IDS")
    enable_feature: bool = EnvVar("ENABLE_FEATURE", default=False)
    optional_arg: str | None = EnvVar("OPTIONAL_ARG", default=None)

config = MyConfig.fromenv()
print(config)
#> MyConfig(timeout_s=2.5, api_key='******', allowed_ids={1,2,3}, enable_feature=True, optional_arg=None)

Missing environment variables

If there are some required environment variables that are not set, error will be raised:

config = MyConfig.fromenv()
#> MissingEnvVarsError: TIMEOUT_S, API_KEY, ALLOWED_IDS

Testing

In tests for your application you don't have to worry about mocking the environment variables. Instead just create a mock config object:

mock_config = MyConfig(timeout_s=4.2, api_key="dummy", allowed_ids={1,2,3}, enable_feature=True)

Supported Types

  • Primitive types

    • int
    • float
    • bool
    • str
  • Dictionary

    dict type reads environmental variable as JSON

  • Sequences (delimiter separated values)

    • list[T]

    • set[T]

    • tuple[T]

      where T is any primitive type

  • Special types

    • SecretString

      Masks sensitive environment variables by displaying ****** when printed or logged. The actual value is accessible via the reveal() method, and memory is cleared when object is no longer needed.

      from envarify import BaseConfig, EnvVar, SecretString
      
      class MyConfig(BaseConfig):
          api_key: SecretString = EnvVar("API_KEY")
      
      config = MyConfig.fromenv()
      print(config.api_key)
      
      #> MyConfig(api_key='******')
      
    • Url

      Validates that string is a URL.

      from envarify import BaseConfig, EnvVar, Url
      
      class MyConfig(BaseConfig):
          url: Url = EnvVar("ws://example.com")
      
      config = MyConfig.fromenv()
      print(config.api_key)
      
      #> MyConfig(url='ws://example.com')
      
    • HttpUrl

      Same as Url but validates for http protocol

    • HttpsUrl

      Same as Url but validates for https protocol

    • AnyHttpUrl

      Same as Url but validates for either http or https protocol

  • BaseConfig subtype itself

    With environment variables COMPONENT_TIMEOUT=5 and OTHER=dummy you can do:

    from envarify import BaseConfig, EnvVar
    
    class ComponentConfig(BaseConfig):
        timeout: int = EnvVar("COMPONENT_TIMEOUT")
    
    class ApplicationConfig(BaseConfig):
        component: ComponentConfig
        other: str = EnvVar("OTHER")
    
    config = ApplicationConfig.fromenv()
    print(config)
    #> ApplicationConfig(component=ComponentConfig(timeout=5), other='dummy')
    

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

envarify-1.3.0.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

envarify-1.3.0-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file envarify-1.3.0.tar.gz.

File metadata

  • Download URL: envarify-1.3.0.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for envarify-1.3.0.tar.gz
Algorithm Hash digest
SHA256 5ff951fd2adaa26226abba51860e4339626d51123dee372e763d3e3c83e21daa
MD5 edef88c10c3b55815333837e556c75d8
BLAKE2b-256 40e888c92ee62f2e9e6660d9d37f54a5c454c96f66ebd1c8caa3dee09794d416

See more details on using hashes here.

File details

Details for the file envarify-1.3.0-py3-none-any.whl.

File metadata

  • Download URL: envarify-1.3.0-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for envarify-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4d2215d7e10110278b62e8a89adbd3a3f90f6ad3fd59d246106ddde6de86efd2
MD5 be063135691953af49c6aaf42a4c4c86
BLAKE2b-256 afd43d95b2ef855d5f0a236ce5a106e91dddebbd448fc9c02f65c6c6cc0269e7

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page