Decorators Example (decorators.py)¶
Purpose¶
Shows how to inject configuration values into functions using decorators:
@Config.with_setting(descriptor)— injects kwarg named after descriptor@Config.with_kwarg(section, option, kwarg_name, default)— inject by strings + custom kwarg name
Running¶
uv run python examples/decorators.py
Behavior¶
ServiceConfig.retry_count and ServiceConfig.timeout are standard descriptors. The decorators wrap the functions so when called, kwargs contain the current config values.
Example Output (first run)¶
Processing with 3 retries
Notes¶
- The
with_kwargvariant does not require direct descriptor reference (less type-safe, more flexible). - You can still override the injected kwarg manually when calling the function; manual kwargs win.
Try Variations¶
- Replace
with_kwargwithwith_settingreferencing a different descriptor.