anvilistas / anvil-labs

MIT License
9 stars 3 forks source link

Atomic: lists in atoms do not recognize the key argument for in-place sorts #150

Closed jshaffstall closed 1 month ago

jshaffstall commented 1 month ago

Here's a clone link if you need it to play with: https://anvil.works/build#clone:PO7DRW24EONBDSVQ=TQY5KSQ5R537SNJPGCSSDUJG

But the issue can be seen in the code below, where the in-place list sorting will fail because of the key argument:

@atom
class GlobalState:
    def __init__(self):
        self._data = []

    @property
    @selector
    def data(self):
        return self._data

    @action
    def add_data(self, name):
        self._data.append({
            'name': name
        })

        # This works
        # self._data = sorted(self._data, key=lambda x: x['name'])

        # This doesn't work
        self._data.sort(key=lambda x: x['name'])
s-cork commented 1 month ago

I would say that work on atomic has stopped in favour of anvil reactive see this forum post: https://anvil.works/forum/t/anvil-reactive-add-reactivity-to-your-anvil-apps/19526

here's the repo: https://github.com/anvilistas/reactive

Third Party app id: N7KFE4YBWMGWJ5OX

jshaffstall commented 1 month ago

Well, that was a disappointment for a few moments. Luckily the transition from atomic to reactive is easy, and the same patterns are usable, so it still has that MVC feel to it.

However, the same sort of bug exists when trying to sort a list in place that's in a reactive class. The exact error is TypeError: 'StoreSignal' does not support indexing.

s-cork commented 1 month ago

do you have another clone link and i'll play with it

jshaffstall commented 1 month ago

I updated the previous clone to use Reactive: https://anvil.works/build#clone:PO7DRW24EONBDSVQ=TQY5KSQ5R537SNJPGCSSDUJG

Run it and it'll automatically add something to the list and try to sort it in place.

s-cork commented 1 month ago

ok should now be fixed

jshaffstall commented 1 month ago

Thanks!