Nested async function scope not being maintained in use_user_data hook - not sure what changed
#293
Replies: 1 comment 1 reply
-
|
As a follow-up, I thought about it more and definitely experienced a similar issue recently. I had a component with a nested function that I actually called in a background thread (using python's Hopefully this can help in getting to the bottom of all this. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have been working on the refactor of reactpy-django to be compatible with the pre-lease v2 of reactpy core. In that context, I've been chasing down an issue related to the use_user_data hook. The pre-existing tests for that feature are failing. I wondered if it was due to underlying updates to reactpy core that I had missed in the refactor, but after digging a while, it seems to be possibly related to how more recent Python versions handle asynchronous variable scope.
Here's the component in question, from
tests\test_app\components.py:Here's the test code from
tests\test_app\tests\test_components.py:Here's the code for the hook where the bug seems to exist (at
src\reactpy_django\hooks.py):The test, which can also be reproduced manually, loads the component initially with no user logged in. Then, a user is logged in by clicking the "Login 1" button. Then, "test" is entered into the text input, and "Enter" is hit to submit. This should then apply/save "test" to the logged-in user's data. Instead, the
ValueError("AnonymousUser cannot have user data.")is thrown.So it seems that the
uservariable from its defined scope (theuse_user_datafunction) is not being properly shared/persisted within the scope of the nested async_set_user_datafunction. I verified that theuservariable updates and is reflected properly in the parent scope after logging in user_1, but again, the nested function's scope will still reflect an anonymous user.I was able to fix (?) this issue - or at least get around it - by updating the
use_user_datato also store the user as as a reference variable withuse_ref, like so:I'm wondering if there's a better way, and if someone else knows better what's going on here. I'm guessing there was an update in the way that a recent version of Python handles scope in this type of context. I did experience something very similar recently where some similar code had worked before and then stopped working "randomly" (or so I thought, but I'm pretty sure I did update my Python version in between).
Beta Was this translation helpful? Give feedback.
All reactions