Repository navigation
Description
Robot FOR IN ZIP loop behaves like Python's zip so that if lists lengths aren't the same, items from the longest one are ignored. For example, the following loop would be executed only twice:
*** Variables ***
@{ANIMALS} dog cat horse cow elephant
@{ELÄIMET} koira kissa
*** Test Cases ***
Example
FOR ${en} ${fi} IN ZIP ${ANIMALS} ${ELÄIMET}
Log ${en} is ${fi} in Finnish
ENDThis behavior can cause problems when iterating over items received from the automated system. For example, the following test would pass regardless how many things Get something returns as long as the returned items match the expected values. The example even succeeds if Get something nothing.
*** Test Cases ***
Example
Validate something expected 1 expected 2 expected 3
*** Keywords ****
Validate something
[Arguments] @{expected}
@{actual} = Get something
FOR ${act} ${exp} IN ZIP ${actual} ${expected}
Validate one thing ${act} ${exp}
ENDTo avoid the above example failing if Get something returns more or less values than expected, a separate check needs to be added. Because there's no ready-made keyword for checking are list lengths equal (Lists should be equal keyword doesn't work here), the validation needs to use something bit more complicated like this:
Should be equal ${{len($actual)}} ${{len($expected)}}
For a real life example see this example from out acceptance tests. Tests also contain various FOR IN ZIP usages where this particular issue could cause problems.
This same underlying issues has caused problems with Python's zip as well. Starting from Python 3.10 (see PEP 619 zip has new strict parameter that can be used like zip(actual, expected, strict=True). It causes a ValueError to be raised if iterated lists have different lengths. Related to this, Python also has separate itertools.zip_longest that goes through all items in all lists so that if certain list has less value than others, value specified with the fillvalue argument is used instead.
My proposal is that we add new mode configuration option to FOR IN ZIP loops. The motivation is that we could have three values shortest, strict and longest emulating Python's behavior with zip(...), zip(..., strict=True=), and zip_longest(...), respectively. This was also considered with Python, but they decided to go with a Boolean option strict partly because they already have a separate zip_longest.
Another reason mode wasn't used with Python is that to properly support the longest case, it would require adding fillvalue that doesn't make sense with other modes. That's a valid concern in our case as well, but I don't consider it too big a problem. We don't at the moment support the longest case at all, so this would instead be a nice enhancement and a lot better syntax than separate FOR IN ZIP LONGEST would be.
Activity
pekkaklarck commentedon Mar 12, 2023
Validations using
FOR IN ZIPloops passing if list lengths aren't equal can cause severe issues and I believe this thus is a pretty important change. I believe it's so severe that we should actually make thestrictmode the default in the future. If we addmodein RF 6.1, we could consider deprecating not usingshortestorlongestmodes when lengths are different already in RF 6.2 and then in RF 7scriptcould be the default. Perhaps the priority of this issue should thus be high and not medium that I initially used.This issue is fully backwards incompatible.
FOR IN ZIPloops currently expect all items to be lists, so adding optional configuration options cannot break any valid usage.pekkaklarck commentedon Mar 12, 2023
Implementing this is pretty simple, which is also part of the reason we could still do this in RF 6.1. There are some design decisions to me made related to the internal implementation.
FOR IN ENUMERATEloops already accept similarstartoption (#3781) and alsoWHILEandTRY/EXCEPThave optionslimitandtypeso there's some precedence. WithWHILEandTRY/EXCEPTthese config options are recognized already by the parser and they are part of the data model as well. WithFOR IN ENUMERATEloopsstartis only handled by the runner, and model objects store it as any other loop value. I believestartand these possible newFOR IN ZIPoptions should be handled by the parser and stored in the data model separately, though. The question is, how should they be stored.We handle all
FORloop types using a single model object that has aflavorattribute specifying the type. If we storestart,modeandfillvalue(perhaps it should befill_valueor justfill) as individual attributes, then they are used also with loop flavors that don't actually support them. That can be a bit odd, especially if we'd later get even more such config options. An alternative to storing these options as individual values would be storing them as aconfigattribute using a dictionary. That would also allow adding new config's without changing APIs at all. Regardless how we decide to do this, I believe the parser should be clever enough to support these modes only with correct flavors.Add Statement.get_option() helper.
Take new FOR IN ZIP modes (#4682) into use in tests.
Pass FOR IN ENUMERATE/ZIP extra info to listeners.
Add Statement.get_option() helper.
Configurable FOR IN ZIP behavior if lengths differ.
Take new FOR IN ZIP modes (#4682) into use in tests.
Pass FOR IN ENUMERATE/ZIP extra info to listeners.