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

Fix implementation of rx.__iter__ #846

Merged
merged 2 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions param/reactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -941,20 +941,9 @@ def __iter__(self):
return
elif not isinstance(self._current, Iterable):
raise TypeError(f'cannot unpack non-iterable {type(self._current).__name__} object.')
iterator = []
def iterate(value):
if iterator:
iterate = iterator[0]
else:
iterate = iter(value)
iterator.append(iterate)
try:
yield next(iterate)
except StopIteration as e:
iterator.clear()
raise e
for item in self._apply_operator(iterate):
yield item
items = self._apply_operator(list)
for i in range(len(self._current)):
yield items[i]
philippjfr marked this conversation as resolved.
Show resolved Hide resolved

def _eval_operation(self, obj, operation):
fn, args, kwargs = operation['fn'], operation['args'], operation['kwargs']
Expand Down
14 changes: 14 additions & 0 deletions tests/testreactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,20 @@ def test_reactive_iter():
assert a.rx.resolve() == 'b'
assert b.rx.resolve() == 'a'

def test_reactive_multi_iter():
i = rx(('a', 'b'))
a1, b1 = i
a2, b2 = i
assert a1.rx.resolve() == 'a'
assert b1.rx.resolve() == 'b'
assert a2.rx.resolve() == 'a'
assert b2.rx.resolve() == 'b'
i.rx.set_input(('b', 'a'))
assert a1.rx.resolve() == 'b'
assert b1.rx.resolve() == 'a'
assert a2.rx.resolve() == 'b'
assert b2.rx.resolve() == 'a'

def test_reactive_is():
i = rx(None)
is_ = i.rx.is_(None)
Expand Down