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

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

config = MyConfig.fromenv()
print(config)
#> MyConfig(timeout_s=2.5, api_key="some_key", allowed_ids={1,2,3}, enable_feature=True)

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 unit 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

    • list[T]
    • set[T]
    • tuple[T]

    where T is any primitive type

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.1.0.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

envarify-1.1.0-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: envarify-1.1.0.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for envarify-1.1.0.tar.gz
Algorithm Hash digest
SHA256 20c265a4fca17f1471d9ffaa8d1ebdf13be4c262db6ce50171f335b66be10298
MD5 f69f3d6b3e7a34cfa4be857ef90a910f
BLAKE2b-256 1c4d6c5c39746828a2d5c29ce1525fb9ec59bbd7e8bdda6c88235469d2389ff7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: envarify-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 7.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for envarify-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 27c7a5ac6fa06715a320ac0fcfa5166be98693bf12fed371ac45998397478683
MD5 326a49ec4a82862428325b8f2f119608
BLAKE2b-256 a485e77b0d58586951fe7cdfc6a82c9472af72f75fa0cf6846e5801818e203ba

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