Skip to main content

Add your description here

Project description

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

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

Getting started

pip install sane_settings
from sane_settings import (
    EnvConfigBase,
    env_field,
    prefix_field,
)


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

    # Database connection parameters
    host: str = env_field("HOST", default="localhost")      # Database host
    port: int = env_field("PORT", default=5432)             # Database port
    user: str = env_field("USER", default="postgres")       # Database user
    password: str = env_field("PASSWORD", default="postgres")  # Database password



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

    # Nested Settings objects
    database: DatabaseSettings = prefix_field("DB")     # Any nested attribute will be EXAMPLE__DB__{attribute}
    # defaults at the end
    SERVICE_NAME: str = "my-service"


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

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)

DEv

Release

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

uv run pytest -v

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

sane_settings-0.2.8.tar.gz (30.9 kB view details)

Uploaded Source

Built Distribution

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

sane_settings-0.2.8-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file sane_settings-0.2.8.tar.gz.

File metadata

  • Download URL: sane_settings-0.2.8.tar.gz
  • Upload date:
  • Size: 30.9 kB
  • Tags: Source
  • Uploaded using 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}

File hashes

Hashes for sane_settings-0.2.8.tar.gz
Algorithm Hash digest
SHA256 e425db44fdd314c15b32bff3012016238ffb3456454fae7c02d5dfe079e915b5
MD5 9e783f729553958329e417c7c4952073
BLAKE2b-256 4459e8d05a375985afd60ef9d187121a32a4a03790f473f9f92db8478ad704d5

See more details on using hashes here.

File details

Details for the file sane_settings-0.2.8-py3-none-any.whl.

File metadata

  • Download URL: sane_settings-0.2.8-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using 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}

File hashes

Hashes for sane_settings-0.2.8-py3-none-any.whl
Algorithm Hash digest
SHA256 7e892fd1c3f08dcc8be20929fecb7de4ab51fdec2a36208538905758f5224369
MD5 fc774dda91830d3ccba8bdd26ab10585
BLAKE2b-256 d1170047979a1635cf17f347b5197a7df722d9f8bb67787d8f7bd38d6e164718

See more details on using hashes here.

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