anvilistas / reactive

signals for anvil
Other
1 stars 0 forks source link

Possible issue with missing keys in dictionary #7

Closed jshaffstall closed 1 day ago

jshaffstall commented 5 days ago

Describe the bug

I'm creating a reactive class that contains an empty dictionary (the same behavior happens if the dictionary has items in it). I then have a render_effect function that uses that dictionary but with a key that doesn't exist. The thought is that there's optional information that may be added to the dictionary later.

The render_effect function looks something like this:

    @render_effect
    def update_display(self):
        self.label_1.text = Globals.data['foo'] if 'foo' in Globals.data else 'Foo not set'

I then have a button that adds in the optional information:

    def button_1_click(self, **event_args):
        Globals.data['foo'] = 'button worked'

But the render_effect function is never called. If the dictionary in the reactive class contains an initial value for the key, then everything works the way it should.

For all I know this might be the intended behavior, and reactive doesn't support adding new items to the dictionary later. But I figured it was worth checking to see if that's the case.

Version

The current main branch of reactive.

To Reproduce

Using https://anvil.works/build#clone:PO7DRW24EONBDSVQ=TQY5KSQ5R537SNJPGCSSDUJG click on the button. The label text should change, but does not.

s-cork commented 5 days ago

Feels like it should work.

s-cork commented 5 days ago

a slight design floor in our current implementation, so i'll need to think a little bit more about this

The issue is that if the key is not in the dictionary, then the current implementation can't track changes to that key

But it definitely should work Will need to see how they do this in solid signals But it won't be such a quick fix

For now I'd say pre populate the dictionary with None or a sentinel

jshaffstall commented 5 days ago

Will do. Appreciate you taking a look at it.

jshaffstall commented 1 day ago

Nice! It even works as sub-dictionaries are added by code.