Metadata-Version: 2.1
Name: env-var
Version: 1.0.1
Summary: simple utility to parse and validate environment variables
Home-page: https://github.com/seba-ban/env-var
License: MIT
Keywords: environment,env-var
Author: Sebastian Banaszkiewicz
Author-email: banaszkiewicz.sebastian@gmail.com
Requires-Python: >=3.8,<4.0
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Requires-Dist: isoduration (>=20.11.0,<21.0.0)
Requires-Dist: python-dateutil (>=2.8.2,<3.0.0)
Requires-Dist: rfc3339-validator (>=0.1.4,<0.2.0)
Requires-Dist: rfc3986-validator (>=0.1.1,<0.2.0)
Requires-Dist: validators (>=0.18.2,<0.19.0)
Project-URL: Documentation, https://env-var.readthedocs.io
Project-URL: Repository, https://github.com/seba-ban/env-var
Description-Content-Type: text/markdown

# env_var

A simple utility for working with environment variables. The main goal was to provide proper type check.

## Installation

```bash
pip install env-var
```

## Usage

```python
from env_var import env

minio_port = env('MINIO_PORT').as_port_number().default(9000).required() # port type hint is `int`
minio_host = env('MINIO_HOST').as_hostname().optional() # minio_host type hint is `str | None`
minio_secure = env('MINIO_SECURE').as_bool().required() # minio_host type hint is `bool`
```

Setting `default` will result in always returning a value, so it makes little sense to use it with `optional`.


It might be useful to gather all environment variables that are used in an application to a separate file.

```python
from enum import Enum
from env_var import env

class SomeImportantOption(Enum):
  option_a = "a"
  option_b = "b"
  option_c = "c"

PG_HOST = env('PG_HOST').as_hostname().required()
PG_PORT = env('PG_PORT').as_port_number().default(5432).required()
PG_DB = env('PG_DB').as_string().required()
PG_USER = env('PG_USER').as_string().required()
PG_PASSWORD = env('PG_PASSWORD').as_string().required()

IMPORTANT_OPTION = env('IMPORTANT_OPTION').as_enum(SomeImportantOption).required()
```
Sometimes it might happen that some variables will be required only in specific circumstances, in such cases calling `required` can be postponed until the variable is actually needed.

```python
UPDATE_URL = env('UPDATE_URL').as_url()
"""required only when some condition is met"""

# elsewhere in the code
def send_update(status: str):
  url = UPDATE_URL.required()
  requests.post(url, dict(status=status))
```

It's also possible to pass custom transformers/validators.

```python
@dataclass
class MyOwnClass:
    initial: str

initial_class = env("INITIAL").custom_transformer(MyOwnClass).required() # intial_class is of type MyOwnClass
```

