Skip to content

Fix get_index_from_list returning -1 for negative start when value is found#5654

Open
sudheerr937-ai wants to merge 1 commit intorobotframework:masterfrom
sudheerr937-ai:fix-get-index-from-list-negative-start
Open

Fix get_index_from_list returning -1 for negative start when value is found#5654
sudheerr937-ai wants to merge 1 commit intorobotframework:masterfrom
sudheerr937-ai:fix-get-index-from-list-negative-start

Conversation

@sudheerr937-ai
Copy link
Copy Markdown
Contributor

get_index_from_list was returning -1 (not found) even when the value existed in the list, but only when a negative start like -1 or -2 was used.
Root cause: the math start + list_.index(value) accidentally produces -1 when start is negative and the value is at the end of the slice.
Fix: convert negative start to its real position in the list before doing the math
Added test cases in atest/testdata/standard_libraries/collections/list.robot covering negative start scenarios.
Fixes #5649

Copy link
Copy Markdown
Member

@pekkaklarck pekkaklarck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The overall approach how to fix the issue looks good, but there are few things to be fixed.

Get Index From List ${LONG} ${2} start=-1 expected=8 type=int
Get Index From List ${LONG} 43 start=-4 expected=5 type=int
Get Index From List ${LONG} nonex start=-3 expected=-1 type=int

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding new validation steps to this existing test is fine, but you could have also created a new test Get Index From List with negative indices or something like that. It would then need to be added to the matching file on the atest/robot side as well.

Please remove the unnecessary empty line.

)
start = 0
if isinstance(start, int) and start < 0:
start = max(len(list_) + start, 0)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. start is known to be int at this point so the isinstance check is unnecessary.
  2. Using max(len(list_) + start), 0) looks a bit odd to me. Wouldn't len(list_) + start) be enough? I can see that if you use a negative index that's out-of-the-range (e.g. list has length 5 and index is -7) the latter can map the index to a value that is incorrect but valid (e.g. -2 with the earlier values), but using max(..., 0) just changes that to an other incorrect value.
  3. I believe it would be better to map the start index to a positive value only after doing self.get_slice_from_list(...). In that case possible invalid indices (e.g. -7 when list has length 5) cause an error already then and afterwards we can safely do start = len(list_) + start with negative indices.

Comment thread src/robot/libraries/Collections.py
@pekkaklarck
Copy link
Copy Markdown
Member

Should we add a note to keyword documentation about negative start indices yielding invalid results prior to RF 7.5?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

get_index_from_list incorrectly returns -1 when value is found using negative start

2 participants