<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Abdillahissa on Medium]]></title>
        <description><![CDATA[Stories by Abdillahissa on Medium]]></description>
        <link>https://medium.com/@abdillahissa?source=rss-0bfe0915331e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*T5JqXCjWYIuruIQA</url>
            <title>Stories by Abdillahissa on Medium</title>
            <link>https://medium.com/@abdillahissa?source=rss-0bfe0915331e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 07:37:11 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@abdillahissa/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Stop Writing This Python Anti-Pattern in Every Project You Build]]></title>
            <link>https://medium.com/@abdillahissa/theres-a-cleaner-way-to-handle-environment-variables-and-it-takes-one-decorator-69f0ef3997eb?source=rss-0bfe0915331e------2</link>
            <guid isPermaLink="false">https://medium.com/p/69f0ef3997eb</guid>
            <dc:creator><![CDATA[Abdillahissa]]></dc:creator>
            <pubDate>Fri, 15 May 2026 09:50:21 GMT</pubDate>
            <atom:updated>2026-05-15T09:53:51.155Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>There’s a cleaner way to handle environment variables and it takes one decorator</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XfLLkR1ySXEBcEir" /><figcaption>Photo by <a href="https://unsplash.com/@cdr6934?utm_source=medium&amp;utm_medium=referral">Chris Ried</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>You’ve written this code before. I know you have.</p><pre>import os<br><br>DATABASE_URL = os.environ.get(&quot;DATABASE_URL&quot;)<br>PORT = int(os.environ.get(&quot;PORT&quot;, &quot;8000&quot;))<br>DEBUG = os.environ.get(&quot;DEBUG&quot;, &quot;false&quot;).lower() == &quot;true&quot;<br>ALLOWED_HOSTS = os.environ.get(&quot;ALLOWED_HOSTS&quot;, &quot;&quot;).split(&quot;,&quot;)</pre><p>It works. Barely. And every time you start a new project, you write it again slightly differently, slightly more fragile than the last time.</p><p>This is one of those patterns that every Python developer inherits and nobody questions. Until it bites them.</p><h3><strong>The three moments this breaks you</strong></h3><p><strong>Moment one: the 2am production incident.</strong></p><p>Your app crashes. The error is buried six call-stacks deep. After 45 minutes of digging, you find it: someone deployed without setting REDIS_URL. The variable was silently None. Nobody caught it because the app started fine it only crashed when that specific code path ran.</p><p><strong>Moment two: the new teammate.</strong></p><p>They ask: “What environment variables does this app need to run?” You say: “Grep the codebase.” That’s not documentation. That’s archaeology.</p><p><strong>Moment three: the silent type bug.</strong></p><p>You do if port &gt; 1024: and get a TypeError. Because PORT is &quot;8000&quot;a string not 8000. You forgot to cast it. The cast is three files away. The bug is in production.</p><h3>What actually needs to happen</h3><p>Your config system needs to do four things:</p><ol><li><strong>List every required variable in one place; </strong>a contract, not a scavenger hunt</li><li><strong>Cast types automatically</strong>;&quot;8000&quot; becomes 8000, &quot;true&quot; becomes True</li><li><strong>Fail at startup, loudly; </strong>not silently, not later, not in production</li><li><strong>Be immutable; </strong>config set once, never changed mid-run</li></ol><p>The standard answer is pydantic-settings. It’s excellent. But it pulls in four heavy dependencies for what is fundamentally a simple problem. And python-dotenv does only half the job, it loads the file but does zero type casting.</p><p>What if you need something lighter?</p><h3>Introducing frozenenv</h3><p>I built frozenenv to solve exactly this. One decorator. Zero dependencies. Pure Python stdlib.</p><pre>pip install frozenenv</pre><p>Here is the entire config from the top of this article, rewritten:</p><pre>from envclass import envclass<br><br><br>@envclass<br>class Config:<br>    DATABASE_URL: str                      # required - EnvError if missing<br>    PORT: int = 8000                       # auto-cast to int<br>    DEBUG: bool = False                    # &quot;true&quot; / &quot;1&quot; / &quot;yes&quot; → True<br>    ALLOWED_HOSTS: list[str] = []          # &quot;a.com,b.com&quot; → [&quot;a.com&quot;, &quot;b.com&quot;]<br>cfg = Config()<br>print(cfg.PORT)                            # 8000  ← actual int<br>print(cfg.DEBUG)                           # False ← actual bool<br>print(cfg.ALLOWED_HOSTS)                   # [&quot;a.com&quot;, &quot;b.com&quot;]</pre><p>Your class is the contract. Every variable, every type, every default visible in one place.</p><h3>The part most env libraries get wrong: mutability</h3><p>Every other env library gives you a config object you can accidentally modify mid-run.</p><pre># With most libraries — this silently works<br>config.PORT = 9999   # whoops<br>config.DEBUG = True  # now your production app is in debug mode</pre><p>frozenenv makes config frozen the moment it&#39;s created:</p><pre>cfg = Config()<br>cfg.PORT = 9999   # ❌ FrozenInstanceError — immediately, loudly</pre><p>Config is not state. It should be set once at startup and never touched again. frozenenv enforces that.</p><h3>Type coercion that actually works</h3><h3>Booleans</h3><p>This is the one everyone gets wrong. How many ways have you seen booleans parsed from env vars?</p><pre># The chaotic ways teams handle booleans<br>os.environ.get(&quot;DEBUG&quot;) == &quot;true&quot;          # misses &quot;True&quot;, &quot;1&quot;, &quot;yes&quot;<br>os.environ.get(&quot;DEBUG&quot;, &quot;0&quot;) == &quot;1&quot;        # misses &quot;true&quot;, &quot;yes&quot;<br>bool(os.environ.get(&quot;DEBUG&quot;))              # always True if var exists at all!</pre><p>frozenenv accepts all the reasonable values:</p><p>Truthy true, True, 1, yes, on, enabled</p><p>Falsyfalse, False, 0, no, off, disabled</p><p>Anything else raises an EnvError immediately with a clear message.</p><h3>Lists</h3><pre># .env file<br>ALLOWED_HOSTS=localhost,127.0.0.1,myapp.com<br>ALLOWED_PORTS=80,443,8080</pre><pre>@envclass<br>class Config:<br>    ALLOWED_HOSTS: list[str] = []<br>    ALLOWED_PORTS: list[int] = []<br><br>cfg = Config()<br>cfg.ALLOWED_HOSTS                  # [&quot;localhost&quot;, &quot;127.0.0.1&quot;, &quot;myapp.com&quot;]<br>cfg.ALLOWED_PORTS                  # [80, 443, 8080]  ← actual ints</pre><h3>Optional variables</h3><pre>@envclass<br>class Config:<br>    DATABASE_URL: str<br>    SENTRY_DSN: str | None = None    # returns None if not set, no KeyError<br>    REDIS_URL: str | None = None</pre><h3>Failing loud at startup, the right way</h3><pre>import sys<br>from envclass import envclass, EnvError<br><br><br>@envclass<br>class Config:<br>    DATABASE_URL: str<br>    SECRET_KEY: str<br>    PORT: int = 8000<br>try:<br>    cfg = Config()<br>except EnvError as e:<br>    print(f&quot;❌ Config error: {e}&quot;)<br>    # Output: Config error: Missing required environment variable(s): DATABASE_URL, SECRET_KEY<br>    sys.exit(1)</pre><p>No more 2am incidents. The app refuses to start unless everything it needs is present and correctly typed.</p><h3>Works with the frameworks you already use</h3><h3>FastAPI</h3><pre># config.py<br>from functools import lru_cache<br>from envclass import envclass<br><br><br><br>@envclass<br>class Settings:<br>    APP_NAME: str = &quot;My API&quot;<br>    DATABASE_URL: str<br>    SECRET_KEY: str<br>    DEBUG: bool = False<br>@lru_cache<br>def get_settings():<br>    return Settings()</pre><pre># main.py<br>from fastapi import Depends<br>from config import Settings, get_settings<br><br><br><br>@app.get(&quot;/health&quot;)<br>def health(settings: Settings = Depends(get_settings)):<br>    return {&quot;app&quot;: settings.APP_NAME, &quot;debug&quot;: settings.DEBUG}</pre><h3>Django</h3><pre># settings.py<br>from envclass import envclass<br><br><br><br>@envclass<br>class Env:<br>    SECRET_KEY: str<br>    DEBUG: bool = False<br>    DATABASE_URL: str<br>    ALLOWED_HOSTS: list[str] = [&quot;localhost&quot;]<br><br>env = Env()<br>SECRET_KEY    = env.SECRET_KEY<br>DEBUG         = env.DEBUG<br>ALLOWED_HOSTS = env.ALLOWED_HOSTS</pre><h3>The .env file just works</h3><p>No python-dotenv needed. frozenenv parses .env files out of the box.</p><pre># .env<br>DATABASE_URL=postgres://localhost/mydb<br>SECRET_KEY=&quot;my secret key with spaces&quot;<br>DEBUG=true<br>ALLOWED_HOSTS=localhost,127.0.0.1<br>PORT=9000<br><br><br># export syntax works too<br>export REDIS_URL=redis://localhost:6379</pre><p>Point to a different file:</p><pre>@envclass(env_file=&quot;.env.production&quot;)<br>class Config:<br>    DATABASE_URL: str</pre><p>Or skip file loading entirely and rely only on real environment variables:</p><pre>@envclass(env_file=None)<br>class Config:<br>    DATABASE_URL: str</pre><h3>Compared to the alternatives</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Rf7ZZZtHjja2vcsZGC2E0g.png" /></figure><p>pydantic-settings is still the right choice for complex validation, custom validators, and nested config models. frozenenv is for the 90% of projects that just need typed, immutable env vars with zero overhead.</p><h3>Install it now</h3><pre>pip install frozenenv</pre><p>The import name is envclass:</p><pre>from envclass import envclass, EnvError</pre><ul><li>PyPI: <a href="https://pypi.org/project/frozenenv">pypi.org/project/frozenenv</a></li><li>GitHub: <a href="https://github.com/hudihi/frozenenv">github.com/hudihi/frozenenv</a></li></ul><h3>Why I built this</h3><p>I kept solving the same small problem typed env vars without heavy dependencies in project after project. Every time I wrote int(os.environ.get(&quot;PORT&quot;, &quot;8000&quot;)) I felt the friction. So I spent a day and built the smallest possible thing that fixes it permanently.</p><p>frozenenv is 200 lines of pure Python. No magic. No build steps. No C extensions. Just a decorator that does what you&#39;ve been doing manually for years but correctly, once, in one place.</p><p>If you’ve written int(os.environ.get(...)) more than twice in your career, this library is for you.</p><p><em>Found this useful? Star the repo on GitHub and share this with a Python developer who deserves better config management. And if you run into an edge case the library doesn’t handle open an issue. That’s how open source gets better.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=69f0ef3997eb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>