You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Argument conversion is different between 3.2.2. and 4.0 #3867
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.
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.
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].
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.
@pekkaklarck This behavior is not acceptable for optional strings.
deffoo(a: str=None) # type is Union[str, None]:passdefbar(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.
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,
Activity
pekkaklarck commentedon Feb 26, 2021
This is due the new Union conversion support (#3738) and to some extend string conversion support (#3736). The first type in the
Unionisstrand 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:As explained above, the alternative to check does the argument match some of the types in the
Unionwould be even worse than the current situation and handlingstrinUnionsomehow specially would be pretty complicated. For the user a simple solution is havingstrlast in theUnioni.e. usingUnion[dict, None, str]in the original example. If that's not considered adequate, we can still consider specialstrhandling in this context.pekkaklarck commentedon Feb 26, 2021
The reason this works as it works with RF 3.2 is that it doesn't support
Unionconversion and the type hintUnion[str, dict, None]in the original example the is totally ignored. This means that, for example,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].pekkaklarck commentedon Feb 26, 2021
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.
KotlinIsland commentedon Mar 18, 2021
@pekkaklarck This behavior is not acceptable for optional strings.
Given these very idiomatic/pythonic type annotations, it is impossible to pass
Nonefrom 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.KotlinIsland commentedon Mar 18, 2021
I would expect:
KotlinIsland commentedon Mar 18, 2021
Actually, why not check if the current value is in the list of types first before trying to convert it.
test itis str | dictaaltat commentedon Mar 18, 2021
This is actually explained in the user guide and has changed in the 4.0 https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#supported-conversions
Therefore in this example
You should change the type hint to
Union[dict, str]KotlinIsland commentedon Mar 18, 2021
It doesn't need to convert at all, it already has a valid type.