alata.
Getting Started

Configuration

Python config modules, typed pydantic settings, the config repository, and environment detection.

Alata configuration is plain Python. Each concern gets a module in config/ — database.py, queue.py, logging.py — that builds a declarative dict from typed settings. At boot, before any service provider runs, the framework loads these modules into a central config repository with dot-notation access, and binds your typed settings object alongside it.

The two layers

Configuration flows through two complementary layers:

  1. config/settings.py — a pydantic BaseSettings class. It reads .env, validates and casts every variable, and exposes typed attributes (settings.DEBUG is a real bool). This is the single source of truth for environment variables.
  2. The other config/ modules — declarative dicts (CONNECTIONS, QUEUE_CONFIG, LOG_CONFIG, ...) built from settings. Providers read these through the config repository, never from the environment directly.

This split keeps env parsing in one validated place while letting each subsystem's configuration stay structured and readable.

config/database.py
from config.settings import settings

# DB_CONNECTION selects the driver; "pgsql" is accepted as an alias for "postgres"
_DRIVER = {"pgsql": "postgres"}.get(settings.DB_CONNECTION, settings.DB_CONNECTION)

CONNECTIONS = {
    "default": {
        "driver": _DRIVER,
        "host": settings.DB_HOST,
        "port": settings.DB_PORT,
        "database": settings.DB_DATABASE,
        "username": settings.DB_USERNAME,
        "password": settings.DB_PASSWORD,
        "pool_size": 5,
        "max_overflow": 10,
        "pool_pre_ping": True,
        "pool_recycle": 1800,
        "echo": False,
    },
}

The config repository

During boot Phase 0 — before any provider registers — the framework imports your config package and merges each module into a ConfigRepository, bound in the service container as "config". Every value is then reachable with a dot-notation key:

from alata.container import container

config = container.make("config")

config.get("database.connections.default.pool_size")  # 5
config.get("cache.default")                           # "file"
config.get("queue.connections.redis.broker_url")      # "" until a Redis URL is configured
config.get("missing.key", "fallback")                 # "fallback"

The repository API is small:

config.get(key, default=None)   # dot-notation lookup
config.set(key, value)          # dot-notation write
config.has(key)                 # key exists?
config.all()                    # the entire nested dict
config.merge(namespace, values) # register a top-level namespace (used at boot)
config.flush()                  # clear everything (test isolation)

The config() helper

For quick reads anywhere in your code, alata.config_repository exports module-level helpers that resolve the repository from the current container:

from alata.config_repository import config, config_set

config("database.connections.default.pool_size")  # 5

# Set before the cache is first resolved:
config_set("cache.default", "null")

If the container has not booted yet (for example during module-level imports), config() returns the default instead of raising — so it is safe in module scope, but the value will only be real after boot.

Typed settings

Your pydantic settings object is bound as "settings":

from alata.container import container

settings = container.make("settings")

settings.DEBUG            # bool
settings.DB_HOST          # str
settings.ENVIRONMENT      # "local" | "development" | "production"
settings.is_production()  # helper method

You can also import it directly — from config.settings import settings — in application code; providers and framework code always go through the container binding.

The template's Settings class ships helpers you will reach for often:

settings.is_local()         # ENVIRONMENT == "local"
settings.is_development()   # ENVIRONMENT == "development"
settings.is_production()    # ENVIRONMENT == "production"
settings.is_testing()       # True under pytest

Add your own variables by declaring typed fields on the class — pydantic handles parsing, casting, and defaults:

config/settings.py
class Settings(BaseSettings):
    # ...
    STRIPE_API_KEY: str | None = None
    FEATURE_NEW_BILLING: bool = False

Config namespaces

The loader (alata.foundation.config_loader) expects a fixed set of modules in your config package.

Required

NamespaceModuleExported namesRepository shape
databaseconfig/database.pyCONNECTIONS, DB_CONFIGdatabase.connections.*, database.config.*
cacheconfig/cache.pyCACHE_CONFIGcache.*
queueconfig/queue.pyQUEUE_CONFIGqueue.*
loggingconfig/logging.pyLOG_CONFIGlogging.*

If any of these modules is missing, boot fails — the framework's base providers depend on them.

