Skip to content
Open
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
8 changes: 8 additions & 0 deletions jmespath/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ def _func_to_number(self, arg):

@signature({'types': ['array', 'string']}, {'types': []})
def _func_contains(self, subject, search):
# A non-string can never be a substring of a string. The array case
# already returns False for a non-matching search of any type, but
# ``search in subject`` raises TypeError when subject is a string and
# search is not, so guard it explicitly (spec: contains('foobar', 123)
# -> false).
if (isinstance(subject, STRING_TYPE)
and not isinstance(search, STRING_TYPE)):
return False
return search in subject

@signature({'types': ['string', 'array', 'object']})
Expand Down
12 changes: 12 additions & 0 deletions tests/compliance/functions.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@
"expression": "contains('abc', 'd')",
"result": false
},
{
"expression": "contains('foobar', `123`)",
"result": false
},
{
"expression": "contains('foobar', `true`)",
"result": false
},
{
"expression": "contains('foobar', `null`)",
"result": false
},
{
"expression": "contains(`false`, 'd')",
"error": "invalid-type"
Expand Down