Add scoped settings context manager with concurrency safety (#247)#249
Merged
Conversation
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>
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.
Purpose
Introduces
lotus.settings.context(**kwargs)— a context manager that temporarily overrides settings and restores them on exit. Usescontextvars.ContextVarso 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 existingconfigure()API. Also fixes staleenable_message_cachereferences in test_settings.py.Test Plan
pytest tests/test_settings.py -v
Three test classes:
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
Checklist
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