Skip to content

Type conversion problem if argument type definition contains str as type.  #4090

Closed

Description

@aaltat

I think there might conversion problem when argument type contains str as one of the types. In this case argument is not converted to other types but will remain as string. Example if I have this library:

from typing import Optional, Union


def optional_with_str(arg: Optional[str] = None) -> str:
    return f"arg: {arg}, type: {type(arg)}"


def union_with_dict(arg: Union[None, dict]) -> str:
    return f"arg: {arg}, type: {type(arg)}"


def union_with_complex(arg: Union[None, dict, str]) -> str:
    return f"arg: {arg}, type: {type(arg)}"

And when I have these tests:

*** Settings ***
Library    library.py

*** Test Cases ***
Example 1
    ${type} =    Optional With Str    ${None}
    Should Be Equal    ${type}    arg: None, type: <class 'NoneType'>
    ${type} =    Optional With Str    Kala
    Should Be Equal    ${type}    arg: Kala, type: <class 'str'>
    ${type} =    Optional With Str    None
    Should Be Equal    ${type}    arg: None, type: <class 'NoneType'>

Example 2
    ${type} =    Union With Dict    ${None}
    Should Be Equal    ${type}    arg: None, type: <class 'NoneType'>
    ${type} =    Union With Dict    None
    Should Be Equal    ${type}    arg: None, type: <class 'NoneType'>
    ${type} =    Union With Dict    {"key": 1}
    Should Be Equal    ${type}    arg: {'key': 1}, type: <class 'dict'>

Example 3.1
    ${type} =    Union With Complex    None
    Should Be Equal    ${type}    arg: None, type: <class 'NoneType'>

Example 3.2
    ${type} =    Union With Complex    {"key": 1}
    Should Be Equal    ${type}    arg: {'key': 1}, type: <class 'dict'>

In this demonstration tests Example 1, Example 3.1 and Example 3.2 fails. Only the test Example 2 passes.

Example 1
Example 1 test fails on last | Should Be Equal | ${type} | arg: None, type: <class 'NoneType'> |, because the Union With Dict keyword returns: arg: None, type: <class 'str'>. But I did expect keyword to convert the argument as Python None.

Example 3.1
Example 3.1 test fails on Should Be Equal keyword, because Union With Complex returns a arg: None, type: <class 'str'>. But I did expect keyword to convert the argument as Python None.

Example 3.2
Example 3.2 test fails on Should Be Equal keyword, because Union With Complex returns a arg: {"key": 1}, type: <class 'str'>. But I did expect keyword to convert the argument as Python dictionary.

Example 2
Example 2 test passes. I assume that it is because of the type hint does not contain str and in this case Robot Framework is forced to make conversion from string to one of the expected types.

I tested with RF 4.0.3 and 4.1.1 and both versions work in similar manner. This might be caused by the changes done in Robot Framework issue #3897

We did spot this issue with SeleniumLibrary robotframework/SeleniumLibrary#1739

Activity

  1. pekkaklarck commented on Sep 17, 2021

    @pekkaklarck
    Member

    It seems to me that everything works as designed. And I also think the design is fine.

    In RF 4.0 when Union support was introduced (#3738), we didn't check did the given argument type match one of the Union types but just tested conversion one-by-one. That caused problems explained in #3897 and we needed to change the design. As you can read from the comments to that issue, at first we decided to handle this so that we always attempt to convert strings but in the realized that causes problems as well and decided to always pass the argument as-is if its type matches any of the Union types.

  2. pekkaklarck commented on Sep 17, 2021

    @pekkaklarck
    Member

    As all these different issues demonstrate, this is a complicated situation with different design possibilities having different pros and cons. Although the current design has its problems, this particular issue is a good example of those issues, I still think it's the best possible generic solution.

    The best way to enhance the current situation is adding support for custom converters to allow libraries to handle cases where the generic solution doesn't work themselves. We already have #4088 about that so hopefully this gets better in RF 5.0. This issue makes the use case of being able to register a converter for a certain named argument, not only for any argument having a certain type, a lot more important.

  3. aaltat commented on Sep 17, 2021

    @aaltat
    ContributorAuthor

    Ah it was #3738 problem, now I remember that fully. Yes, I do not see clear way out of this problem, expect the one proposed with #4088 and introduction of custom converters.

  4. aaltat commented on Sep 17, 2021

    @aaltat
    ContributorAuthor

    I guess we can close this issue or is there some reason to keep it open?

  5. emanlove commented on Sep 18, 2021

    @emanlove
    Member

    Pekka, is there a master issue which we could place references to instance of "problems", like robotframework/SeleniumLibrary#1739? Maybe it is just #4088. Trying not to lose sight of where we see different results then expected.

  6. pekkaklarck commented on Sep 20, 2021

    @pekkaklarck
    Member

    I agree with @aaltat that this issue can be closed. The current design has its limitations, demonstrated by robotframework/SeleniumLibrary#1739, but it still seems to be the design with least problems. It's important to get #4088 implemented in RF 5.0 to make it possible to handle cases where the design isn't optimal.

    As a reference you can use #3738 that introduced Union support and #3897 that explains problems why the original design needed to be changed. #4088 then covers adding support for custom converters that will make it possible to handle this case specially on SL side.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions