Metadata-Version: 2.1
Name: envarify
Version: 1.2.0
Summary: Environment variables parsing and validation using python type hints
Home-page: https://github.com/vadimtitov/envarify
Author: Vadim Titov
Author-email: titov.hse@gmail.com
Project-URL: Bug Tracker, https://github.com/vadimtitov/envarify/issues
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Requires-Python: >=3.6
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: test
Requires-Dist: pytest; extra == "test"

# Envarify
Environment variables parsing and validation using python type hints


# Usage

Having some environment variables:
```bash
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:
```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:
```python
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:
```python
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.
        ```python
        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='"******"')

        ```


- ### `BaseConfig` subtype itself
    With environment variables ```COMPONENT_TIMEOUT=5``` and ```OTHER=dummy``` you can do:
    ```python
    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')
    ```
