Skip to content

Commit

Permalink
fix resolve_ref recursion
Browse files Browse the repository at this point in the history
  • Loading branch information
maximlt committed Jun 18, 2024
1 parent 4877a1d commit 1b9c320
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
6 changes: 3 additions & 3 deletions param/parameterized.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,11 @@ def resolve_ref(reference, recursive=False):
"""
if recursive:
if isinstance(reference, (list, tuple, set)):
return [r for v in reference for r in resolve_ref(v)]
return [r for v in reference for r in resolve_ref(v, recursive)]
elif isinstance(reference, dict):
return [r for kv in reference.items() for o in kv for r in resolve_ref(o)]
return [r for kv in reference.items() for o in kv for r in resolve_ref(o, recursive)]
elif isinstance(reference, slice):
return [r for v in (reference.start, reference.stop, reference.step) for r in resolve_ref(v)]
return [r for v in (reference.start, reference.stop, reference.step) for r in resolve_ref(v, recursive)]
reference = transform_reference(reference)
if hasattr(reference, '_dinfo'):
dinfo = getattr(reference, '_dinfo', {})
Expand Down
49 changes: 48 additions & 1 deletion tests/testrefs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import param
import pytest

from param.parameterized import Skip
from param.parameterized import Skip, resolve_ref
from param.reactive import bind, rx

class Parameters(param.Parameterized):
Expand Down Expand Up @@ -286,3 +286,50 @@ def gen_strings2():
assert task1.done()
assert not task2.done()
assert len(threads) == 2

def test_resolve_ref_parameter():
p = Parameters()
refs = resolve_ref(p.param.string)
assert len(refs) == 1
assert refs[0] is p.param.string

def test_resolve_ref_depends_method():
p = Parameters()
refs = resolve_ref(p.formatted_string)
assert len(refs) == 1
assert refs[0] is p.param.string

def test_resolve_ref_recursive_list():
p = Parameters()
nested = [[p.param.string]]
refs = resolve_ref(nested, recursive=True)
assert len(refs) == 1
assert refs[0] is p.param.string

def test_resolve_ref_recursive_set():
p = Parameters()
nested = {(p.param.string,)} # Parameters aren't hashable
refs = resolve_ref(nested, recursive=True)
assert len(refs) == 1
assert refs[0] is p.param.string

def test_resolve_ref_recursive_tuple():
p = Parameters()
nested = ((p.param.string,),)
refs = resolve_ref(nested, recursive=True)
assert len(refs) == 1
assert refs[0] is p.param.string

def test_resolve_ref_recursive_dict():
p = Parameters()
nested = {'0': {'0': p.param.string}}
refs = resolve_ref(nested, recursive=True)
assert len(refs) == 1
assert refs[0] is p.param.string

def test_resolve_ref_recursive_slice():
p = Parameters()
nested = [slice(p.param.string)]
refs = resolve_ref(nested, recursive=True)
assert len(refs) == 1
assert refs[0] is p.param.string

0 comments on commit 1b9c320

Please sign in to comment.