Skip to content

Commit

Permalink
fix passing lists to querybuilder
Browse files Browse the repository at this point in the history
fix aiidateam#2418

 * convert lists/sets to tuples for cls <> tag list
 * add test that uses list instead of tuple
  • Loading branch information
ltalirz committed Jan 30, 2019
1 parent 5228cbb commit 01b11d3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
4 changes: 4 additions & 0 deletions aiida/backends/tests/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,10 @@ def test_subclassing(self):
filters={'attributes.cat': 'miau'}, subclassing=False)
self.assertEqual(qb.count(), 2)

# Just for safety, also test for using a list instead of a tuple
qb = QueryBuilder().append(cls=[StructureData, ParameterData], filters={'attributes.cat': 'miau'})
self.assertEqual(qb.count(), 2)

def test_list_behavior(self):
from aiida.orm import Node
from aiida.orm.querybuilder import QueryBuilder
Expand Down
12 changes: 9 additions & 3 deletions aiida/orm/querybuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ def _get_unique_tag(self, classifiers):
Can also be a list of dictionaries, when multiple classes are passed to QueryBuilder.append
:returns: A tag, as a string.
:returns: A tag as a string (it is a single string also when passing multiple classes).
"""

def get_tag_from_type(classifiers):
Expand Down Expand Up @@ -621,7 +621,13 @@ def append(self,

l_class_added_to_map = False
if cls:
if cls in self._cls_to_tag_map.keys():
# Note: tuples can be used as array keys, lists & sets can't
if isinstance(cls, (list, set)):
tag_key = tuple(cls)
else:
tag_key = cls

if tag_key in self._cls_to_tag_map.keys():
# In this case, this class already stands for another
# tag that was used before.
# This means that the first tag will be the correct
Expand All @@ -630,7 +636,7 @@ def append(self,
pass

else:
self._cls_to_tag_map[cls] = tag
self._cls_to_tag_map[tag_key] = tag
l_class_added_to_map = True

# ALIASING ##############################
Expand Down

0 comments on commit 01b11d3

Please sign in to comment.