Skip to content

Conversation

@mukulmurthy
Copy link
Contributor

Add an internal dict state inside Context that can be interacted with via the get_state_value and set_state_value APIs. This is intended for middleware or other stages of a request to pass information back and forth, since there is not an easy way to do that right now without using ContextVars.

When a context is created, it inherits state from its parents. What that means is that:

  • State set on a child context never affects the parent context.
  • State set on a parent context after the child context is initialized is not propagated to the child context.

Proposed in #1118. Tested with new tests in test_context.py.

@github-actions github-actions bot added the tests label Jul 16, 2025
Comment on lines +98 to +99
is created (nested contexts), it inherits a copy of its parent's state, ensuring
that modifications in child contexts don't affect parent contexts.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed the semantics of this a little in the PR description. If you think child state should affect the parent, we could do it that way.

@mukulmurthy mukulmurthy changed the title Add state dict to Context (#1118) Add state dict to Context (#1118) Jul 16, 2025
@jlowin jlowin added the enhancement Improvement to existing functionality. For issues and smaller PR improvements. label Jul 22, 2025
@jlowin jlowin merged commit 544bc22 into jlowin:main Jul 22, 2025
7 of 8 checks passed
@mukulmurthy
Copy link
Contributor Author

Thank you for fixing up and merging @jlowin!

@jlowin jlowin added feature Major new functionality. Reserved for 2-4 significant PRs per release. Not for issues. and removed enhancement Improvement to existing functionality. For issues and smaller PR improvements. labels Jul 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Major new functionality. Reserved for 2-4 significant PRs per release. Not for issues.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants