Skip to main content

Sane Settings

Simply Sane Settings

Born of the desire of having app config behaving in an explicit way, to zero in and fix missing config very quickly

Key Principles

  1. Explicit is better than implicit - Never silently use defaults when you meant to configure something
  2. Fail fast with clarity - Missing env vars show you exactly what was expected
  3. Debuggability - Debug logs highlight when defaults are used, helping catch typos
  4. Security - Secret values are masked in logs automatically

Installation

pip install sane-settings

Requires Python 3.10 or higher.

Quick Start

from dataclasses import dataclass
from sane_settings import EnvConfigBase, env_field, prefix_field, SecretStr

@dataclass
class DatabaseSettings(EnvConfigBase):
    host: str = env_field("HOST", default="localhost")
    port: int = env_field("PORT", default=5432)
    password: SecretStr = env_field("PASSWORD")  # Required - will fail if missing

@dataclass
class AppSettings(EnvConfigBase):
    database: DatabaseSettings = prefix_field("DB")
    debug: bool = env_field("DEBUG", default=False)

# Load from environment
settings = AppSettings.load_from_env(app_prefix="MYAPP")

# Access nested settings
print(settings.database.host)  # Uses MYAPP_DB_HOST
print(settings.password)       # SecretStr('**********')

Design Goals

  • Having an extremely explicit settings library that minimise the time spent fighting with settings
  • For attributes without defaults, if SaneSettings does not find a env var, it will fail loading and tell you EXACTLY what env var it was expecting.
  • Any attributes with default that does NOT get replaced with an env var will be highlighted in logs (use DEBUG log level) which helps you quickly find typos in your Env Vars
  • Only supports environment variables to override the defaults

Non Goals

  • Loading from config files

Advanced Example

from sane_settings import (
    EnvConfigBase,
    Environments,
    SecretStr,
    env_field,
    prefix_field,
)


@dataclass
class DatabaseSettings(EnvConfigBase):
    """Database connection settings."""

    # Database connection parameters
    host: str = env_field("HOST", default="localhost")                # APP_DB_HOST
    port: int = env_field("PORT", default=5432)                       # APP_DB_PORT
    user: str = env_field("USER", default="postgres")                 # APP_DB_USER
    password: SecretStr = env_field("PASSWORD", default="postgres")   # APP_DB_PASSWORD, will NOT show in logs unless you use DatabaseSettings().password.get_secret_value()
    name: str = env_field("NAME", default="queue_service")            # APP_DB_NAME

    @property
    def uri(self) -> str:
        """Get the PostgreSQL connection URI."""
        return f"postgresql://{self.user}:{self.password.get_secret_value()}@{self.host}:{self.port}/{self.name}?sslmode=allow"


@dataclass
class Settings(EnvConfigBase):
    """Main application settings."""

    # Nested Settings objects
    database: DatabaseSettings = prefix_field("DB")     # Any nested attribute will use APP_DB_{atribute}
    
    # Basic settings
    TRACING_BACKEND: TracingBackends | None = env_field("TRACING_BACKEND")
    ENVIRONMENT: Environments = env_field("ENVIRONMENT")                    # a default enum for your environments


    # defaults at the end
    SERVICE_NAME: str = "my-service"


settings = Settings.load_from_env(app_prefix="APP", pretty_check=True)

Documentation

Document Purpose
User Guide Getting started, configuration patterns, type casting, best practices
API Reference Complete reference for all public classes and functions
Architecture Guide How the library works internally, type system, loading mechanism
Examples Real-world usage patterns for common scenarios

Development

Release

(https://github.com/alltuner/uv-version-bumper) just bump-patch|minor|major just push-all

uv run pytest -v

Metadata

Release files for sane-settings 0.2.9

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sane-settings 0.2.9
File Size Uploaded
sane_settings-0.2.9.tar.gz 31.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sane-settings 0.2.9
File Interpreter ABI Platform
sane_settings-0.2.9-py3-none-any.whl Python 3 none any Details

Total release size: 37.2 kB

Release files / sane_settings-0.2.9.tar.gz

Download URL sane_settings-0.2.9.tar.gz
Size 31.0 kB
Tags Source
SHA-256 checksum
How to use checksums
c4b087c8700304aabdb11dfdca4cae49d31b2a80f6ef9588efccf801109827c9
BLAKE2b-256 checksum
How to use checksums
fc3122423fcdb57f9c27ef0f9b5fa023d1058259679b8c300dc8aa265eb1b287
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / sane_settings-0.2.9-py3-none-any.whl

Download URL sane_settings-0.2.9-py3-none-any.whl
Size 6.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6807a91ad22b0e98a9397cf17972684750952f1affae16a3c6b1bb5feedfcbcf
BLAKE2b-256 checksum
How to use checksums
af07e502a26d12339e5403c5157c7e2b73e62aa765ae6a92353bdcf6a5fd07ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.28 {"installer":{"name":"uv","version":"0.9.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.2.9 This release

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.2

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page