Skip to content

Argument conversion is different between 3.2.2. and 4.0 #3867

Closed

Description

@aaltat

When I have library like this:

from typing import Union


def keyword(arg: Union[str, dict, None] = None):
    print(arg)
    print(type(arg))

And tests like this

*** Setting ***
Library    MyLib.py

*** Variable ***
&{A}    key=value

*** Test Cases ***
Test A
    Keyword    ${A}

Test B
    Keyword    None

Test C
    Keyword    ${None}

Then the Robot Framework 4.0 the logging shows that arg type is always a string: <class 'str'>. But with Robot Framework 3.2.2, Test A logs <class 'robot.utils.dotdict.DotDict'>, the Test B logs: <class 'str'> and Test C logs <class 'NoneType'>.

I would thing that the conversion of the 3.2.2 to be more correct although it is not perfect.

Activity

  1. pekkaklarck commented on Feb 26, 2021

    @pekkaklarck
    Member

    This is due the new Union conversion support (#3738) and to some extend string conversion support (#3736). The first type in the Union is str and Robot somewhat blindly converts all arguments to strings. The exact problem is discussed in the description of the former issue as the first design decision to be made:

    Should we first check does the given argument match any of the possible types before doing conversion? If, for example, we have arg: Union[str, int] and the given argument is an integer, should we pass it as is (it matches one of the possible types) or should we test conversion with possible types one-by-one? In the latter case we'd convert the integer to a string because the first possible type is str. In this case checking does the argument match any of the types feels better.

    On the other hand, if we have arg: Union[int, str] and call with a string 2, checking does the given argument match any of the type would mean that we don't do any conversion because one of the types is str. If we'd go through types one-by-one, we'd convert it to an integer because int is before str. In this case testing conversion one-by-one works better.

    We could also make it so that we test conversions one-by-one if the given argument is a string and otherwise first check does the argument match any of the types. That feels a bit inconsistent and hard to explain, though. I got a feeling that going through types one-by-one works best. The key thing to remember is that str should be the last type to avoid converting all arguments to strings.

    As explained above, the alternative to check does the argument match some of the types in the Union would be even worse than the current situation and handling str in Union somehow specially would be pretty complicated. For the user a simple solution is having str last in the Union i.e. using Union[dict, None, str] in the original example. If that's not considered adequate, we can still consider special str handling in this context.

  2. pekkaklarck commented on Feb 26, 2021

    @pekkaklarck
    Member

    The reason this works as it works with RF 3.2 is that it doesn't support Union conversion and the type hint Union[str, dict, None] in the original example the is totally ignored. This means that, for example,

    Keyword    {'a': 1}
    

    would mean calling the keyword with a string, not with a dictionary. With RF 4 the argument is converted to a dictionary if the type hint is Union[dict, None, str].

  3. pekkaklarck commented on Feb 26, 2021

    @pekkaklarck
    Member

    I discussed this with @aaltat and we agreed the current behavior is fine but needs to be explicitly documented. It turned out documentation related this can be enhanced so I'll do that.

  4. KotlinIsland commented on Mar 18, 2021

    @KotlinIsland
    Contributor

    @pekkaklarck This behavior is not acceptable for optional strings.

    def foo(a: str = None) # type is Union[str, None]:
        pass
    def bar(a: Optional[str]) # type is Union[str, None]:
        pass

    Given these very idiomatic/pythonic type annotations, it is impossible to pass None from robot, this current behavior is extremely unintuitive/unexpected.

    I would suggest that when the keyword is called, robot should prioritize ${None} as NoneType when the type Union contains None.

  5. KotlinIsland commented on Mar 18, 2021

    @KotlinIsland
    Contributor

    I would expect:

    foo  None  # passes string "None"
    foo  ${None}  # passes None
    foo  ${{None or None}}  # passes None
    foo  ${{None or "None"}}  # passes string "None"
  6. KotlinIsland commented on Mar 18, 2021

    @KotlinIsland
    Contributor

    Actually, why not check if the current value is in the list of types first before trying to convert it.

    test
        ${a} =  get
        what is  ${a}
        test  ${a}
    rf = BuiltIn()
    
    def get():
        return {":)": 100}
    
    def what_is(this):
        rf.log(this)
        rf.log(type(this))
    
    def test(it: Union[str, dict]):
        rf.log(it)
        rf.log(type(it))
    1. test it is str | dict
    2. if it is str or dict then pass value as is
    3. else run coercion algorithm as normal
  7. aaltat commented on Mar 18, 2021

    @aaltat
    ContributorAuthor

    This is actually explained in the user guide and has changed in the 4.0 https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#supported-conversions

    Specifying multiple possible types
    
    Starting from Robot Framework 4.0, it is possible to specify that an argument has multiple possible types. In this situation argument conversion is attempted based on each type in the order they have been specified. If any conversion succeeds, the resulting value is used without attempting conversion to remaining types. If no type conversion succeeds, 
    

    Therefore in this example

    def test(it: Union[str, dict]):
        rf.log(it)
        rf.log(type(it))
    

    You should change the type hint to Union[dict, str]

  8. KotlinIsland commented on Mar 18, 2021

    @KotlinIsland
    Contributor

    It doesn't need to convert at all, it already has a valid type.

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