Fix ends_with returning False for an empty suffix#119
Open
gaoflow wants to merge 1 commit into
Open
Conversation
_value_ends_with sliced value[-len(suffix):]. For an empty suffix,
-len(suffix) is -0 == 0, so the slice is value[0:] (the whole value)
and 'whole == empty' is False. Its sibling _value_starts_with uses
value[:len(prefix)], which for an empty prefix is value[:0] (an empty
slice) and correctly returns True. So starts_with('') was True while
ends_with('') was False, contradicting Python's str.endswith('')
semantics.
Index from the front (len(value) - len(suffix)) so an empty suffix
yields an empty slice. Equivalent to the old code for non-empty
suffixes.
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.
What
ends_withreturnsFalsefor an empty suffix, contradicting Python'sstr.endswith('')/bytes.endswith(b'')and its ownstarts_withsibling.Why
_value_ends_withslices from the back:For an empty suffix,
-len(suffix)is-0 == 0, so the slice isvalue[0:]— the whole value — and
whole == emptyisFalse. The sibling_value_starts_withslices from the front (value[:len(prefix)]), which foran empty prefix is
value[:0]= an empty slice, correctly yieldingTrue.The two handlers disagree on the empty case even though every non-empty case
matches.
Fix
Index from the front so an empty suffix yields an empty slice:
Equivalent to the old expression for every non-empty suffix; correct for the
empty one.
Tests
Added
test_ast_expression_value_methods_empty_affix, exercising STRING,BYTES and ARRAY through both
ends_withandstarts_with, asserting that anempty affix and the whole value both match. It fails before the change
(
ends_with('') should be True) and passes after. Full suite:Ran 455 tests ... OK (skipped=31);mypyclean on the changed file.This pull request was prepared with the assistance of AI, under my direction and review.