Fix async state scoping for submodules (#2866)#2892
Open
raza-khan0108 wants to merge 1 commit into
Open
Conversation
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.
What type of PR is this? (Check all that apply)
Description
This PR resolves an issue where
_AsyncStatefailed to correctly resolve the module context when accessing or modifying variables within a submodule. By capturing the module context using the caller's stack frame in__getattr__and__setattr__(viainspect.stack()[1]), state variables accessed during async callbacks are now correctly resolved in the proper submodule context instead of erroneously falling back to__main__.Related Tickets & Documents
How to reproduce the issue
Set up an application with a submodule handling an async callback, where state variables are updated.
main.py) routing to ahome_pageimported from a submodule (pages/home.py).pages/home.py, define an async callback (e.g.async def count_to_10(state): state.counter = 10) hooked up to a button.Variable 'counter' is not available in the '__main__' moduleorAttributeError. It now successfully resolves the variable inside thepages.homecontext.(See #2866 for the full reproduction code).
Backporting
This change should be backported to:
Checklist
We encourage keeping the code coverage percentage at 80% or above.
If not, explain why:
If not, explain why: A unit test accurately captures and isolates the
_AsyncStatemodule scoping bug, rendering a full end-to-end test unnecessary for this fix.If not, explain why: This is an internal framework fix and introduces no public API or syntax changes for users.
If not, explain why: Can be handled internally during the release preparation if necessary.