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)
optional_arg: str | None = EnvVar("OPTIONAL_ARG", default=None)
config = MyConfig.fromenv()
print(config)
#> MyConfig(timeout_s=2.5, api_key="some_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
-
-
BaseConfig
subtype itselfWith environment variables
COMPONENT_TIMEOUT=5
andOTHER=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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file envarify-1.1.3.tar.gz
.
File metadata
- Download URL: envarify-1.1.3.tar.gz
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83099a91aca3456ade7680d87f12984d4ed62439c5874ea2d6074ee775fe1459 |
|
MD5 | 9e6e2b3df0b1d05920d6ecb077813c28 |
|
BLAKE2b-256 | d296dab8b2fcbdc3116d174fac2af1b6c465fca5f36fa3c4984420cfbdc4a5e1 |
File details
Details for the file envarify-1.1.3-py3-none-any.whl
.
File metadata
- Download URL: envarify-1.1.3-py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fbdd40e0364a5184a2d4c33655d7dd24134658aa423a9ba8780ae7c2610d4d63 |
|
MD5 | 47978ea12f13cc5d64cef48788b62bb5 |
|
BLAKE2b-256 | 1e5696efa410a822ebae226b5c62d26431a48cf12eaa0fc2c877337065ffca70 |