typed-environ

Load environment variables with types


License
MIT
Install
pip install typed-environ==1.0.1

Documentation

typed-environ

Build Status Coverage Status

Environment variables with type support.

Usage

from typed_environ import environ
  • int value without default

    port = int(environ['PORT'])
  • int value with default

    port = environ.get('PORT', type=int, default=8000)
  • bool value without default

    debug = bool(environ['DEBUG'])

    Accepted values.

    Note: The return value of environ['DEBUG'] is not simply a str, since bool cast for a str only checks if the string is empty. Similarly, casting the result into list or dict (see below) works differently from casting on a string. The return value can still be used as a string normally (excluding bool and iter calls). To avoid complications on str values, explicitly call str(environ['xxx']), or use os.environ instead.

  • bool value with default

    debug = environ.get('DEBUG', type=bool, default=False)
  • list

    allowed_hosts = environ.get('ALLOWED_HOSTS', type=list)

    Split the string by ','.

    Note 1: If the value is an empty string, returns [] instead of [''].

    Note 2: default here is None. It does not have to be a value of type.

  • json

    data_list = list(environ['DATA_LIST'])
    data = dict(environ['DATA'])

    Load the environment variable from a json-formatted string, which can be a list or a dict.

    Note: When calling iter (and list, dict, tuple, set, etc. by extension), json-decoding will be attempted first, and then comma-splitting as specified above.

Installation

pip install typed-environ