Skip to main content

Strong typing for your environment variables

Project description

🦕 envtyped

This library is heavily inspired by python-decouple. Main goal is to provide a simple way to load environment variables, with type hinting, for my FastAPI projects.

📝 Table of Contents

📦 Installation

pip install envtyped

🚀 Usage

from envtyped import config

# Get environment variable, no need to do anything else, .env file is lazy loaded
SECRET_KEY = config.get("MY_ENV_VAR") # str

# You can cast the value to another type
DELAY_IN_SECONDS = config.get("DELAY_IN_SECONDS", cast=int) # int

# You can also use a custom function to cast the value
DELAY = config.get("DELAY", cast=lambda x: datetime.fromtimestamp(int(x))) # datetime

# If you need to, you also can use a custom class to cast the value, that can be constructed from a string
class Employee:
    def __init__(self, name: str) -> None:
        self.name = name.capitalize()

    def hello(self) -> str:
        return f"Hello {self.name}"


EMPLOYEE = config.get("EMPLOYEE", cast=Employee) # Employee
EMPLOYEE.hello()
# -> Hello John

# By default all env variables are required, but you can also set a default value
SECRET_KEY = config.get("SECRET_KEY", default="default_secret_key")

# Or you can set a value to be optional
SECRET_KEY = config.get("SECRET_KEY", optional=True) # Optional[str]


# This library provides a helpers to read csv 
from envtyped import Csv

AVAILABLE_LANGUAGES = config.get("AVAILABLE_LANGUAGES", cast=Csv()) # List[str]

# Csv can also be used to cast a list of string to a list of class
class Language:
    def __init__(self, name: str) -> None:
        self.name = name

AVAILABLE_LANGUAGES = config.get("AVAILABLE_LANGUAGES", cast=Csv(cast=Language)) # List[Language]

# Choise is a helper to limit the value to a list of choice
from envtyped import Choise

USER_LANGUAGE = config.get("USER_LANGUAGE", cast=Choise(["en", "fr"])) # str
USER_CURRENCY = config.get("USER_CURRENCY", cast=Choise(["usd", "eur"], default="usd")) # str

📝 License

This project is licensed under the MIT License - see the LICENSE file for details.

With ❤️

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

envtyped-0.1.1.tar.gz (4.0 kB view hashes)

Uploaded Source

Built Distribution

envtyped-0.1.1-py3-none-any.whl (5.2 kB view hashes)

Uploaded Python 3

Supported by

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