Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

envhint-0.1.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

envhint-0.1.0-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

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

Hashes for envhint-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f59f9f87981a0b5d966ab41f8dfbd54ba33d8ae01433139a0fad7667db5041ac
MD5 ccf14d2a5a972ce6b21bc79bd752d6c3
BLAKE2b-256 2e737c419b831b07114e2eee971cf6b0a8caa5fd65206645128d73eaaf9a6eb9

See more details on using hashes here.

Provenance

The following attestation bundles were made for envhint-0.1.0.tar.gz:

Publisher: publish.yml on SpinnakerSix/envhint

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for envhint-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 388db55eb976c740c34d081236b38b85e5f4a16f2311ac459fe10f5e89a38165
MD5 d54c4c4781a6461f003979ccb41cc399
BLAKE2b-256 c708f9aaedc709111f87c0d7ff7b6f67c7f1f02cdec2268b1972785d58b1ebf1

See more details on using hashes here.

Provenance

The following attestation bundles were made for envhint-0.1.0-py3-none-any.whl:

Publisher: publish.yml on SpinnakerSix/envhint

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page