Repository navigation
feat: ship the settings contract as pydantic-settings models - #23
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #22
Problem
The settings contract —
BaseServiceSettingsProtocoland the four section protocols — is read structurally, and the sinks fall back to defaults withgetattr. 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'stracing.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 newsettingsextra (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 rates0.0, ...). They validate what the backends assume instead of coercing silently: the level isLogLevelStr(upper-cased on input, soLOGGING__LEVEL=debugworks andwarnfails at load rather than becomingINFO), ratios are bounded to[0, 1], the port to a port.BaseServiceSettings(BaseSettings)composes them withenvironment="local",app_version="0.0.0"andget_app_version(),env_nested_delimiter="__", no prefix. A subclass'smodel_configmerges with it, and sections narrow or disable exactly as a hand-written class does (metrics: None = None, aLoggingSettingssubclass with a narrowerLiteral).logging,metrics(exposition off) anderror_tracking(no DSN, so off) are present;tracingisNonebecause 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; thesettingsextra pulls in nothing else.pydantic_settingsjoins the core forbidden list). Missing extra →ImportErrornamingservicewright[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"), acontribpackage (a third top-level concept for one module), makingsettingspull the observability extras (heavy and surprising),SecretStrfordsn(the protocol and the Sentry sink takestr).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:4317against a subclass with its ownenv_file),TracingSettings()handed to the real OTel sink resolving the resource name toAppSpec.service_name, the shipped defaults producing the same OTel provider / Sentry init kwargs / stdlib handler as a bare section, level and ratio validation,Noneand narrowing in subclasses,model_configmerging, the extra-missing import error.tests/unit/test_settings_typing.py: two new mypy-driven cases —BaseServiceSettingsand a narrowed/disabled subclass satisfyBaseServiceSettingsProtocol.servicewright/adapters/settingsremoved, the new module fails at collection (ModuleNotFoundError) and bothshipped-modelstyping cases fail (they reveal a member of the shipped class, soignore_missing_importscannot turn a missing module into a vacuousAnypass).DEBUG, tracing created withcollector_url='otel:4317', error tracking present and off.zensical build --clean— no issues; the reference page renders the new package.