Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle rx.State subclasses defined in function #4129

Merged
merged 3 commits into from
Oct 11, 2024
Merged

Conversation

masenf
Copy link
Collaborator

@masenf masenf commented Oct 8, 2024

  • create a new container module: reflex.istate.dynamic to save references to dynamically generated substates.
  • for substates with <locals> in the name, copy these to the container module and update the name to avoid duplication.
  • add test for "poor man" ComponentState

Fix #4128

* create a new container module: `reflex.istate.dynamic` to save references to
  dynamically generated substates.
* for substates with `<locals>` in the name, copy these to the container module
  and update the name to avoid duplication.
* add test for "poor man" ComponentState

Fix #4128
Also use the original name when checking for the "mangled name" pattern when
doing undeclared Var assignment checking.
if proposed_name not in known_names:
break
proposed_name = f"{cls.__name__}_{ix}"
setattr(reflex.istate.dynamic, proposed_name, cls)
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't this prevent garbage collection if we never clean up the refs from the container module?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

the refs are for dynamically generated classes, so while it's possible that we won't clean these class objects up, i think that's okay, given that they are probably only created where needed for the app. i don't see a use case where an app creates a bunch of dynamic classes and then doesn't use them (why did it create them in the first place?)

Copy link
Contributor

@picklelo picklelo left a comment

Choose a reason for hiding this comment

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

working for me

@masenf masenf merged commit 736b2a6 into main Oct 11, 2024
39 checks passed
@masenf masenf deleted the masenf/pickle-local-state branch October 11, 2024 23:51
Kastier1 pushed a commit that referenced this pull request Oct 23, 2024
* Handle rx.State subclasses defined in function

* create a new container module: `reflex.istate.dynamic` to save references to
  dynamically generated substates.
* for substates with `<locals>` in the name, copy these to the container module
  and update the name to avoid duplication.
* add test for "poor man" ComponentState

Fix #4128

* test_state: disable local def handling for dupe-detection test

* Track the original module and name for type hint evaluation

Also use the original name when checking for the "mangled name" pattern when
doing undeclared Var assignment checking.
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.

AttributeError: Can't pickle local object 'dynamic_state.<locals>._DynamicState'
3 participants