Skip to main content

bean.config

bean.config is a minimal configuration framework for Python.

Define your configuration as a class and populate it from command-line arguments, environment variables, defaults, overrides, or custom sources.

Just enough to arrange some beans.


Overview

With bean.config you get:

  • Type-safe configuration classes.
  • Automatic loading from multiple built-in or user-defined sources.
  • Configurable source priority.
  • Custom validators and normalizers.
from enum import Enum
from bean import config

class Mode(Enum):
    DEV = "dev"
    PROD = "prod"

@config
class AppConfig:
    DEBUG: bool = False
    MODE: Mode = Mode.DEV
    HOST: str
    PORT: int = 8080

# `AppConfig` is now the loaded configuration instance.
print(config.dump_str(AppConfig))

Note:

To quickly inspect the public API:

python -c "
    import bean.config as bean
    for v in sorted(dir(bean)):
        print(f'- {v}')
"

Installation

Requirements:

  • Python 3.14+

Using pip:

pip install --upgrade bean-config

Using curl (direct download):

curl -Ls \
    https://raw.githubusercontent.com/numen-0/bean/refs/heads/main/bean-config/src/bean/config.py

API

This is a quick reference for the main API.

For full details, see the source code.

Loading

cfg = config.load(
    Config,
    argv=["--host", "localhost"],
)

config.load(
    Config,
    env_prefix="APP",
    overrides={
        "HOST": "localhost",
    },
)

config.load(
    Config,
    priorities=("extra", "defaults"), # only load from extra and then defaults
    extra_sources={
        "extra": foo,                 # foo(field) -> value
    },
)

Built-in sources:

source description
args Command-line arguments (argparse).
envs Environment variables.
defaults Default class attributes.
overrides Explicit values passed via the overrides parameter.

Validators

Validators can raise exceptions or return a boolean signaling success:

class Config:
    NAME: str
    PORT: int

    @config.validator("PORT")
    def port_is_valid(self, port: int) -> bool:
        return 0 < port <= 65535

    @config.validator("NAME")
    def name_is_valid(self, name: str):
      if name == "":
          raise ValueError("Empty NAME")

Note: Validators may return True, False or None.

Note: A validator fails only if it returns False or raises an exception.

Normalizers

Normalizers can be used to finalize the configuration load.

class Config:
    NAME: str
    HOST: str

    @config.normalizer("NAME", "HOST")
    def lowercase(self, name: str, host: str) -> tuple[str, str]:
        return name.lower(), host.lower()

    @config.normalizer("NAME", "HOST", series=True)
    def strip(self, value: str) -> str:
        return value.strip()

    @config.normalizer()
    def normalize(self) -> None:
      ...

Note:

The return value must match the declared fields

  • No fields -> return None
  • One field -> return the normalized value
  • Multiple fields -> return a tuple with one value per field
  • series=True -> the function receives and returns one field at a time

Notes

Validators and normalizers follow the same definition rules:

  • The number of declared fields must match the function parameters.
  • They work with instance methods, @staticmethod, and @classmethod.
  • Execution order is determined by priority, then by function name.

License

All the repo falls under the MIT License.

Metadata

Release files for bean-config 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bean-config 0.2.0
File Size Uploaded
bean_config-0.2.0.tar.gz 11.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bean-config 0.2.0
File Interpreter ABI Platform
bean_config-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.1 kB

Release files / bean_config-0.2.0.tar.gz

Download URL bean_config-0.2.0.tar.gz
Size 11.4 kB
Tags Source
SHA-256 checksum
How to use checksums
0249be74914e7e3d754e08590c3372ff39ff4c617725381abe41582f3116366b
BLAKE2b-256 checksum
How to use checksums
644d4fae48b987067e47d0536988d3b111e459f2ac14cd822e49cc32197e3670
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.3

Release files / bean_config-0.2.0-py3-none-any.whl

Download URL bean_config-0.2.0-py3-none-any.whl
Size 9.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b653e664f7553a4f9fa5c18ffaca3b779cf0b88e27d41eaeaa5cdf62f8af59bb
BLAKE2b-256 checksum
How to use checksums
68ceebc09d940b998374357154227e32f17f6c2ecc74fc5126e57c01cbf276e0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.3

Release history Release notifications | RSS feed

0.2.2

2 release files

0.2.1

2 release files

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page