Declarative dataclass settings.
Project description
dataclass-settings
dataclass-settings intends to work with any
PEP-681-compliant dataclass-like object,
including but not limited to:
- Pydantic models (v1/v2),
- dataclasses
- attrs classes.
- msgspec models.
dataclass-settings owes its existence
pydantic-settings, in that
pydantic-settings will be a benchmark for dataclass-settings's featureset.
However it was bourne out of frustration with pydantic-setting's approach to
implementing that featureset.
Example
from __future__ import annotations
from dataclass_settings import load_settings, Env, Secret
from pydantic import BaseModel
class Example(BaseModel):
env: Annotated[str, Env("ENVIRONMENT")] = "local"
dsn: Annotated[str, Env("DSN"), Secret('dsn')] = "dsn://"
sub_config: SubConfig
class SubConfig(BaseModel):
nested: Annotated[int, Env("NESTED")] = "4"
example: Example = load_settings(Example)
# or, if you want `nested` to be `SUB_CONFIG_NESTED`
example: Example = load_settings(Example, nested_delimiter='_')
vs Pydantic Settings
Simplicity
-
pydantic-settingsalters how you go about defining your normal pydantic models. You need to switch (some of the) base classes, you need to configure the magicalmodel_config = SettingsConfigDict(...)object, etc.The model becomes inherently entangled with the settings-loading library.
-
dataclass-settingsattaches targeted Annotations metadata to a vanilla pydantic model. You can choose to not useload_settings(for example, in tests), and construct the model instance however you'd like.
Clarity
-
pydantic-settingsmakes it really, really difficult to intuit what the concrete environment variable that's going to be loaded for a given field is actually going to be. Based on my own experience, and from perusing their issue tracker, it seems like this is not an uncommon experience.The combination of field name,
SettingsConfigDictsettings, casing,alias/validation_alias/serialization_alias, and relative position of the env var in the greater config all contribute to it being a task to deduce which concrete name will be used when loading. -
dataclass-settingsby default requires an explicit, concrete name, which maps directly to the value being loaded (Env('FOO')loadsFOO, for sure!)If you want to opt into a less explcict, more inferred setup (like pydantic-settings), you can do so by utilizing the
nested_delimiter='_'andinfer_name=Truearguments.
Typing
-
pydantic-settingsdoes not play super well with type checkers, necessitating the use of a mypy plugin for it to not emit type errors into user code.The code recommended in their documentation for namespacing settings, looks like:
class Settings(BaseSettings): more_settings: SubModel = SubModel()
This only type-checks with mypy (after using the plugin), but not pyright/pylance. Additionally, this actually evaluates the
SubModelconstructor during module parsing!These issues seem(?) to be inherent to the strategy of subclassing
BaseModel, and building in its logic into the object construction process -
dataclass-settingssidesteps this problem by decoupling the definition of the settings from the loading of settings.As such, you're more able to define the model, exactly as you would have with vanilla pydantic:
class Settings(BaseModel): more_settings: SubModel
Internally, the
load_settingsfunction handles the work of constructing the requisite input structure pydantic expects to construct the whole object tree.
Compatibility
-
pydantic-settings'sBaseSettingsinherits from pydantic'sBaseModel. And thus can only function against pydantic models, as the name would imply. -
dataclass-settings's primary entrypoint is a function that accepts a supportable type. As such, it can theoretically support any type that has a well defined object structure, like all ofpydantic,dataclasses, andattrs.Practically,
pydantichas the most robust system for parsing/validating a json-like structure into the models, so it's probably to be the most flexible anyways. But for many simple cases, particularly those without nesting, or that only deal in simple types (like int, float, str, etc); then dataclasses/attrs can certainly provide a similar experience.
Flexibility
-
At time of writing,
pydantic-settings's strategy around "loaders", i.e. supportable settings sources is relatively inflexible. Their issue tracker contains a decent number of requests for a more flexible way of defining settings priorities among different loaders, or even using different settings from within a loader.This, at least, doesn't seem to be an inherent issue to the library necessarily. Just that at present, their API appears to try to reuse pydantic's
Fieldandaliasmechanisms to infer the settings for all loaders. -
dataclass-settingsinstead annotates each field individually, with the loaders that field should use. That means you can have different priorities (or entirely different loaders!) per field.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dataclass_settings-0.7.0.tar.gz.
File metadata
- Download URL: dataclass_settings-0.7.0.tar.gz
- Upload date:
- Size: 97.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
186941b0a4041f6f359670fe181df5bb6b9573c77f4606eb61afb49845c4eb82
|
|
| MD5 |
523473ed1c7950b73cde98de5b2cfc73
|
|
| BLAKE2b-256 |
dfd40cad1a782124538226801d9075e9b35b1d6be5ba9143b61f6db3fbeed53d
|
Provenance
The following attestation bundles were made for dataclass_settings-0.7.0.tar.gz:
Publisher:
release.yml on DanCardin/dataclass-settings
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dataclass_settings-0.7.0.tar.gz -
Subject digest:
186941b0a4041f6f359670fe181df5bb6b9573c77f4606eb61afb49845c4eb82 - Sigstore transparency entry: 569553929
- Sigstore integration time:
-
Permalink:
DanCardin/dataclass-settings@20aab89f83ed1d4ca13294164c2fb6e266467dd1 -
Branch / Tag:
refs/tags/v0.7.0 - Owner: https://github.com/DanCardin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@20aab89f83ed1d4ca13294164c2fb6e266467dd1 -
Trigger Event:
push
-
Statement type:
File details
Details for the file dataclass_settings-0.7.0-py3-none-any.whl.
File metadata
- Download URL: dataclass_settings-0.7.0-py3-none-any.whl
- Upload date:
- Size: 19.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
87736307cc84c010f0227144679b362ddb2b7a9ed052de91bf4f3e0313c4afbd
|
|
| MD5 |
c524e1298ab4cd1b6c48283b350df96b
|
|
| BLAKE2b-256 |
522b1828ed211b6db3feede75f719442a58fc8219b194aea82ff8342e8c3c7c0
|
Provenance
The following attestation bundles were made for dataclass_settings-0.7.0-py3-none-any.whl:
Publisher:
release.yml on DanCardin/dataclass-settings
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dataclass_settings-0.7.0-py3-none-any.whl -
Subject digest:
87736307cc84c010f0227144679b362ddb2b7a9ed052de91bf4f3e0313c4afbd - Sigstore transparency entry: 569553960
- Sigstore integration time:
-
Permalink:
DanCardin/dataclass-settings@20aab89f83ed1d4ca13294164c2fb6e266467dd1 -
Branch / Tag:
refs/tags/v0.7.0 - Owner: https://github.com/DanCardin
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@20aab89f83ed1d4ca13294164c2fb6e266467dd1 -
Trigger Event:
push
-
Statement type: