Skip to main content

load-environ-typed

This library lets you declare a NamedTuple or dataclass with field types, and gives you a function that will load the values from the environment, validating your types for you.

Getting started

from load_environ_typed import load

from dataclasses import dataclass

@dataclass
class MyEnviron:
	db_host: str
	db_port: int

environ = load(MyEnviron)

FAQ

What types can I use? What about custom types?

By default, any type that takes a single string in it's constructor can be used as type. Think of int, str, float, pathlib.Path, etc.

We've added default loaders for the types below:

  • bool - "true" or "false", case insenstive
  • datetime.date - Using datetime.date.fromisoformat
  • datetime.time - Using datetime.time.fromisoformat
  • datetime.datetime - Using datetime.datetime.fromisoformat

How are fields matched to enviroment variables?

The loader assumes the names are the same, except that the class fields are lowercase, and the environment fields are uppercase. If you have different or more complicated rules, you can pass a name conversion function via field_name_to_var_name.

@dataclass
class MyEnviron:
    iso_date: datetime.date

environ = sut.load(MyEnviron, environ={
    'ISO_DATE': '2021-01-01',
})

Can values be optional?

Certainly:

@dataclass
class MyEnviron:
	DB_HOST: Optional[str]

However:

  • empty string and "none" (case insensitive) count as None
  • defaults take precedence over optionality

What if my type cannot take a string in its constructor?

You can pass so-called loader functions. These take in a string, and are expected to return a value of the given type, or raise a ValueError when the given string is not valid for the given type. This is also the mechanism that we use to support standard Python types such datetime.date, which is shown below.

@dataclass
class MyEnviron:
    ISO_DATE: datetime.date

environ = sut.load(MyEnviron, environ={
    'ISO_DATE': '2021-01-01',
}, loaders={
    'ISO_DATE': datetime.date.fromisoformat,
    # or, if you want ALL `date`s to use this loader:
    datetime.date: datetime.date.fromisoformat,
})

NOTE: date has a default loader, so you don't need to do this for date.

Can I use Unions?

You can, but you need to use a default loader, as you need some way to distinguish between the types, and there is no general way to do so, at least not without enforcing our way of working on you.

How do I work with default values?

If you want default values, it's probably best to have use a dataclass with kw_only=True, as otherwise you have to order your variables based on whether there's a default or not.

@dataclass(kw_only=True)
class MyEnviron:
	DB_HOST: str
	DB_PORT: int = 3306

For most types, you can simply set the default value as you're used to with dataclasses. However, you may not want to instantiate an (expensive) property as default. In those cases, you can pass defaults along using the defaults argument.

@dataclass
class MyEnviron:
	VAR: SomeExpensiveClass

environ = load(MyEnviron, defaults={
	'VAR': '#!serialized.data!#',
})

NOTE: kw_only requires Python3.10 or higher. Below 3.10, you can use the defaults argument or order your variables. Similarly, if you plan on solely using the defaults argument, you don't need kw_only.

Load returns an instance. What if I want a global?

Since environ is available at startup, and doesn't change, it's perfectly valid to just instantiate a global variable. You should probably use a frozen dataclass for this.

@dataclass(frozen=True)
class MyEnviron:
	DB_HOST: str
	DB_PORT: int

ENVIRON = load(MyEnviron)

What if there's an issue with the default loaders?

First, the loaders you pass will be taken before using the default loaders.

Second, if you have more structural issues with the default loaders, simply pass use_default_loaders=False.

Contributing

Check out this repo, and then use the following steps to test it:

python3 -m venv venv
venv/bin/pip install -r requirements-dev.txt
make test

Deploying

First, update pyproject.toml with the new version number, and commit that.

Then:

rm -f dist/* # Clean old build files
venv/bin/python -m build
venv/bin/python -m twine upload dist/*

Release files for load-environ-typed 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for load-environ-typed 0.3.1
File Size Uploaded
load_environ_typed-0.3.1.tar.gz 12.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for load-environ-typed 0.3.1
File Interpreter ABI Platform
load_environ_typed-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 21.8 kB

Release files / load_environ_typed-0.3.1.tar.gz

Download URL load_environ_typed-0.3.1.tar.gz
Size 12.6 kB
Tags Source
SHA-256 checksum
How to use checksums
f907e3e72ad39e99d1148baac60be73c439a9ce72b9ea8fd7fcba49b30d0e920
BLAKE2b-256 checksum
How to use checksums
485ad2f6cbf6d38de80890ccb1db46ff2050022bb291e55cff9132d71c98a990
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / load_environ_typed-0.3.1-py3-none-any.whl

Download URL load_environ_typed-0.3.1-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b1d61f93f79e47959df1feb93ffb85df977e97d7f74430321ae635ef7b421ad3
BLAKE2b-256 checksum
How to use checksums
15ac67b5fd50f2e5509d87a6a12cadaacd2fcdbb44158a5a813db081fb84ffed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.3.1 This release

2 release files

0.3.0

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page