Skip to content

feat: ship the settings contract as pydantic-settings models - #23

Merged
AlexeyShalaev merged 2 commits into
masterfrom
feat/settings-models
Aug 29, 2026
Merged

AlexeyShalaev merged 2 commits into
masterfrom
feat/settings-models

Conversation

@AlexeyShalaev

@AlexeyShalaev AlexeyShalaev commented Aug 29, 2026 •

Copy link
Copy Markdown
Member

Closes #22

Problem

The settings contract — BaseServiceSettingsProtocol and the four section protocols — is read structurally, and the sinks fall back to defaults with getattr. The docs described it as field tables plus a pydantic-settings example that every consumer transcribed by hand, and nothing checked the copy: a field could turn required (the reporter's tracing.service_name), a misspelled one silently fell back to a default nobody could see, and the defaults themselves were guesswork because the tables listed types and meanings but not values.

Design

servicewright.adapters.settings, behind the new settings extra (pydantic-settings>=2.3,<3, the extra name the sibling libraries share):

  • LoggingSettings, MetricsSettings, TracingSettings, ErrorTrackingSettings — pydantic models with exactly the protocols' fields and the sinks' own fallbacks as defaults (service_name="" → AppSpec.service_name, environment="" → settings.environment, sample_ratio=1.0, both Sentry rates 0.0, ...). They validate what the backends assume instead of coercing silently: the level is LogLevelStr (upper-cased on input, so LOGGING__LEVEL=debug works and warn fails at load rather than becoming INFO), ratios are bounded to [0, 1], the port to a port.
  • BaseServiceSettings(BaseSettings) composes them with environment="local", app_version="0.0.0" and get_app_version(), env_nested_delimiter="__", no prefix. A subclass's model_config merges with it, and sections narrow or disable exactly as a hand-written class does (metrics: None = None, a LoggingSettings subclass with a narrower Literal).
  • Defaults by section presence: logging, metrics (exposition off) and error_tracking (no DSN, so off) are present; tracing is None because a present section installs a tracer provider — TRACING__COLLECTOR_URL=... creates it from the environment. A present section still activates its backend at bootstrap, so the extras story is unchanged; the settings extra pulls in nothing else.
  • Placement follows this repo's layering: a pydantic-settings binding of the settings port is an adapter, so it lives next to the others (and joins the "Adapters never depend on each other" contract; pydantic_settings joins the core forbidden list). Missing extra → ImportError naming servicewright[settings].

Docs: the section tables gain a Default column, the concept page gets a "Shipped models" section, the blueprints import the models instead of restating them, extras tables/reference/architecture updated.

Rejected: docs-only (leaves the transcription unchecked — done as an addition), models in core (breaks "Core is stdlib-only"), a contrib package (a third top-level concept for one module), making settings pull the observability extras (heavy and surprising), SecretStr for dsn (the protocol and the Sentry sink take str).

Verification

  • make check + make test: ruff, format, mypy, import-linter clean; 677 passed, 10 skipped, coverage 94.28% (the new package at 100%).
  • tests/unit/test_settings_models.py (21 tests): the reporter's scenario (LOGGING__LEVEL=DEBUG TRACING__COLLECTOR_URL=otel:4317 against a subclass with its own env_file), TracingSettings() handed to the real OTel sink resolving the resource name to AppSpec.service_name, the shipped defaults producing the same OTel provider / Sentry init kwargs / stdlib handler as a bare section, level and ratio validation, None and narrowing in subclasses, model_config merging, the extra-missing import error.
  • tests/unit/test_settings_typing.py: two new mypy-driven cases — BaseServiceSettings and a narrowed/disabled subclass satisfy BaseServiceSettingsProtocol.
  • Negative control: with servicewright/adapters/settings removed, the new module fails at collection (ModuleNotFoundError) and both shipped-models typing cases fail (they reveal a member of the shipped class, so ignore_missing_imports cannot turn a missing module into a vacuous Any pass).
  • The issue's snippet run verbatim (module path aside) with the env from the issue: logging DEBUG, tracing created with collector_url='otel:4317', error tracking present and off.
  • Docs build: zensical build --clean — no issues; the reference page renders the new package.

The settings contract was documented as field tables plus a pydantic-settings
example that every consumer transcribed by hand, and nothing checked the copy:
a field could become required (tracing.service_name), a misspelled one fell
back to a default nobody could see, and the defaults themselves were guesswork.

servicewright.adapters.settings, behind the new `settings` extra, is the
contract written down once: LoggingSettings, MetricsSettings, TracingSettings
and ErrorTrackingSettings with exactly the protocols' fields and the sinks'
own fallbacks as defaults (validated instead of silently coerced), and
BaseServiceSettings composing them with `environment`, `app_version` and
get_app_version(), loading from the environment with `__` nesting. Subclasses
narrow or disable sections exactly as a hand-written class does.

The docs tables gain the defaults, the blueprints import the models instead of
restating them, and the mypy-driven typing test now asserts the shipped models
satisfy BaseServiceSettingsProtocol.

Closes #22
…resolvable

With ignore_missing_imports on, mypy types a missing module as Any and the
protocol check passes vacuously; revealing a member of the shipped models
turns that into a failure.
@AlexeyShalaev
AlexeyShalaev merged commit 55a62db into master Aug 29, 2026
6 checks passed
@AlexeyShalaev
AlexeyShalaev deleted the feat/settings-models branch September 6, 2026 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: the four settings models its own docs tell you to transcribe are not shipped anywhere

1 participant