Skip to content

Add scoped settings context manager with concurrency safety (#247)#249

Merged
liana313 merged 4 commits into
mainfrom
feature/scoped-settings-context-manager
Apr 13, 2026
Merged

Add scoped settings context manager with concurrency safety (#247)#249
liana313 merged 4 commits into
mainfrom
feature/scoped-settings-context-manager

Conversation

@liana313
Copy link
Copy Markdown
Collaborator

Purpose

Introduces lotus.settings.context(**kwargs) — a context manager that temporarily overrides settings and restores them on exit. Uses contextvars.ContextVar so each thread and asyncio task sees only its own overrides, making concurrent pipelines and test isolation safe. Adds __getattribute__ override to transparently route reads through the per-context overlay without changing the existing configure() API. Also fixes stale enable_message_cache references in test_settings.py.

Test Plan

pytest tests/test_settings.py -v

Three test classes:

  • TestSettings — existing configure/validate behavior unchanged
  • TestSettingsContext — restore on exit, restore to class default, exception safety, nesting, multiple overrides, invalid key raises before any mutation
  • TestSettingsContextConcurrency — thread isolation (barrier to force overlap), global baseline unaffected after threads exit, asyncio task isolation

Test Results

tests/test_settings.py::TestSettings::test_initial_values PASSED
tests/test_settings.py::TestSettings::test_configure_method PASSED
tests/test_settings.py::TestSettings::test_invalid_setting PASSED
tests/test_settings.py::TestSettingsContext::test_context_restores_on_exit PASSED
tests/test_settings.py::TestSettingsContext::test_context_restores_class_default_on_exit PASSED
tests/test_settings.py::TestSettingsContext::test_context_restores_on_exception PASSED
tests/test_settings.py::TestSettingsContext::test_context_yields_settings PASSED
tests/test_settings.py::TestSettingsContext::test_context_multiple_overrides PASSED
tests/test_settings.py::TestSettingsContext::test_nested_contexts PASSED
tests/test_settings.py::TestSettingsContext::test_context_invalid_setting_raises PASSED
tests/test_settings.py::TestSettingsContext::test_context_serialization_format PASSED
tests/test_settings.py::TestSettingsContextConcurrency::test_thread_isolation PASSED
tests/test_settings.py::TestSettingsContextConcurrency::test_thread_baseline_unaffected PASSED
tests/test_settings.py::TestSettingsContextConcurrency::test_asyncio_task_isolation PASSED

14 passed, 1 warning in 5.34s

(Optional) Documentation Update

TODO

Type of Change

  • [ x] Bug fix (non-breaking change which fixes an issue)
  • [ x] New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Performance improvement
  • Refactoring (no functional changes)

Checklist

  • [ x] My code follows the style guidelines of this project
  • [ x] I have performed a self-review of my own code
  • I have commented my code, updating docstrings
  • I have made corresponding changes to the documentation
  • [ x] I have added tests that prove my fix is effective or that my feature works
  • [ x] New and existing unit tests pass locally with my changes

BEFORE SUBMITTING, PLEASE READ https://github.com/lotus-data/lotus/blob/main/CONTRIBUTING.md
anything written below this line will be removed by GitHub Actions

liana313 and others added 4 commits April 13, 2026 16:08
Introduces `lotus.settings.context(**kwargs)` — a context manager that
temporarily overrides settings and restores them on exit. Uses
`contextvars.ContextVar` so each thread and asyncio task sees only its
own overrides, making concurrent pipelines and test isolation safe.
Adds `__getattribute__` override to transparently route reads through
the per-context overlay without changing the existing `configure()` API.
Also fixes stale `enable_message_cache` references in test_settings.py.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Four examples demonstrating lotus.settings.context() usage: scoped model
switching within a pipeline, eval cache isolation, concurrent thread safety,
and concurrent asyncio task safety.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Documents lotus.settings.context() with sections covering basic usage,
nested contexts, concurrent threads, and asyncio tasks.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
The settings tests require no LM or external services, so they run as a
standalone job with a 5-minute timeout.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@liana313 liana313 merged commit 43386e7 into main Apr 13, 2026
9 checks passed
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.

1 participant