Skip to content

Make FOR IN ZIP loop behavior if lists have different lengths configurable #4682

Closed

Description

@pekkaklarck

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
    END

This 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}
    END

To 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

  1. added this to the v6.1 milestone on Mar 12, 2023
  2. pekkaklarck commented on Mar 12, 2023

    @pekkaklarck
    MemberAuthor

    Validations using FOR IN ZIP loops 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 the strict mode the default in the future. If we add mode in RF 6.1, we could consider deprecating not using shortest or longest modes when lengths are different already in RF 6.2 and then in RF 7 script could 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 ZIP loops currently expect all items to be lists, so adding optional configuration options cannot break any valid usage.

  3. pekkaklarck commented on Mar 12, 2023

    @pekkaklarck
    MemberAuthor

    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 ENUMERATE loops already accept similar start option (#3781) and also WHILE and TRY/EXCEPT have options limit and type so there's some precedence. With WHILE and TRY/EXCEPT these config options are recognized already by the parser and they are part of the data model as well. With FOR IN ENUMERATE loops start is only handled by the runner, and model objects store it as any other loop value. I believe start and these possible new FOR IN ZIP options 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 FOR loop types using a single model object that has a flavor attribute specifying the type. If we store start, mode and fillvalue (perhaps it should be fill_value or just fill) 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 a config attribute 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.

  4. added a commit that references this issue on Mar 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions