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:
config/settings.py— a pydanticBaseSettingsclass. It reads.env, validates and casts every variable, and exposes typed attributes (settings.DEBUGis a realbool). This is the single source of truth for environment variables.- 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.
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 methodYou 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 pytestAdd your own variables by declaring typed fields on the class — pydantic handles parsing, casting, and defaults:
class Settings(BaseSettings):
# ...
STRIPE_API_KEY: str | None = None
FEATURE_NEW_BILLING: bool = FalseConfig namespaces
The loader (alata.foundation.config_loader) expects a fixed set of modules in your config package.
Required
| Namespace | Module | Exported names | Repository shape |
|---|---|---|---|
database | config/database.py | CONNECTIONS, DB_CONFIG | database.connections.*, database.config.* |
cache | config/cache.py | CACHE_CONFIG | cache.* |
queue | config/queue.py | QUEUE_CONFIG | queue.* |
logging | config/logging.py | LOG_CONFIG | logging.* |
If any of these modules is missing, boot fails — the framework's base providers depend on them.
Optional
| Namespace | Module | Exported names | Repository shape |
|---|---|---|---|
redis | config/redis.py | CONNECTIONS | redis.connections.* |
rate_limit | config/rate_limit.py | RATE_LIMIT_CONFIG | rate_limit.* |
filesystem | config/filesystem.py | DISKS | filesystem.disks.* |
middleware | config/middleware.py | CORS, GZIP, TRUSTED_HOSTS, REQUEST_LOGGING, MAINTENANCE_MODE | middleware.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:
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:
- An active pytest test selects
testing. During test collection, setAPP_ENV=testingexplicitly before importing the application. - Explicit
env=argument toApplication(...). --envCLI flag, such as./alata migrate --env production.APP_ENValready present in the process environment..envin the project root — loaded, then itsAPP_ENVis 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_ENVdrives kernel environment resolution and the exception handler's production detection (any value starting withprodcounts 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:
APP_ENV=production
ENVIRONMENT=production
DEBUG=falseThe 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" → TrueApplication 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:
APP_TIMEZONE=UTC
SCHEDULE_TIMEZONE=Asia/TokyoThe 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, defaultFalse) — whentrue, the scheduler skips due events unless they are markedeven_in_maintenance_mode(). Thestorage/framework/downflag file created by./alata downhas the same effect, and additionally makes HTTP requests return 503.middleware.maintenance_mode.bypass_paths(fromconfig/middleware.py) — paths the 503 middleware lets through while the app is down. The template bypasses/healthso load balancers keep probing:
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.