Skip to content

Commit 0dbac0d

Browse files
authored
Bug Fix: the CONTAINS and NOT_CONTAINS operators cannot compare against sets (#332)
1 parent ea2a2ee commit 0dbac0d

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

pynamodb/models.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
CAPACITY_UNITS, META_CLASS_NAME, REGION, HOST, EXISTS, NULL,
3333
DELETE_FILTER_OPERATOR_MAP, UPDATE_FILTER_OPERATOR_MAP, PUT_FILTER_OPERATOR_MAP,
3434
COUNT, ITEM_COUNT, KEY, UNPROCESSED_ITEMS, STREAM_VIEW_TYPE, STREAM_SPECIFICATION,
35-
STREAM_ENABLED, EQ, NE)
35+
STREAM_ENABLED, EQ, NE, BINARY_SET, STRING_SET, NUMBER_SET)
3636

3737

3838
log = logging.getLogger(__name__)
@@ -1057,9 +1057,17 @@ def _build_filters(cls,
10571057
else:
10581058
if not isinstance(value, list):
10591059
value = [value]
1060-
value = [
1061-
{ATTR_TYPE_MAP[attribute_class.attr_type]: attribute_class.serialize(val)} for val in value
1062-
]
1060+
attr_type = attribute_class.attr_type
1061+
if operator in ['contains', 'not_contains'] and attr_type in [BINARY_SET, STRING_SET, NUMBER_SET]:
1062+
# The 'CONTAINS' and 'NOT_CONTAINS' comparison operators do not support a set in ATTR_VALUE_LIST.
1063+
# If the `attribute_class` to serialize is a set, take the first element of type and value.
1064+
value = [
1065+
{ATTR_TYPE_MAP[attr_type][0]: attribute_class.serialize(val)[0]} for val in value
1066+
]
1067+
else:
1068+
value = [
1069+
{ATTR_TYPE_MAP[attr_type]: attribute_class.serialize(val)} for val in value
1070+
]
10631071
condition = {
10641072
ATTR_VALUE_LIST: value
10651073
}

pynamodb/tests/test_model.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2674,7 +2674,7 @@ def test_index_queries(self):
26742674
'aliases': {
26752675
'AttributeValueList': [
26762676
{
2677-
'SS': ['1']
2677+
'S': '1'
26782678
}
26792679
],
26802680
'ComparisonOperator': 'CONTAINS'

0 commit comments

Comments
 (0)