Typed environment variable loading via class declaration — zero boilerplate
Project description
envhint
Typed environment variable loading via class declaration. Requires only strcoerce.
The problem
# Scattered across every project:
PORT = int(os.environ.get("PORT", "8080"))
DEBUG = bool(os.environ.get("DEBUG", "false")) # bug — always True
HOSTS = os.environ.get("ALLOWED_HOSTS", "").split(",")
TIMEOUT = timedelta(minutes=int(os.environ.get("TIMEOUT_MINUTES", "5")))
Install
pip install envhint
Usage
from envhint import Env
from datetime import timedelta
class Settings(Env):
port: int = 8080
debug: bool = False
db_url: str # required — raises EnvError if absent
allowed_hosts: list[str] = []
request_timeout: timedelta = timedelta(seconds=30)
settings = Settings()
print(settings.port) # 8080 (int, not "8080")
print(settings.debug) # False (not True like bool("false"))
print(settings.allowed_hosts) # ["api.example.com", "admin.example.com"]
Set via environment:
PORT=9000
DEBUG=false
DB_URL=postgresql://localhost/mydb
ALLOWED_HOSTS=api.example.com,admin.example.com
REQUEST_TIMEOUT=2m30s
All errors reported at once
class Settings(Env):
db_url: str
secret_key: str
api_key: str
Settings(_env={})
# EnvError: Environment configuration errors:
# DB_URL: required but not set
# SECRET_KEY: required but not set
# API_KEY: required but not set
Testing without patching os.environ
def test_my_feature():
settings = Settings(_env={
"DB_URL": "postgresql://localhost/test",
"SECRET_KEY": "test-secret",
})
assert settings.port == 8080 # default
.env file support
settings = Settings(_env_file=".env")
# Real env vars take priority over .env file values
Supported types
| Annotation | Parsed from |
|---|---|
str |
passthrough |
int |
"42" → 42 |
float |
"3.14" → 3.14 |
bool |
"true/false/yes/no/1/0/on/off" |
list[str] |
"a,b,c" → ["a", "b", "c"] |
timedelta |
"5m", "2h30m", "1d" |
Optional[X] |
absent → None, present → coerced as X |
License
MIT
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file envhint-0.1.0.tar.gz.
File metadata
- Download URL: envhint-0.1.0.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f59f9f87981a0b5d966ab41f8dfbd54ba33d8ae01433139a0fad7667db5041ac
|
|
| MD5 |
ccf14d2a5a972ce6b21bc79bd752d6c3
|
|
| BLAKE2b-256 |
2e737c419b831b07114e2eee971cf6b0a8caa5fd65206645128d73eaaf9a6eb9
|
Provenance
The following attestation bundles were made for envhint-0.1.0.tar.gz:
Publisher:
publish.yml on SpinnakerSix/envhint
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
envhint-0.1.0.tar.gz -
Subject digest:
f59f9f87981a0b5d966ab41f8dfbd54ba33d8ae01433139a0fad7667db5041ac - Sigstore transparency entry: 1763429198
- Sigstore integration time:
-
Permalink:
SpinnakerSix/envhint@17681d65856e70f24ae112a579c79d8c459d4e2e -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/SpinnakerSix
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@17681d65856e70f24ae112a579c79d8c459d4e2e -
Trigger Event:
push
-
Statement type:
File details
Details for the file envhint-0.1.0-py3-none-any.whl.
File metadata
- Download URL: envhint-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
388db55eb976c740c34d081236b38b85e5f4a16f2311ac459fe10f5e89a38165
|
|
| MD5 |
d54c4c4781a6461f003979ccb41cc399
|
|
| BLAKE2b-256 |
c708f9aaedc709111f87c0d7ff7b6f67c7f1f02cdec2268b1972785d58b1ebf1
|
Provenance
The following attestation bundles were made for envhint-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on SpinnakerSix/envhint
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
envhint-0.1.0-py3-none-any.whl -
Subject digest:
388db55eb976c740c34d081236b38b85e5f4a16f2311ac459fe10f5e89a38165 - Sigstore transparency entry: 1763429522
- Sigstore integration time:
-
Permalink:
SpinnakerSix/envhint@17681d65856e70f24ae112a579c79d8c459d4e2e -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/SpinnakerSix
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@17681d65856e70f24ae112a579c79d8c459d4e2e -
Trigger Event:
push
-
Statement type: