Skip to content

Commit c50f15b

Browse files
committed
Remote: Support argument conversion based on defaults
Returning `None` from `get_keyword_types` by default disabled argument conversion. Fixes robotframework#3873.
1 parent 3cea135 commit c50f15b

File tree

6 files changed

+46
-7
lines changed

6 files changed

+46
-7
lines changed

atest/robot/standard_libraries/remote/arguments.robot

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ Required Arguments
1212
Arguments With Default Values
1313
Check Test Case ${TESTNAME}
1414

15+
Defaults as tuples
16+
Check Test Case ${TESTNAME}
17+
18+
Arguent conversion based on defaults
19+
Check Test Case ${TESTNAME}
20+
1521
Named Arguments
1622
Check Test Case ${TESTNAME}
1723

atest/testdata/standard_libraries/remote/arguments.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ def get_keyword_types(self, name):
1515
kw = getattr(self.library, name)
1616
return getattr(kw, 'robot_types', None)
1717

18+
def get_keyword_arguments(self, name):
19+
if name == 'defaults_as_tuples':
20+
return [('first', 'eka'), ('second', 2)]
21+
return RemoteServer.get_keyword_arguments(self, name)
22+
1823

1924
class Arguments(object):
2025

@@ -70,6 +75,10 @@ def varargs(self, *args):
7075
def required_defaults_and_varargs(self, req, default='world', *varargs):
7176
return self._format_args(req, default, *varargs)
7277

78+
# Handled separately by get_keyword_arguments above.
79+
def defaults_as_tuples(self, first='eka', second=2):
80+
return self._format_args(first, second)
81+
7382
def kwargs(self, **kwargs):
7483
return self._format_args(**kwargs)
7584

atest/testdata/standard_libraries/remote/arguments.robot

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,17 @@ Arguments With Default Values
2020
one, two, 3 Arguments With Default Values one two
2121
one, 2 (int), 3 Arguments With Default Values one
2222

23+
Defaults as tuples
24+
eka, 2 (int) Defaults as tuples
25+
xxx, 2 (int) Defaults as tuples xxx
26+
xxx, 3 (int) Defaults as tuples xxx 3
27+
28+
Arguent conversion based on defaults
29+
xxx, 3 (int) Defaults as tuples xxx 3
30+
xxx, 3.14 (float) Defaults as tuples xxx 3.14
31+
xxx, yyy Defaults as tuples xxx yyy
32+
0 (int), 3 (int) Defaults as tuples ${0} 3
33+
2334
Named Arguments
2435
first, second, 3 Arguments With Default Values first arg2=second
2536
first, second, 3 Arguments With Default Values arg1=first arg2=second

doc/userguide/src/ExtendingRobotFramework/CreatingTestLibraries.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1195,6 +1195,8 @@ but using them as named arguments causes an error on Python side.
11951195

11961196
__ https://www.python.org/dev/peps/pep-0570/
11971197

1198+
.. _argument conversion:
1199+
11981200
Argument types
11991201
~~~~~~~~~~~~~~
12001202

doc/userguide/src/ExtendingRobotFramework/RemoteLibrary.rst

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,26 +249,36 @@ and what they must return is explained in the table below.
249249
`get_keyword_tags` Tags as a list of strings.
250250
=========================== ======================================
251251

252-
Type information can be returned either as a list mapping type names to
253-
arguments based on position or as a dictionary mapping argument names to
254-
type names directly. In practice this works the same way as when
255-
`specifying types using the @keyword decorator`__ with normal libraries.
256-
The difference is that because the XML-RPC protocol does not support
252+
Type information used for `argument conversion`_ can be returned either as
253+
a list mapping type names to arguments based on position or as a dictionary
254+
mapping argument names to type names directly. In practice this works the same
255+
way as when `specifying types using the @keyword decorator`__ with normal
256+
libraries. The difference is that because the XML-RPC protocol does not support
257257
arbitrary values, type information needs to be specified using type names
258258
or aliases like `'int'` or `'integer'`, not using actual types like `int`.
259259
Additionally `None` or `null` values may not be allowed by the XML-RPC server,
260260
but an empty string can be used to indicate that certain argument does not
261261
have type information instead.
262262

263+
Argument conversion is supported also based on default values using the
264+
`same logic as with normal libraries`__. For this to work, arguments with
265+
default values must be returned as tuples, not as strings, the `same way
266+
as with dynamic libraries`__. For example, argument conversion works if
267+
argument information is returned like `[('count', 1), ('caseless', True)]`
268+
but not if it is `['count=1', 'caseless=True']`.
269+
263270
Remote servers can also provide `general library documentation`__ to
264271
be used when generating documentation with the Libdoc_ tool. This information
265272
is got by calling `get_keyword_documentation` with special values `__intro__`
266273
and `__init__`.
267274

268-
.. note:: `get_keyword_types` is new in Robot Framework 3.1.
275+
.. note:: `get_keyword_types` is new in Robot Framework 3.1 and support for
276+
argument conversion based on defaults is new in Robot Framework 4.0.
269277

270278
__ `Getting keyword arguments`_
271279
__ `Specifying argument types using @keyword decorator`_
280+
__ `Implicit argument types based on default values`_
281+
__ `Getting keyword arguments`_
272282
__ `Getting general library documentation`_
273283

274284
Using `get_library_information`

src/robot/libraries/Remote.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ def _get_kw_info(self, kw, info, getter, default=None):
100100
return default
101101

102102
def get_keyword_types(self, name):
103-
return self._get_kw_info(name, 'types', self._client.get_keyword_types)
103+
return self._get_kw_info(name, 'types', self._client.get_keyword_types,
104+
default=())
104105

105106
def get_keyword_tags(self, name):
106107
return self._get_kw_info(name, 'tags', self._client.get_keyword_tags)

0 commit comments

Comments
 (0)