Optional

NamespaceModuleExported namesRepository shape
redisconfig/redis.pyCONNECTIONSredis.connections.*
rate_limitconfig/rate_limit.pyRATE_LIMIT_CONFIGrate_limit.*
filesystemconfig/filesystem.pyDISKSfilesystem.disks.*
middlewareconfig/middleware.pyCORS, GZIP, TRUSTED_HOSTS, REQUEST_LOGGING, MAINTENANCE_MODEmiddleware.cors, middleware.gzip, middleware.trusted_hosts, middleware.request_logging, middleware.maintenance_mode

Optional modules are loaded when present. A provider can still require its configuration: keep config/filesystem.py while StorageProvider is registered, or remove both when your application does not use storage.

Framework code never imports config.* modules directly — everything goes through the "config" and "settings" bindings. Follow the same rule in providers: read from the repository, not from os.environ, so tests can override values with config_set().

Using a different package name

The config package name is a kernel parameter (default "config"). Pass config_module when constructing the application to relocate it:

bootstrap/application.py
app = Application(
    base_path=Path(__file__).parent.parent,
    config_module="settings_pkg",
    providers=[...],
)

.env files and environment detection

At construction, the kernel resolves the active environment and loads env files in a defined order:

  1. An active pytest test selects testing. During test collection, set APP_ENV=testing explicitly before importing the application.
  2. Explicit env= argument to Application(...).
  3. --env CLI flag, such as ./alata migrate --env production.
  4. APP_ENV already present in the process environment.
  5. .env in the project root — loaded, then its APP_ENV is read. If no environment can be determined at all, boot raises.

After resolution, the kernel loads .env and then .env.{environment}, if present. Values in these files override existing process variables; the environment-specific file wins. Put test database settings in .env.testing, and include APP_ENV=testing there so the settings and kernel agree. See testing for a complete setup.

The template's Settings also loads .env from the current working directory — it deliberately never walks up into parent directories, so a stray .env outside your project can't leak configuration in. Run commands from the project root.

APP_ENV vs ENVIRONMENT

The template carries two related variables:

  • APP_ENV drives kernel environment resolution and the exception handler's production detection (any value starting with prod counts as production).
  • ENVIRONMENT (local | development | production) drives the settings helpers (is_production()), which gate things like trusted-host enforcement and strict database mode.

Keep them consistent — in production, set both:

.env
APP_ENV=production
ENVIRONMENT=production
DEBUG=false

The env() helper

For one-off reads outside pydantic settings, alata.foundation.environment provides an env() helper with type inference:

from alata.foundation.environment import env

env("WORKER_COUNT", 4)      # "8" in the environment → int 8
env("FEATURE_FLAG", False)  # "true" → True

Application timezone

APP_TIMEZONE sets the default timezone for date helpers, schedules, and logs. It defaults to UTC. Use SCHEDULE_TIMEZONE or LOG_TIMEZONE when either needs a different default:

.env
APP_TIMEZONE=UTC
SCHEDULE_TIMEZONE=Asia/Tokyo

The corresponding repository keys are app.timezone, app.schedule_timezone, and app.log_timezone. See dates and timezones for conversion helpers, model timestamps, and upgrading an existing Settings class.

Maintenance mode configuration

Two settings control maintenance mode behavior:

  • APP_MAINTENANCE_MODE (settings, default False) — when true, the scheduler skips due events unless they are marked even_in_maintenance_mode(). The storage/framework/down flag file created by ./alata down has the same effect, and additionally makes HTTP requests return 503.
  • middleware.maintenance_mode.bypass_paths (from config/middleware.py) — paths the 503 middleware lets through while the app is down. The template bypasses /health so load balancers keep probing:
config/middleware.py
MAINTENANCE_MODE = {
    "bypass_paths": ["/health"],
}

Overriding config in tests

Set repository overrides before the relevant service is first resolved. Changing a value does not rebuild an existing cache store, database engine, or other singleton:

from alata.config_repository import config_set

def test_uses_null_cache():
    config_set("cache.default", "null")
    ...

For cache tests, prefer Cache.fake_dict() and restore it with Cache.restore() in teardown. See testing for isolated container fixtures.

On this page