Skip to content

Commit 398e60b

Browse files
committed
UG: Document that Union[str, int] convers all args to strings.
Better to use Union[int, str]. Related to robotframework#3738. Fixes robotframework#3867.
1 parent adb9801 commit 398e60b

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

doc/userguide/src/ExtendingRobotFramework/CreatingTestLibraries.rst

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,7 +1541,8 @@ Specifying multiple possible types
15411541
Starting from Robot Framework 4.0, it is possible to specify that an argument
15421542
has multiple possible types. In this situation argument conversion is attempted
15431543
based on each type in the order they have been specified. If any conversion
1544-
succeeds, the resulting value is used, and otherwise the whole conversion fails.
1544+
succeeds, the resulting value is used without attempting conversion to remaining
1545+
types. If no type conversion succeeds, the whole conversion fails.
15451546

15461547
When using function annotations, the natural syntax to specify that argument
15471548
has multiple possible types is using Union_:
@@ -1556,7 +1557,7 @@ has multiple possible types is using Union_:
15561557

15571558
An alternative is giving types a tuple. It is not recommended with annotations
15581559
because that syntax is not supported by other tools, but it works well with
1559-
the `@keyword` decorator:
1560+
the `@keyword` decorator and is Python 2 compatible:
15601561

15611562
.. sourcecode:: python
15621563

@@ -1571,6 +1572,12 @@ With the above examples the `length` argument would first be converted to an
15711572
integer and if that fails then to a float. The `padding` would be first
15721573
converted to `None`, then to an integer, and finally to a string.
15731574

1575+
Because conversion is attempted one-by-one and string conversion always succeeds,
1576+
possible `str` should be the last type. For example, using `Union[str, int]` would
1577+
cause all arguments, including integers, to be converted to strings, but
1578+
`Union[int, str]` means that integer conversion is attempted first and string
1579+
conversion is done only if that fails.
1580+
15741581
If any of the specified types is not recognized by Robot Framework and
15751582
the given argument cannot be converted to any of the types before it,
15761583
the given argument will be used as-is. For example, with this keyword

0 commit comments

Comments
 (0)