Return false for contains(string, non-string) instead of raising TypeError#360
Open
gaoflow wants to merge 1 commit into
Open
Return false for contains(string, non-string) instead of raising TypeError#360gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
contains() with a string subject and a non-string search raised an
uncaught TypeError ("'in <string>' requires string as left operand")
instead of returning false. The JMESPath spec defines
contains('foobar', `123`) as false, and the array case already returns
false for a type-mismatched search of any type, so the string and array
paths were inconsistent.
Guard the membership test: when the subject is a string and the search
is not, return false (a non-string can never be a substring). Adds
compliance cases for number, boolean and null searches.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
contains()with a string subject and a non-string search raises an uncaughtTypeErrorinstead of returningfalse:This contradicts the JMESPath spec, which defines
contains('foobar',123) → false. It affects any non-string search against a string subject — number, boolean, null, array, or object.The array case already returns
falsefor a type-mismatched search of any type (contains([1,2,3],false) → false), because Python'sinon a list accepts any right-hand type. Only the string path was inconsistent, since123 in "foobar"raises.Cause
Functions._func_containswas simply:The signature permits a
searchof any type, butsearch in subjectraisesTypeErrorwhensubjectis a string andsearchis not.Fix
Guard the membership test: when the subject is a string and the search is not, return
false(a non-string can never be a substring of a string). This brings the string path in line with the array path and the spec.Verification
tests/compliance/functions.json), each expectingfalse. They fail withTypeErrorbefore the fix and pass after (confirmed viagit stash).This pull request was prepared with the assistance of AI, under my direction and review.