DEV Community

Abdillah Issa
Abdillah Issa

Posted on

frozenenv: typed environment variables as a frozen dataclass, zero dependencies Meta

Tired of writing this in everty project:

PORT = int(os.environ.get("PORT", "8000"))
DEBUG = os.environ.get("DEBUG", "false").lower() == "true"
Enter fullscreen mode Exit fullscreen mode

frozenenv fixes that with one decorator:

   from envclass import envclass

   @envclass
   class Config:
      DATABASE_URL: str
      PORT: int = 8000
      DEBUG: bool = False
      ALLOWED_HOSTS: list[str] = []

 cfg = Config()
 print(cfg.PORT) # 8000 - actual int, not "8000"
Enter fullscreen mode Exit fullscreen mode

Features:

  • Zero dependencies: pure Python stdlib
  • Type coercion: str, int, float, bool, list[X], Optional[X]
  • Frozen - Config is immutable after creation
  • Built-in .env support: no python-dotenv needed
  • Fails at startup: not buried in runtime

install: pip install frozenenv

GitHub: github.com/hudihi/frozenenv
Full writeup: there-a-cleaner-way-to-handle-environment-variables-and-it-takes-one-decorator

Top comments (0)