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 required environment variables not set, they will be grouped into one error e.g.:
config = MyConfig.fromenv()
#> MissingEnvVarsError: TIMEOUT_S, API_KEY, ALLOWED_IDS
Supported Types
-
Built-in/standard library types
int
float
bool
str
typing.StrEnum
datetime.date
datetime.datetime
-
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 forhttp
protocol -
HttpsUrl
Same as
Url
but validates forhttps
protocol -
AnyHttpUrl
Same as
Url
but validates for eitherhttp
orhttps
protocol
-
-
Dictionary
dict
/typing.Dict
type reads environmental variable as JSON
-
Sequences (delimiter separated values)
-
list[T]
/typing.List[T]
-
set[T]
/typing.Set[T]
-
tuple[T]
/typing.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')
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)
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.4.0.tar.gz
.
File metadata
- Download URL: envarify-1.4.0.tar.gz
- Upload date:
- Size: 13.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 657de5befe421af4739f341636a3b473415a28db054a1d87c70d1b51a1c0d6e1 |
|
MD5 | c214ed52748dc65c7dd7c568fe00476c |
|
BLAKE2b-256 | fe71c23140ac24a1185176fb2f3f4828f987d4e81aa7d45382e5d9d3505ecebe |
File details
Details for the file envarify-1.4.0-py3-none-any.whl
.
File metadata
- Download URL: envarify-1.4.0-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9208c3eb14855d48333d259f81c6ae880c8ccce6d88cf378af9eb6e8a9deca63 |
|
MD5 | 30fe607cffd4a4c03f1ddfafd59dd89a |
|
BLAKE2b-256 | 6fdec0cc39ed859f404248f13c9357ecf7195477ab89f715c6e55904dbb3c2d1 |