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

Ensure named_objs does not fail on unhashable objects #632

Merged
merged 4 commits into from
May 26, 2022
Merged
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
13 changes: 11 additions & 2 deletions param/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ def hashable(x):
else:
return x


def named_objs(objlist, namesdict=None):
"""
Given a list of objects, returns a dictionary mapping from
Expand All @@ -132,12 +133,20 @@ def named_objs(objlist, namesdict=None):
"""
objs = OrderedDict()

objtoname = {}
unhashables = []
if namesdict is not None:
objtoname = {hashable(v): k for k, v in namesdict.items()}
for k, v in namesdict.items():
try:
objtoname[hashable(v)] = k
except TypeError:
unhashables.append((k, v))

for obj in objlist:
if namesdict is not None and hashable(obj) in objtoname:
if objtoname and hashable(obj) in objtoname:
jbednar marked this conversation as resolved.
Show resolved Hide resolved
k = objtoname[hashable(obj)]
elif any(obj is v for (_, v) in unhashables):
k = [k for (k, v) in unhashables if v is obj][0]
jbednar marked this conversation as resolved.
Show resolved Hide resolved
elif hasattr(obj, "name"):
k = obj.name
elif hasattr(obj, '__name__'):
Expand Down